A Vue 3 example of how to use route guards to redirect users to the login page if they are not authenticated. This example uses Vue Router and Pinia for state management.
This project uses Node 18+
Created using Vite and the Vue 3 template.
- Clone or download the repo to your local machine
- Navigate to the project root using your terminal
- Run
npm i
to install the project dependencies - Run
npm run dev
to start the development server
- Home page accessible to all users
- Login page accessible to all users
- Show Login button when User is not authenticated
- Show Logout button when User is authenticated
- Show Dashboard in navigation when User is authenticated
- Dashboard page accessible only to authenticated users
- Redirect User to Login page if attempting to visit the Dashboard page if not authenticated
- Logout should redirect User to Home page
- Login should redirect User to Dashboard
- Redirect User to Dashboard page if attempting to visit the Login page while authenticated
- Review
router/index.js
formeta.auth
andbeforeEnter
usage. - Review
store/index.js
forisAuthenticated
state andlogin
andlogout
actions. - Review
components/Navigation.vue
forv-show
usage to show/hide navigation links based on authentication status.
Follow along at rebz.me for more Vue 3 examples, videos, tutorials.