1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-01 08:31:59 +00:00
immich/web/src/hooks.ts
2022-07-16 10:54:00 -05:00

51 lines
1.1 KiB
TypeScript

import type { GetSession, Handle } from '@sveltejs/kit';
import * as cookie from 'cookie';
import { api } from '@api';
export const handle: Handle = async ({ event, resolve }) => {
const cookies = cookie.parse(event.request.headers.get('cookie') || '');
if (!cookies.session) {
return await resolve(event);
}
try {
const { email, isAdmin, firstName, lastName, id, accessToken } = JSON.parse(cookies.session);
api.setAccessToken(accessToken);
const { status } = await api.authenticationApi.validateAccessToken();
if (status === 201) {
event.locals.user = {
id,
accessToken,
firstName,
lastName,
isAdmin,
email
};
}
const response = await resolve(event);
return response;
} catch (error) {
console.log('Error [handle]');
return await resolve(event);
}
};
export const getSession: GetSession = async ({ locals }) => {
if (!locals.user) return {};
return {
user: {
id: locals.user.id,
accessToken: locals.user.accessToken,
firstName: locals.user.firstName,
lastName: locals.user.lastName,
isAdmin: locals.user.isAdmin,
email: locals.user.email
}
};
};