mirror of
https://github.com/wireadmin/wireadmin
synced 2025-02-02 19:14:59 +00:00
26 lines
759 B
TypeScript
26 lines
759 B
TypeScript
import type { Handle } from '@sveltejs/kit';
|
|
import { verifyToken } from '$lib/auth';
|
|
import 'dotenv/config';
|
|
|
|
export const handle: Handle = async ({ event, resolve }) => {
|
|
if (!AUTH_EXCEPTION.includes(event.url.pathname)) {
|
|
const token = event.cookies.get('authorization');
|
|
const token_valid = await verifyToken(token ?? '');
|
|
|
|
const redirect = new Response(null, { status: 302, headers: { location: '/login' } });
|
|
const is_login_page = event.url.pathname === '/login';
|
|
|
|
if (!token_valid && !is_login_page) {
|
|
return redirect;
|
|
}
|
|
|
|
if (token_valid && is_login_page) {
|
|
return new Response(null, { status: 302, headers: { location: '/' } });
|
|
}
|
|
}
|
|
|
|
return resolve(event);
|
|
};
|
|
|
|
const AUTH_EXCEPTION = ['/api/health'];
|