@nekm/svelte-core 1.7.3 → 1.7.5

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,4 +1,9 @@
1
1
  import type { Handle, RequestEvent } from "@sveltejs/kit";
2
+ export interface ProtectTokens<TAuthToken = unknown, TIdToken = unknown, TAccessToken = unknown> {
3
+ readonly auth: TAuthToken;
4
+ readonly id: TIdToken;
5
+ readonly access: TAccessToken;
6
+ }
2
7
  export interface ProtectConfig<TAuthToken = unknown, TIdToken = unknown, TAccessToken = unknown> {
3
8
  readonly protect?: (event: RequestEvent) => Promise<boolean> | boolean;
4
9
  readonly jwtDecodeAndVerifyIdToken: (token: string) => Promise<TIdToken> | TIdToken;
@@ -9,8 +14,8 @@ export interface ProtectConfig<TAuthToken = unknown, TIdToken = unknown, TAccess
9
14
  readonly stateGet: (event: RequestEvent) => Promise<string> | string;
10
15
  };
11
16
  readonly hooks?: {
12
- readonly onLogout?: () => Promise<void> | void;
13
- readonly onLogin?: (authToken: TAuthToken, idToken: TIdToken, accessToken: TAccessToken) => Promise<void> | void;
17
+ readonly onLogout?: (event: RequestEvent) => Promise<void> | void;
18
+ readonly onLogin?: (event: RequestEvent, tokens: ProtectTokens<TAuthToken, TIdToken, TAccessToken>) => Promise<void> | void;
14
19
  };
15
20
  readonly oauth: {
16
21
  readonly clientId: string;
@@ -8,7 +8,7 @@ export const routeLoginFactory = (config) => {
8
8
  return {
9
9
  path: ROUTE_PATH_LOGIN,
10
10
  async handle({ event }) {
11
- const state = await config.session.stateGenerate();
11
+ const state = await config.session.stateGenerate(event);
12
12
  const params = queryParamsCreate({
13
13
  client_id: config.oauth.clientId,
14
14
  response_type: "code",
@@ -26,18 +26,18 @@ export const routeRedirectLoginFactory = (config) => {
26
26
  async handle({ event }) {
27
27
  const state = event.url.searchParams.get("state") ?? undefined;
28
28
  ;
29
- const stateSession = await config.session.stateGet();
29
+ const stateSession = await config.session.stateGet(event);
30
30
  if (state !== stateSession) {
31
31
  throw new Error("State do not match");
32
32
  }
33
33
  const code = event.url.searchParams.get("code") ?? undefined;
34
34
  throwIfUndefined(code);
35
- const authToken = await exchangeCodeForToken(fetch, event.url.origin, code);
35
+ const auth = await exchangeCodeForToken(fetch, event.url.origin, code);
36
36
  // @ts-expect-error Ignore, for now
37
- const idToken = await config.jwtDecodeAndVerifyIdToken(authToken.id_token);
37
+ const id = await config.jwtDecodeAndVerifyIdToken(auth.id_token);
38
38
  // @ts-expect-error Ignore, for now
39
- const accessToken = await config.jwtDecodeAndVerifyAccessToken(authToken.access_token);
40
- await onLogin(authToken, idToken, accessToken);
39
+ const access = await config.jwtDecodeAndVerifyAccessToken(auth.access_token);
40
+ await onLogin(event, { auth, id, access });
41
41
  throw redirect(302, "/");
42
42
  }
43
43
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@nekm/svelte-core",
3
3
  "description": "Code I find myself re-writing over-and-over, Svelte edition.",
4
4
  "license": "MIT",
5
- "version": "1.7.3",
5
+ "version": "1.7.5",
6
6
  "scripts": {
7
7
  "dev": "vite dev",
8
8
  "build": "vite build && npm run prepack",