-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhooks.server.ts
More file actions
26 lines (22 loc) · 876 Bytes
/
hooks.server.ts
File metadata and controls
26 lines (22 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { AUTH_COOKIE_ERASE_OPTIONS, AUTH_COOKIE_NAME } from '$lib/constants/auth';
import { ENV } from '$lib/constants/env';
import { HEADER_NAMES } from '$lib/constants/headers';
import type { Handle, HandleFetch } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
// erase token cookie
if (event.url.pathname === '/logout') {
event.cookies.set(AUTH_COOKIE_NAME, String(), AUTH_COOKIE_ERASE_OPTIONS);
event.locals.accessToken = undefined;
event.locals.user = undefined;
}
return resolve(event);
};
export const handleFetch: HandleFetch = async ({ event, request, fetch }): Promise<Response> => {
if (request.url.startsWith(ENV.GITHUB_URL)) {
const token = event.locals.accessToken;
if (token) {
request.headers.set(HEADER_NAMES.AUTHORIZATION, `Bearer ${token}`);
}
}
return fetch(request);
};