You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
572 B
28 lines
572 B
// middleware/auth.ts
|
|
|
|
/**
|
|
* Authentication middleware
|
|
*
|
|
* Protects routes from unauthenticated access
|
|
*
|
|
* Usage in pages:
|
|
*
|
|
* definePageMeta({
|
|
* middleware: 'auth'
|
|
* })
|
|
*/
|
|
|
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
|
const { loggedIn } = useUserSession()
|
|
|
|
// Not logged in - redirect to auth page
|
|
if (!loggedIn.value) {
|
|
// Store intended destination for post-login redirect
|
|
useCookie('redirect_after_login', {
|
|
maxAge: 600, // 10 minutes
|
|
path: '/',
|
|
}).value = to.fullPath
|
|
|
|
return navigateTo('/auth')
|
|
}
|
|
})
|
|
|