@omerlo/omerlo-webkit 0.0.52 → 0.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { error } from '@sveltejs/kit';
|
|
2
2
|
import { env } from '$env/dynamic/private';
|
|
3
3
|
import { ApiError } from '../utils/api';
|
|
4
|
-
import { clearAuthorizationCookies, clearAuthorizationUsingHeader, getAccessTokenFromCookie, getApplicationToken, getRefreshTokenFromCookie, setAuthorizationCookies } from './utils';
|
|
4
|
+
import { clearAuthorizationCookies, clearAuthorizationUsingHeader, getAccessTokenFromCookie, getApplicationToken, getRefreshTokenFromCookie, setAuthorizationCookies, refreshAuthorizationUsingHeader } from './utils';
|
|
5
5
|
import { refresh } from './token';
|
|
6
6
|
// NOTE: inspired by https://sami.website/blog/sveltekit-api-reverse-proxy
|
|
7
7
|
const handleApiProxy = async ({ event, ...tail }) => {
|
|
@@ -28,6 +28,7 @@ const handleApiProxy = async ({ event, ...tail }) => {
|
|
|
28
28
|
event.locals.accessToken = undefined;
|
|
29
29
|
resp = await handleApiProxy({ event, ...tail });
|
|
30
30
|
}
|
|
31
|
+
refreshAuthorizationUsingHeader(headers, event.locals.refreshedToken);
|
|
31
32
|
const responseOpts = {
|
|
32
33
|
headers: headers,
|
|
33
34
|
status: resp.status,
|
|
@@ -87,6 +88,7 @@ export const handleUserToken = async ({ event, resolve }) => {
|
|
|
87
88
|
const token = await refresh(refreshToken);
|
|
88
89
|
setAuthorizationCookies(event.cookies, token);
|
|
89
90
|
event.locals.accessToken = token.accessToken;
|
|
91
|
+
event.locals.refreshedToken = token;
|
|
90
92
|
}
|
|
91
93
|
catch (err) {
|
|
92
94
|
if (err instanceof ApiError && err.status == 401) {
|
|
@@ -6,6 +6,7 @@ export declare function isAuthenticated(cookies: Cookies): boolean;
|
|
|
6
6
|
export declare function setAuthorizationCookies(cookies: Cookies, token: OmerloToken): void;
|
|
7
7
|
export declare function clearAuthorizationCookies(cookies: Cookies): void;
|
|
8
8
|
export declare function clearAuthorizationUsingHeader(headers: Headers): void;
|
|
9
|
+
export declare function refreshAuthorizationUsingHeader(headers: Headers, token?: OmerloToken): void;
|
|
9
10
|
export declare function getAccessTokenFromCookie(cookies: Cookies): string | null;
|
|
10
11
|
export declare function getRefreshTokenFromCookie(cookies: Cookies): string | null;
|
|
11
12
|
/**
|
|
@@ -45,6 +45,13 @@ export function clearAuthorizationUsingHeader(headers) {
|
|
|
45
45
|
headers.append('Set-Cookie', `logged_in=; Path=/; Secure; SameSite=Lax; Max-Age=0`);
|
|
46
46
|
headers.append('x-logout', `true`);
|
|
47
47
|
}
|
|
48
|
+
export function refreshAuthorizationUsingHeader(headers, token) {
|
|
49
|
+
if (!token)
|
|
50
|
+
return;
|
|
51
|
+
headers.append('Set-Cookie', `logged_in=true; Path=/; SameSite=Lax; Max-Age=${THREE_MONTH}`);
|
|
52
|
+
headers.append('Set-Cookie', `${accessTokenCookieName}=${token.accessToken}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${token.expiresIn - 60}`);
|
|
53
|
+
headers.append('Set-Cookie', `${refreshTokenCookieName}=${token.refreshToken}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${THREE_MONTH}`);
|
|
54
|
+
}
|
|
48
55
|
export function getAccessTokenFromCookie(cookies) {
|
|
49
56
|
return cookies.get(accessTokenCookieName) || null;
|
|
50
57
|
}
|
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
import { initAssocs, parseAssocs } from './assocs';
|
|
2
2
|
export async function parseApiResponse(response, parser) {
|
|
3
|
-
const
|
|
4
|
-
let data = null, meta = null;
|
|
3
|
+
const text = await response.text();
|
|
5
4
|
if (response.ok) {
|
|
5
|
+
const payload = JSON.parse(text);
|
|
6
6
|
let assocs = initAssocs(payload.assocs);
|
|
7
7
|
assocs = parseAssocs(assocs);
|
|
8
|
-
meta = payload.meta;
|
|
9
|
-
data = parser(payload.data, assocs);
|
|
8
|
+
const meta = payload.meta;
|
|
9
|
+
const data = parser(payload.data, assocs);
|
|
10
|
+
const errors = payload.errors || [];
|
|
11
|
+
return { ok: true, status: response.status, parser, meta, data, errors };
|
|
10
12
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
// Response not OK - try to parse JSON for API errors, fallback to generic error
|
|
14
|
+
let errors = [];
|
|
15
|
+
try {
|
|
16
|
+
const payload = JSON.parse(text);
|
|
17
|
+
errors = payload.errors || [];
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
console.error('[OmerloWebkit - parseApiResponse] Non-OK response with non-JSON body', {
|
|
21
|
+
url: response.url,
|
|
22
|
+
status: response.status,
|
|
23
|
+
statusText: response.statusText,
|
|
24
|
+
contentType: response.headers.get('content-type'),
|
|
25
|
+
body: text.slice(0, 1000)
|
|
26
|
+
});
|
|
27
|
+
errors = [{ message: `${response.status} ${response.statusText}`, body: text.slice(0, 500) }];
|
|
28
|
+
}
|
|
29
|
+
return { ok: false, status: response.status, parser, meta: null, data: null, errors };
|
|
13
30
|
}
|
|
14
31
|
export function parseMany(parser) {
|
|
15
32
|
return (response, assocs) => {
|