feat: add auth guards and server-side logout with postcode search integration
- Add navigation guard requiring authentication for dashboard routes - Create
This commit is contained in:
@@ -8,12 +8,23 @@ import SettingsLayout from '../views/dashboard/settings/SettingsLayout.vue'
|
||||
import Profile from '../views/dashboard/settings/Profile.vue'
|
||||
import Security from '../views/dashboard/settings/Security.vue'
|
||||
import Appearance from '../views/dashboard/settings/Appearance.vue'
|
||||
import { useAuth } from '../composables/useAuth.js'
|
||||
|
||||
const routes = [
|
||||
{ path: '/', component: Home, name: 'home' },
|
||||
{
|
||||
path: '/logout',
|
||||
name: 'logout',
|
||||
component: { render: () => null },
|
||||
beforeEnter: () => {
|
||||
window.location.href = '/logout'
|
||||
return false
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/dashboard',
|
||||
component: DashboardLayout,
|
||||
meta: { requiresAuth: true },
|
||||
children: [
|
||||
{ path: '', component: Overview, name: 'dashboard' },
|
||||
{ path: 'saved-stations', component: SavedStations, name: 'dashboard.saved-stations' },
|
||||
@@ -32,7 +43,20 @@ const routes = [
|
||||
},
|
||||
]
|
||||
|
||||
export default createRouter({
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes,
|
||||
})
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
if (to.meta.requiresAuth) {
|
||||
const { isAuthenticated, fetchUser } = useAuth()
|
||||
await fetchUser()
|
||||
if (!isAuthenticated.value) {
|
||||
window.location.href = '/login'
|
||||
return false
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user