28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
export default defineNuxtRouteMiddleware((to) => {
|
|
const { isAuthenticated } = useAuth()
|
|
|
|
// Pages that are accessible only when NOT logged in (Auth pages)
|
|
const authPages = [
|
|
'/auth/login',
|
|
'/auth/register',
|
|
'/auth/forgot-password',
|
|
'/auth/reset-password'
|
|
]
|
|
|
|
// Pages that are accessible as public landing
|
|
// Note: /courses and /discovery (now in browse/) might be public depending on logic,
|
|
// but let's assume browse pages are public or handled separately.
|
|
// For now, we list the root.
|
|
const publicPages = ['/', '/courses', '/browse', '/browse/discovery']
|
|
|
|
// 1. If user is authenticated and tries to access login/register (Keep landing page accessible)
|
|
if (isAuthenticated.value && authPages.includes(to.path)) {
|
|
return navigateTo('/dashboard', { replace: true })
|
|
}
|
|
|
|
// 2. If user is NOT authenticated and tries to access a page that has this middleware applied
|
|
// and is NOT one of the public or auth pages.
|
|
if (!isAuthenticated.value && !authPages.includes(to.path) && !publicPages.includes(to.path)) {
|
|
return navigateTo('/auth/login', { replace: true })
|
|
}
|
|
})
|