mirror of https://github.com/TZGyn/shortener
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import { lucia } from '$lib/server/auth'
|
|
import { redirect, type Handle } from '@sveltejs/kit'
|
|
|
|
export const handle: Handle = async ({ event, resolve }) => {
|
|
const sessionId = event.cookies.get(lucia.sessionCookieName)
|
|
|
|
const pathname = event.url.pathname
|
|
|
|
if (pathname.startsWith('/landing')) {
|
|
const response = await resolve(event)
|
|
|
|
return response
|
|
}
|
|
|
|
const allowedPath = ['/login', '/signup']
|
|
|
|
if (allowedPath.includes(pathname)) {
|
|
if (sessionId) {
|
|
redirect(303, '/')
|
|
}
|
|
event.locals.session = null
|
|
const response = await resolve(event)
|
|
|
|
return response
|
|
}
|
|
|
|
if (!sessionId) {
|
|
redirect(303, '/login')
|
|
}
|
|
const { session, user } = await lucia.validateSession(sessionId)
|
|
|
|
if (!user) {
|
|
redirect(303, '/login')
|
|
}
|
|
|
|
if (session && session.fresh) {
|
|
const sessionCookie = lucia.createSessionCookie(session.id)
|
|
// sveltekit types deviates from the de-facto standard
|
|
// you can use 'as any' too
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
path: '.',
|
|
...sessionCookie.attributes,
|
|
})
|
|
}
|
|
if (!session) {
|
|
const sessionCookie = lucia.createBlankSessionCookie()
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
path: '/',
|
|
...sessionCookie.attributes,
|
|
})
|
|
}
|
|
|
|
event.locals.user = user
|
|
event.locals.session = session
|
|
|
|
const response = await resolve(event)
|
|
|
|
return response
|
|
}
|