This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
hooks.js
61 lines (47 loc) · 1.92 KB
/
hooks.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { parseIdentityCookies } from '$lib/utils/cookies';
import { parseJwt } from '$lib/utils/helpers';
// ==============================================================================
// HANDLE
// ==============================================================================
export async function handle({ request, resolve }) {
// console.log('=============================================================');
// console.log(new Date().toISOString(), ': HOOKS handle');
// parse jwt from cookie in request, if present, and populate locals.user
const { jwt } = parseIdentityCookies(request);
if (jwt) {
request.locals.token = jwt;
request.locals.user = parseJwt(jwt);
}
if (request.locals.user) {
request.locals.user.authenticated = true;
} else {
request.locals.user = {};
request.locals.user.authenticated = false;
}
// console.log(new Date().toISOString(), ': HOOKS handle jwt :', jwt);
// console.log(new Date().toISOString(), ': HOOKS handle locals.user.authenticated :', request.locals.user.authenticated);
// process requested route/endpoint
const response = await resolve(request);
return {
...response,
headers: {
...response.headers,
// 'x-custom-header': 'potato',
}
};
}
// ==============================================================================
// GETSESSION
// ==============================================================================
export function getSession(request) {
// console.log('-------------------------------------------------------------');
// console.log(new Date().toISOString(), ': HOOKS getSession');
// console.log(new Date().toISOString(), ': HOOKS getSession locals.user.authenticated :', request.locals.user.authenticated);
return {
user: {
authenticated: request.locals.user.authenticated || false,
authExpires: request.locals.user.exp || null,
email: request.locals.user.email || null,
}
};
}