@logto/sveltekit 0.2.10 → 0.3.1
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.
- package/lib/index.d.ts +17 -5
- package/lib/index.js +36 -21
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import LogtoClient, { type LogtoConfig, type
|
|
1
|
+
import LogtoClient, { type CookieConfig, type LogtoConfig, type PersistKey, type Storage } from '@logto/node';
|
|
2
2
|
import { type Handle, type RequestEvent } from '@sveltejs/kit';
|
|
3
|
-
export type { AccessTokenClaims,
|
|
4
|
-
export {
|
|
3
|
+
export type { AccessTokenClaims, ClientAdapter, CookieConfig, IdTokenClaims, InteractionMode, JwtVerifier, LogtoClientErrorCode, LogtoConfig, LogtoErrorCode, Storage, StorageKey, UserInfoResponse, } from '@logto/node';
|
|
4
|
+
export { CookieStorage, default as LogtoClient, LogtoClientError, LogtoError, LogtoRequestError, OidcError, PersistKey, Prompt, ReservedResource, ReservedScope, StandardLogtoClient, UserScope, buildOrganizationUrn, getOrganizationIdFromUrn, organizationUrnPrefix, } from '@logto/node';
|
|
5
5
|
export type HookConfig = {
|
|
6
6
|
/**
|
|
7
7
|
* The error response factory when an error occurs during the callback. If not provided, a 400
|
|
@@ -10,6 +10,13 @@ export type HookConfig = {
|
|
|
10
10
|
* @param error The error that occurred.
|
|
11
11
|
*/
|
|
12
12
|
onCallbackError?: (error: unknown) => Response;
|
|
13
|
+
/**
|
|
14
|
+
* The error response factory when an error occurs during fetching user info or parsing the IdToken.
|
|
15
|
+
* If not provided, a 500 response will be returned.
|
|
16
|
+
*
|
|
17
|
+
* @param error
|
|
18
|
+
*/
|
|
19
|
+
onGetUserInfoError?: (error: unknown) => Response;
|
|
13
20
|
/**
|
|
14
21
|
* The path to the callback handler. Default to `/callback`.
|
|
15
22
|
*/
|
|
@@ -23,6 +30,11 @@ export type HookConfig = {
|
|
|
23
30
|
* created.
|
|
24
31
|
*/
|
|
25
32
|
buildLogtoClient?: (event: RequestEvent) => LogtoClient;
|
|
33
|
+
/**
|
|
34
|
+
* The custom persistent storage instance parsed to the `LogtoClient`. It will be used to store the session and tokens.
|
|
35
|
+
* If not provided, a default `CookieStorage` instance will be created.
|
|
36
|
+
*/
|
|
37
|
+
customStorage?: Storage<PersistKey>;
|
|
26
38
|
};
|
|
27
39
|
/**
|
|
28
40
|
* The factory to create a SvelteKit hook to handle Logto authentication. The hook will
|
|
@@ -63,8 +75,8 @@ export type HookConfig = {
|
|
|
63
75
|
* ```
|
|
64
76
|
*
|
|
65
77
|
* @param config The Logto configuration.
|
|
66
|
-
* @param cookieConfig The configuration object for the cookie storage.
|
|
78
|
+
* @param cookieConfig The configuration object for the cookie storage. Required if no custom storage is provided.
|
|
67
79
|
* @param hookConfig The configuration object for the hook itself.
|
|
68
80
|
* @returns The SvelteKit hook.
|
|
69
81
|
*/
|
|
70
|
-
export declare const handleLogto: (config: LogtoConfig, cookieConfig
|
|
82
|
+
export declare const handleLogto: (config: LogtoConfig, cookieConfig?: Pick<CookieConfig, 'cookieKey' | 'encryptionKey'>, hookConfig?: HookConfig) => Handle;
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import LogtoClient, { CookieStorage } from '@logto/node';
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
1
|
+
import LogtoClient, { CookieStorage, } from '@logto/node';
|
|
2
|
+
import { isRedirect, redirect } from '@sveltejs/kit';
|
|
3
|
+
export { CookieStorage, default as LogtoClient, LogtoClientError, LogtoError, LogtoRequestError, OidcError, PersistKey, Prompt, ReservedResource, ReservedScope, StandardLogtoClient, UserScope, buildOrganizationUrn, getOrganizationIdFromUrn, organizationUrnPrefix, } from '@logto/node';
|
|
4
4
|
/**
|
|
5
5
|
* The factory to create a SvelteKit hook to handle Logto authentication. The hook will
|
|
6
6
|
* initialize the `LogtoClient` instance and add it to the `locals` of the request event. It will
|
|
@@ -40,26 +40,20 @@ export { LogtoError, LogtoRequestError, LogtoClientError, OidcError, Prompt, Res
|
|
|
40
40
|
* ```
|
|
41
41
|
*
|
|
42
42
|
* @param config The Logto configuration.
|
|
43
|
-
* @param cookieConfig The configuration object for the cookie storage.
|
|
43
|
+
* @param cookieConfig The configuration object for the cookie storage. Required if no custom storage is provided.
|
|
44
44
|
* @param hookConfig The configuration object for the hook itself.
|
|
45
45
|
* @returns The SvelteKit hook.
|
|
46
46
|
*/
|
|
47
47
|
export const handleLogto = (config, cookieConfig, hookConfig) => {
|
|
48
|
-
const { signInCallback = '/callback', onCallbackError, fetchUserInfo = false, buildLogtoClient, } = hookConfig ?? {};
|
|
48
|
+
const { signInCallback = '/callback', onCallbackError, onGetUserInfoError, fetchUserInfo = false, buildLogtoClient, } = hookConfig ?? {};
|
|
49
|
+
// eslint-disable-next-line complexity
|
|
49
50
|
return async ({ resolve, event }) => {
|
|
50
51
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- sanity check
|
|
51
52
|
if (event.locals.logtoClient) {
|
|
52
53
|
console.warn('`logtoClient` already exists in `locals`, you probably have added the `handleLogto` hook more than once. Skipping.');
|
|
53
54
|
return resolve(event);
|
|
54
55
|
}
|
|
55
|
-
const storage =
|
|
56
|
-
setCookie: (...args) => {
|
|
57
|
-
event.cookies.set(...args);
|
|
58
|
-
},
|
|
59
|
-
getCookie: (...args) => event.cookies.get(...args),
|
|
60
|
-
...cookieConfig,
|
|
61
|
-
}, event.request);
|
|
62
|
-
await storage.init();
|
|
56
|
+
const storage = hookConfig?.customStorage ?? (await buildCookieStorageFromEvent(event, cookieConfig));
|
|
63
57
|
const logtoClient = buildLogtoClient?.(event) ??
|
|
64
58
|
new LogtoClient(config, {
|
|
65
59
|
navigate: (url) => {
|
|
@@ -78,19 +72,40 @@ export const handleLogto = (config, cookieConfig, hookConfig) => {
|
|
|
78
72
|
// eslint-disable-next-line @typescript-eslint/no-throw-literal -- SvelteKit's convention
|
|
79
73
|
throw error;
|
|
80
74
|
}
|
|
81
|
-
return
|
|
82
|
-
new Response(`Error: ${error instanceof Error ? error.message : JSON.stringify(error, undefined, 2)}`, {
|
|
83
|
-
status: 400,
|
|
84
|
-
}));
|
|
75
|
+
return onCallbackError?.(error) ?? defaultErrorHandler(error, 400);
|
|
85
76
|
}
|
|
86
77
|
return redirect(302, '/');
|
|
87
78
|
}
|
|
88
79
|
if (await logtoClient.isAuthenticated()) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
80
|
+
try {
|
|
81
|
+
// eslint-disable-next-line @silverhand/fp/no-mutation
|
|
82
|
+
event.locals.user = await (fetchUserInfo
|
|
83
|
+
? logtoClient.fetchUserInfo()
|
|
84
|
+
: logtoClient.getIdTokenClaims());
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
return onGetUserInfoError?.(error) ?? defaultErrorHandler(error);
|
|
88
|
+
}
|
|
93
89
|
}
|
|
94
90
|
return resolve(event);
|
|
95
91
|
};
|
|
96
92
|
};
|
|
93
|
+
const defaultErrorHandler = (error, status = 500) => {
|
|
94
|
+
return new Response(`Error: ${error instanceof Error ? error.message : JSON.stringify(error, undefined, 2)}`, {
|
|
95
|
+
status,
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const buildCookieStorageFromEvent = async (event, cookieConfig) => {
|
|
99
|
+
if (!cookieConfig) {
|
|
100
|
+
throw new Error('Missing cookie configuration for the CookieStorage.');
|
|
101
|
+
}
|
|
102
|
+
const storage = new CookieStorage({
|
|
103
|
+
setCookie: (...args) => {
|
|
104
|
+
event.cookies.set(...args);
|
|
105
|
+
},
|
|
106
|
+
getCookie: (...args) => event.cookies.get(...args),
|
|
107
|
+
...cookieConfig,
|
|
108
|
+
}, event.request);
|
|
109
|
+
await storage.init();
|
|
110
|
+
return storage;
|
|
111
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/sveltekit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib/index.js",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@sveltejs/kit": "^2.0.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@logto/node": "^2.5.
|
|
49
|
+
"@logto/node": "^2.5.2"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|