2022-07-26 19:28:07 +02:00
|
|
|
import type { ExternalFetch, GetSession, Handle } from '@sveltejs/kit';
|
2022-05-21 09:23:55 +02:00
|
|
|
import * as cookie from 'cookie';
|
2022-08-08 01:36:34 +02:00
|
|
|
import { serverApi } from '@api';
|
2022-05-21 09:23:55 +02:00
|
|
|
|
2022-06-11 23:12:06 +02:00
|
|
|
export const handle: Handle = async ({ event, resolve }) => {
|
|
|
|
const cookies = cookie.parse(event.request.headers.get('cookie') || '');
|
|
|
|
|
2022-07-26 19:28:07 +02:00
|
|
|
if (!cookies['immich_is_authenticated']) {
|
2022-06-11 23:12:06 +02:00
|
|
|
return await resolve(event);
|
|
|
|
}
|
2022-07-26 19:28:07 +02:00
|
|
|
const accessToken = cookies['immich_access_token'];
|
2022-06-11 23:12:06 +02:00
|
|
|
|
|
|
|
try {
|
2022-08-07 01:14:54 +02:00
|
|
|
serverApi.setAccessToken(accessToken);
|
|
|
|
const { data } = await serverApi.userApi.getMyUserInfo();
|
2022-07-26 19:28:07 +02:00
|
|
|
event.locals.user = data;
|
2022-06-11 23:12:06 +02:00
|
|
|
|
2022-07-26 19:28:07 +02:00
|
|
|
return await resolve(event);
|
2022-06-11 23:12:06 +02:00
|
|
|
} catch (error) {
|
2022-07-26 19:28:07 +02:00
|
|
|
event.locals.user = undefined;
|
2022-06-11 23:12:06 +02:00
|
|
|
return await resolve(event);
|
|
|
|
}
|
2022-05-21 09:23:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getSession: GetSession = async ({ locals }) => {
|
2022-06-11 23:12:06 +02:00
|
|
|
if (!locals.user) return {};
|
|
|
|
|
|
|
|
return {
|
2022-07-26 19:28:07 +02:00
|
|
|
user: locals.user
|
2022-06-11 23:12:06 +02:00
|
|
|
};
|
|
|
|
};
|