mirror of
https://github.com/bartvdbraak/omnidash.git
synced 2025-06-29 12:49:12 +00:00
chore: move structure to root
This commit is contained in:
parent
3b27d3841b
commit
eed9c4161f
213 changed files with 1 additions and 38 deletions
36
src/hooks.server.ts
Normal file
36
src/hooks.server.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import { type Handle } from '@sveltejs/kit';
|
||||
import PocketBase from 'pocketbase';
|
||||
import { building, dev } from '$app/environment';
|
||||
import { SERVER_PB } from '$env/static/private';
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
event.locals.id = '';
|
||||
event.locals.email = '';
|
||||
event.locals.pocketBase = new PocketBase(SERVER_PB);
|
||||
|
||||
const isAuth: boolean = event.url.pathname === '/auth';
|
||||
if (isAuth || building) {
|
||||
event.cookies.set('pb_auth', '', { path: '/' });
|
||||
return await resolve(event);
|
||||
}
|
||||
|
||||
const pb_auth = event.request.headers.get('cookie') ?? '';
|
||||
event.locals.pocketBase.authStore.loadFromCookie(pb_auth);
|
||||
try {
|
||||
const auth = await event.locals.pocketBase
|
||||
.collection('users')
|
||||
.authRefresh<{ id: string; email: string }>();
|
||||
event.locals.id = auth.record.id;
|
||||
event.locals.email = auth.record.email;
|
||||
} catch (_) {
|
||||
event.locals.pocketBase.authStore.clear();
|
||||
}
|
||||
|
||||
const response = await resolve(event);
|
||||
const cookie = event.locals.pocketBase.authStore.exportToCookie({
|
||||
secure: !dev,
|
||||
sameSite: 'lax'
|
||||
});
|
||||
response.headers.append('set-cookie', cookie);
|
||||
return response;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue