Secure API endpoints with requireBookingManager authorization check Update confirmation page to prompt for login if unauthorized Add safe redirect handling to login and guest middleware
21 lines
558 B
TypeScript
21 lines
558 B
TypeScript
import { getDefaultAuthenticatedPath } from '~~/shared/auth'
|
|
|
|
function getSafeRedirectPath(value: unknown) {
|
|
const redirect = Array.isArray(value) ? value[0] : value
|
|
|
|
return typeof redirect === 'string' && redirect.startsWith('/') && !redirect.startsWith('//')
|
|
? redirect
|
|
: null
|
|
}
|
|
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
|
const auth = useAuth()
|
|
await auth.fetchSession()
|
|
|
|
if (!auth.user.value) {
|
|
return
|
|
}
|
|
|
|
return navigateTo(getSafeRedirectPath(to.query.redirect) || getDefaultAuthenticatedPath(auth.user.value))
|
|
})
|