// auth.js — a tiny shared auth state module import { ref } from 'vue'; export const isLoggedIn = ref(false); // router/index.js import { createRouter, createWebHistory } from 'vue-router'; import Dashboard from '../views/Dashboard.vue'; import Login from '../views/Login.vue'; import { isLoggedIn } from '../auth'; const routes = [ { path: '/dashboard', component: Dashboard }, { path: '/login', component: Login }, ]; export const router = createRouter({ history: createWebHistory(), routes }); // Global navigation guard router.beforeEach((to, from) => { if (to.path === '/dashboard' && !isLoggedIn.value) { return '/login'; // redirect } // returning nothing (undefined) allows the navigation }); Logged in: {{ isLoggedIn }} Toggle login state Try the dashboard
Logged in: {{ isLoggedIn }}