CHAPTER 22
Beginner
Authentication and Authorization
Updated: May 18, 2026
5 min read
# CHAPTER 22
Authentication and Authorization
1. Chapter Introduction
Authentication (who are you?) and Authorization (what can you access?) are core requirements for any real application. This chapter builds a complete auth system: JWT-based login, Pinia auth store, protected routes, and role-based access control.2. Learning Objectives
- Implement JWT-based authentication.
- Create an auth store with Pinia.
- Protect routes with navigation guards.
- Implement role-based authorization.
- Build a complete login/logout flow.
3. Auth Store (Pinia)
javascript
4. Navigation Guards
javascript
5. Login Component
vue
6. Common Mistakes
-
Storing tokens in memory only: Tokens in
ref()disappear on refresh. UselocalStoragefor persistence.
-
Not refreshing axios headers after login: Set
axios.defaults.headers.common['Authorization']in the login action.
7. MCQs
Question 1
JWT stands for?
Question 2
Where should the auth token be stored?
Question 3
Navigation guard to.meta.requiresAuth accesses?
Question 4
guestOnly meta redirects logged-in users to?
Question 5
axios.defaults.headers.common sets?
Question 6
Logout should?
Question 7
Role-based access in guard checks?
Question 8
query.redirect in login route?
Question 9
fetchCurrentUser on app start is used to?
Question 10
isLoggedIn = computed(() => !!token.value) converts token to?
8. Interview Questions
- Q: How do you protect routes in Vue Router using navigation guards?
- Q: How do you persist authentication state across page refreshes?