@strivacity/sdk-svelte 3.0.2 → 4.0.0-beta.0
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/CHANGELOG.md +6 -0
- package/README.md +1714 -428
- package/dist/assets/rolldown-runtime.cjs +1 -0
- package/dist/client/components.svelte.cjs +2 -0
- package/dist/client/components.svelte.cjs.map +1 -0
- package/dist/client/components.svelte.d.ts +11 -0
- package/dist/client/components.svelte.mjs +2 -0
- package/dist/client/components.svelte.mjs.map +1 -0
- package/dist/client/hooks.svelte.cjs +2 -0
- package/dist/client/hooks.svelte.cjs.map +1 -0
- package/dist/client/hooks.svelte.d.ts +27 -0
- package/dist/client/hooks.svelte.mjs +2 -0
- package/dist/client/hooks.svelte.mjs.map +1 -0
- package/dist/client/index.cjs +1 -0
- package/dist/client/index.d.ts +8 -0
- package/dist/client/index.mjs +1 -0
- package/dist/client/storages.cjs +1 -0
- package/dist/client/storages.d.ts +1 -0
- package/dist/client/storages.mjs +1 -0
- package/dist/client/types.cjs +1 -0
- package/dist/client/types.d.ts +238 -0
- package/dist/client/types.mjs +1 -0
- package/dist/client/utils.cjs +1 -0
- package/dist/client/utils.d.ts +1 -0
- package/dist/client/utils.mjs +1 -0
- package/dist/errors.cjs +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.mjs +1 -0
- package/dist/server/index.cjs +1 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.mjs +1 -0
- package/dist/server/sdk.cjs +2 -0
- package/dist/server/sdk.cjs.map +1 -0
- package/dist/server/sdk.d.ts +8 -0
- package/dist/server/sdk.mjs +2 -0
- package/dist/server/sdk.mjs.map +1 -0
- package/dist/server/storages.cjs +2 -0
- package/dist/server/storages.cjs.map +1 -0
- package/dist/server/storages.d.ts +10 -0
- package/dist/server/storages.mjs +2 -0
- package/dist/server/storages.mjs.map +1 -0
- package/dist/server/types.cjs +1 -0
- package/dist/server/types.d.ts +35 -0
- package/dist/server/types.mjs +1 -0
- package/dist/server/utils.cjs +1 -0
- package/dist/server/utils.d.ts +1 -0
- package/dist/server/utils.mjs +1 -0
- package/dist/types.cjs +1 -2
- package/dist/types.d.ts +2 -181
- package/dist/types.mjs +1 -2
- package/eslint.config.mjs +3 -0
- package/package.json +59 -11
- package/project.json +47 -0
- package/src/client/components.svelte.ts +125 -0
- package/src/client/hooks.svelte.ts +221 -0
- package/src/client/index.ts +8 -0
- package/src/client/storages.ts +1 -0
- package/src/client/types.ts +299 -0
- package/src/client/utils.ts +1 -0
- package/src/errors.ts +1 -0
- package/src/server/index.ts +6 -0
- package/src/server/sdk.ts +77 -0
- package/src/server/storages.ts +22 -0
- package/src/server/types.ts +49 -0
- package/src/server/utils.ts +1 -0
- package/src/types.ts +2 -0
- package/svelte.config.js +5 -0
- package/testing/fixtures/AuthProviderHost.svelte +29 -0
- package/testing/fixtures/Harness.svelte +30 -0
- package/testing/fixtures/NativeLoginConsumer.svelte +21 -0
- package/testing/fixtures/NativeLoginContextConsumer.svelte +10 -0
- package/testing/fixtures/StrivacityConsumer.svelte +10 -0
- package/testing/tests/components.spec.ts +183 -0
- package/testing/tests/errors.spec.ts +10 -0
- package/testing/tests/hooks.spec.ts +275 -0
- package/testing/tests/index.spec.ts +199 -0
- package/testing/tests/server/sdk.spec.ts +214 -0
- package/testing/tests/server/storages.spec.ts +50 -0
- package/testing/tests/server/utils.spec.ts +10 -0
- package/testing/tests/storages.spec.ts +10 -0
- package/testing/tests/utils.spec.ts +10 -0
- package/testing/utils/mount.ts +83 -0
- package/tsconfig.app.json +4 -0
- package/tsconfig.json +6 -0
- package/vite.config.mts +52 -0
- package/dist/AuthProvider.cjs +0 -2
- package/dist/AuthProvider.cjs.map +0 -1
- package/dist/AuthProvider.mjs +0 -2
- package/dist/AuthProvider.mjs.map +0 -1
- package/dist/AuthProvider.svelte.d.ts +0 -1
- package/dist/LoginRenderer.cjs +0 -2
- package/dist/LoginRenderer.cjs.map +0 -1
- package/dist/LoginRenderer.mjs +0 -2
- package/dist/LoginRenderer.mjs.map +0 -1
- package/dist/LoginRenderer.svelte.d.ts +0 -1
- package/dist/composables.cjs +0 -2
- package/dist/composables.cjs.map +0 -1
- package/dist/composables.d.ts +0 -12
- package/dist/composables.mjs +0 -2
- package/dist/composables.mjs.map +0 -1
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -13
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +0 -1
- package/dist/types.cjs.map +0 -1
- package/dist/types.mjs.map +0 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from '@strivacity/sdk-core/flows';
|
|
2
|
+
export * from '@strivacity/sdk-core/handlers';
|
|
3
|
+
export * from '@strivacity/sdk-core/utils';
|
|
4
|
+
export * from './components.svelte';
|
|
5
|
+
export * from './hooks.svelte';
|
|
6
|
+
export * from './storages';
|
|
7
|
+
export * from './types';
|
|
8
|
+
export * from './utils';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@strivacity/sdk-core/storages';
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IdTokenClaims,
|
|
3
|
+
RedirectFlow,
|
|
4
|
+
PopupFlow,
|
|
5
|
+
NativeFlow,
|
|
6
|
+
EmbeddedFlow,
|
|
7
|
+
initFlow,
|
|
8
|
+
NativeParams,
|
|
9
|
+
NativeFlowState,
|
|
10
|
+
SessionData,
|
|
11
|
+
NativeFlowMessage,
|
|
12
|
+
SDKInitConfig,
|
|
13
|
+
} from '@strivacity/sdk-core';
|
|
14
|
+
import type { FallbackError } from '@strivacity/sdk-core/utils';
|
|
15
|
+
|
|
16
|
+
export * from '@strivacity/sdk-core/types';
|
|
17
|
+
|
|
18
|
+
export type SDKContext<Flow extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow> = {
|
|
19
|
+
/**
|
|
20
|
+
* The underlying Strivacity SDK flow instance.
|
|
21
|
+
*/
|
|
22
|
+
readonly sdk: Flow;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A boolean indicating whether the SDK is still loading.
|
|
26
|
+
*/
|
|
27
|
+
readonly loading: boolean;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* BCP 47 language code representing the current language of the SDK.
|
|
31
|
+
*/
|
|
32
|
+
readonly language: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A boolean ref indicating whether the user is authenticated.
|
|
36
|
+
*/
|
|
37
|
+
readonly isAuthenticated: boolean;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The claims from the ID token, if available.
|
|
41
|
+
*/
|
|
42
|
+
readonly idTokenClaims: IdTokenClaims | null;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The access token, if available.
|
|
46
|
+
*/
|
|
47
|
+
readonly accessToken: string | null;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The refresh token, if available.
|
|
51
|
+
*/
|
|
52
|
+
readonly refreshToken: string | null;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A boolean ref indicating whether the access token has expired.
|
|
56
|
+
*/
|
|
57
|
+
readonly accessTokenExpired: boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The expiration date of the access token, if available.
|
|
61
|
+
*/
|
|
62
|
+
readonly accessTokenExpirationDate: number | null;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Subscribes to a specific SDK event.
|
|
66
|
+
*/
|
|
67
|
+
subscribeToEvent: Flow['subscribeToEvent'];
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Subscribes to all SDK events.
|
|
71
|
+
*/
|
|
72
|
+
subscribeToAllEvents: Flow['subscribeToAllEvents'];
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Checks whether the user is currently authenticated, optionally triggering a token refresh.
|
|
76
|
+
*/
|
|
77
|
+
checkAuthentication: Flow['checkAuthentication'];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Initializes the SDK, loading metadata and restoring any persisted session.
|
|
81
|
+
*/
|
|
82
|
+
init: Flow['init'];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Performs a token exchange using the provided parameters.
|
|
86
|
+
*/
|
|
87
|
+
tokenExchange: Flow['tokenExchange'];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Handles the authentication callback by parsing the response from the given URL.
|
|
91
|
+
*/
|
|
92
|
+
handleCallback: Flow['handleCallback'];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Refreshes the current access token using the refresh token.
|
|
96
|
+
*/
|
|
97
|
+
refresh: Flow['refresh'];
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Revokes the current tokens.
|
|
101
|
+
*/
|
|
102
|
+
revoke: Flow['revoke'];
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Logs the user out and optionally redirects to a post-logout URI.
|
|
106
|
+
*/
|
|
107
|
+
logout: Flow['logout'];
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Initiates the login flow.
|
|
111
|
+
*/
|
|
112
|
+
login: Flow['login'];
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Initiates the registration flow.
|
|
116
|
+
*/
|
|
117
|
+
register: Flow['register'];
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Handles the entry point URL of the authentication flow, typically used for embedded or native modes.
|
|
121
|
+
*/
|
|
122
|
+
entry: Flow['entry'];
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export type SDKInstance = ReturnType<typeof initFlow>;
|
|
126
|
+
|
|
127
|
+
export type LoginContext = {
|
|
128
|
+
/**
|
|
129
|
+
* Whether a form submission or session initialization is in progress.
|
|
130
|
+
*/
|
|
131
|
+
loading: boolean;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Current form field values, keyed by form ID then widget ID.
|
|
135
|
+
*/
|
|
136
|
+
forms: Record<string, Record<string, unknown>>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Current validation and info messages, keyed by form ID then widget ID.
|
|
140
|
+
*/
|
|
141
|
+
messages: Record<string, Record<string, NativeFlowMessage>>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The current login flow state returned by the backend.
|
|
145
|
+
*/
|
|
146
|
+
state: Partial<NativeFlowState>;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Submits the form with the given ID and advances the login flow.
|
|
150
|
+
*
|
|
151
|
+
* @param formId - The ID of the form to submit.
|
|
152
|
+
* @param customBody - Optional custom body to send with the form submission. If not provided, the current form values will be used.
|
|
153
|
+
* @returns A promise that resolves when the form submission is complete.
|
|
154
|
+
*/
|
|
155
|
+
submitForm: (formId: string, customBody?: Record<string, unknown>) => Promise<void>;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Redirects to the hosted login UI.
|
|
159
|
+
*
|
|
160
|
+
* @param message - Optional message to log before redirecting.
|
|
161
|
+
* @returns A promise that resolves when the redirect is initiated.
|
|
162
|
+
*/
|
|
163
|
+
triggerFallback: (message?: string) => void;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Signals that the login flow was closed by the user.
|
|
167
|
+
*/
|
|
168
|
+
triggerClose: () => void;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Updates a single field value within a form before submission.
|
|
172
|
+
*
|
|
173
|
+
* @param formId - The ID of the form containing the field.
|
|
174
|
+
* @param widgetId - The ID of the widget (field) to update.
|
|
175
|
+
* @param value - The new value to set for the field.
|
|
176
|
+
*/
|
|
177
|
+
setFormValue: (formId: string, widgetId: string, value: unknown) => void;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Sets a validation or info message on a specific widget.
|
|
181
|
+
*
|
|
182
|
+
* @param formId - The ID of the form containing the widget.
|
|
183
|
+
* @param widgetId - The ID of the widget to set the message for.
|
|
184
|
+
* @param value - The message to set, which can be a string or a structured NativeFlowMessage.
|
|
185
|
+
*/
|
|
186
|
+
setMessage: (formId: string, widgetId: string, value: NativeFlowMessage) => void;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export type StyAuthProviderProps = {
|
|
190
|
+
/**
|
|
191
|
+
* The options to be used by the AuthProvider. These options are passed to the SDK during initialization.
|
|
192
|
+
*/
|
|
193
|
+
options: SDKInitConfig;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* A getter returning the session data to be used by the AuthProvider.
|
|
197
|
+
*
|
|
198
|
+
* Called reactively (inside a Svelte effect) whenever the returned value changes, so it should read from a
|
|
199
|
+
* reactive source (e.g. a `$props()` field) rather than a plain snapshot. If not provided, the AuthProvider
|
|
200
|
+
* will attempt to retrieve the session data from the SDK.
|
|
201
|
+
*/
|
|
202
|
+
session?: () => SessionData | null | undefined;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The reactive context value exposed to native login flow widgets via `useNativeLoginContext()` (key: `nativeFlowContext`).
|
|
207
|
+
*/
|
|
208
|
+
export type NativeFlowContextValue = {
|
|
209
|
+
/**
|
|
210
|
+
* Whether a form submission or session initialization is in progress.
|
|
211
|
+
*/
|
|
212
|
+
loading: boolean;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Current form field values, keyed by form ID then widget ID.
|
|
216
|
+
*/
|
|
217
|
+
forms: Record<string, Record<string, unknown>>;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Current validation and info messages, keyed by form ID then widget ID.
|
|
221
|
+
*/
|
|
222
|
+
messages: Record<string, Record<string, NativeFlowMessage>>;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The current login flow state returned by the backend.
|
|
226
|
+
*/
|
|
227
|
+
state: Partial<NativeFlowState>;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Submits the form with the given ID and advances the login flow.
|
|
231
|
+
*
|
|
232
|
+
* @param formId - The ID of the form to submit.
|
|
233
|
+
* @param customBody - Optional custom body to send with the form submission. If not provided, the current form values will be used.
|
|
234
|
+
* @returns A promise that resolves when the form submission is complete.
|
|
235
|
+
*/
|
|
236
|
+
submitForm: (formId: string, customBody?: Record<string, unknown>) => Promise<void>;
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Redirects to the hosted login UI.
|
|
240
|
+
*
|
|
241
|
+
* @param message - Optional message to log before redirecting.
|
|
242
|
+
*/
|
|
243
|
+
triggerFallback: (message?: string) => void;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Signals that the login flow was closed by the user.
|
|
247
|
+
*/
|
|
248
|
+
triggerClose: () => void;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Updates a single field value within a form before submission.
|
|
252
|
+
*
|
|
253
|
+
* @param formId - The ID of the form containing the field.
|
|
254
|
+
* @param widgetId - The ID of the widget (field) to update.
|
|
255
|
+
* @param value - The new value to set for the field.
|
|
256
|
+
*/
|
|
257
|
+
setFormValue: (formId: string, widgetId: string, value: unknown) => void;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Sets a validation or info message on a specific widget.
|
|
261
|
+
*
|
|
262
|
+
* @param formId - The ID of the form containing the widget.
|
|
263
|
+
* @param widgetId - The ID of the widget to set the message for.
|
|
264
|
+
* @param value - The message to set.
|
|
265
|
+
*/
|
|
266
|
+
setMessage: (formId: string, widgetId: string, value: NativeFlowMessage) => void;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
export type UseNativeLoginOptions = {
|
|
270
|
+
/**
|
|
271
|
+
* Optional parameters to be passed to the login session request. These parameters will be sent to the `authorizationUri` endpoint.
|
|
272
|
+
*/
|
|
273
|
+
params?: NativeParams;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Called when the user successfully completes the login flow.
|
|
277
|
+
*/
|
|
278
|
+
onLogin?: (session: SessionData) => void | Promise<void>;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Called when the fallback flow is triggered, typically due to an error or unsupported environment. Receives a `FallbackError` object containing the hosted URL to redirect to.
|
|
282
|
+
*/
|
|
283
|
+
onFallback?: (error: FallbackError) => void | Promise<void>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Called when the login flow is closed by the user.
|
|
287
|
+
*/
|
|
288
|
+
onClose?: () => void | Promise<void>;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Called when an error occurs during the login session initialization or flow.
|
|
292
|
+
*/
|
|
293
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Called when a global message is received during the login flow.
|
|
297
|
+
*/
|
|
298
|
+
onGlobalMessage?: (message: NativeFlowMessage) => void | Promise<void>;
|
|
299
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@strivacity/sdk-core/utils';
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@strivacity/sdk-core/utils/errors';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { RequestEvent, Handle } from '@sveltejs/kit';
|
|
2
|
+
import type { SessionData } from '@strivacity/sdk-core/types';
|
|
3
|
+
import type { SvelteKitServerSDK, SvelteKitServerSDKInitConfig } from './types';
|
|
4
|
+
import { redirect } from '@sveltejs/kit';
|
|
5
|
+
import { isSessionExpired } from '@strivacity/sdk-core/utils';
|
|
6
|
+
import { createBaseServerSDK } from '@strivacity/sdk-core/server';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates the Strivacity server SDK for SvelteKit: session management, auth route handlers, and the `handle` hook used in `hooks.server.ts`.
|
|
10
|
+
*
|
|
11
|
+
* @param {SvelteKitServerSDKInitConfig} initConfig - The configuration options for initializing the server SDK.
|
|
12
|
+
* @returns {SvelteKitServerSDK} The initialized server SDK instance.
|
|
13
|
+
*/
|
|
14
|
+
export function createServerSDK(initConfig: SvelteKitServerSDKInitConfig): SvelteKitServerSDK {
|
|
15
|
+
const base = createBaseServerSDK<RequestEvent>(
|
|
16
|
+
{
|
|
17
|
+
toRequest: (event) => event.request,
|
|
18
|
+
},
|
|
19
|
+
initConfig,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
async function requireSession(event: RequestEvent, guardOpts: { returnTo?: string } = {}): Promise<SessionData> {
|
|
23
|
+
let session = await base.getSession(event);
|
|
24
|
+
|
|
25
|
+
if (session && isSessionExpired(session) && session.refresh_token) {
|
|
26
|
+
try {
|
|
27
|
+
session = await base.refreshSession(event);
|
|
28
|
+
} catch {
|
|
29
|
+
session = null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!session) {
|
|
34
|
+
const loginUrl = ['embedded', 'native'].includes(base.options.mode) ? base.options.loginUri : `${base.options.authUrlPrefix}/login`;
|
|
35
|
+
const uri = new URL(loginUrl, event.url.origin);
|
|
36
|
+
const returnTo = guardOpts.returnTo ?? `${event.url.pathname}${event.url.search}`;
|
|
37
|
+
|
|
38
|
+
if (returnTo) {
|
|
39
|
+
uri.searchParams.set('returnTo', returnTo);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
redirect(302, uri);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return session;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const handle: Handle = async ({ event, resolve }) => {
|
|
49
|
+
const response = await base.handler(event);
|
|
50
|
+
|
|
51
|
+
return response ?? resolve(event);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
get options() {
|
|
56
|
+
return base.options;
|
|
57
|
+
},
|
|
58
|
+
handle,
|
|
59
|
+
requireSession,
|
|
60
|
+
getSession: (event) => base.getSession(event),
|
|
61
|
+
updateSession: (session, event) => base.updateSession(session, event),
|
|
62
|
+
refreshSession: (event) => base.refreshSession(event),
|
|
63
|
+
revokeSession: (event) => base.revokeSession(event),
|
|
64
|
+
getEntrySession: (entryUrl) => base.getEntrySession(entryUrl),
|
|
65
|
+
completeLogin: (params, event) => base.completeLogin(params, event),
|
|
66
|
+
logout: (postLogoutRedirectUri, event) => base.logout(postLogoutRedirectUri, event),
|
|
67
|
+
handleLogin: (event) => base.handleLogin(event),
|
|
68
|
+
handleRegister: (event) => base.handleRegister(event),
|
|
69
|
+
handleCallback: (event) => base.handleCallback(event),
|
|
70
|
+
handleRefresh: (event) => base.handleRefresh(event),
|
|
71
|
+
handleRevoke: (event) => base.handleRevoke(event),
|
|
72
|
+
handleEntry: (event) => base.handleEntry(event),
|
|
73
|
+
handleLogout: (event) => base.handleLogout(event),
|
|
74
|
+
handleBackChannelLogout: (event) => base.handleBackChannelLogout(event),
|
|
75
|
+
handler: (event) => base.handler(event),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import type { ServerStorage, SDKStorage, SessionIdCookieStorageOptions } from './types';
|
|
3
|
+
import { createSessionIdCookieStorage as createSessionIdCookieStorageBase } from '@strivacity/sdk-core/storages/server';
|
|
4
|
+
|
|
5
|
+
export * from '@strivacity/sdk-core/storages';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a session storage that puts a random unique id value cookie on the client and keeping the actual session data in the given `storage`.
|
|
9
|
+
*
|
|
10
|
+
* @param {SDKStorage} storage - The storage used to persist session data, keyed by a randomly generated session id.
|
|
11
|
+
* @param {SessionIdCookieStorageOptions} [options] - Options for configuring the session-id cookie storage.
|
|
12
|
+
* @returns {ServerStorage} An object implementing the ServerStorage interface for managing sessions indexed by a session-id cookie.
|
|
13
|
+
*/
|
|
14
|
+
export function createSessionIdCookieStorage(storage: SDKStorage, options?: SessionIdCookieStorageOptions): ServerStorage {
|
|
15
|
+
return createSessionIdCookieStorageBase<RequestEvent>(
|
|
16
|
+
{
|
|
17
|
+
toRequest: (event) => event.request,
|
|
18
|
+
},
|
|
19
|
+
storage,
|
|
20
|
+
options,
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Handle, RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import type {
|
|
3
|
+
ServerSDKOptions,
|
|
4
|
+
ServerSDKInitConfig,
|
|
5
|
+
SDKStorage,
|
|
6
|
+
SessionData,
|
|
7
|
+
CookieOptions,
|
|
8
|
+
LogoutTokenClaims,
|
|
9
|
+
BaseServerSDK,
|
|
10
|
+
} from '@strivacity/sdk-core/types';
|
|
11
|
+
|
|
12
|
+
export * from '@strivacity/sdk-core/types';
|
|
13
|
+
|
|
14
|
+
export type SvelteKitServerStorage = SDKStorage<
|
|
15
|
+
[event?: RequestEvent],
|
|
16
|
+
[event?: RequestEvent, cookieOptions?: CookieOptions],
|
|
17
|
+
[event?: RequestEvent, cookieOptions?: CookieOptions]
|
|
18
|
+
> & {
|
|
19
|
+
deleteByLogoutToken?(token: LogoutTokenClaims): Promise<void>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type SvelteKitServerSDKOptions<
|
|
23
|
+
Storage extends SvelteKitServerStorage = SvelteKitServerStorage,
|
|
24
|
+
StateStorage extends SDKStorage = SDKStorage,
|
|
25
|
+
> = ServerSDKOptions<RequestEvent, Storage, StateStorage>;
|
|
26
|
+
|
|
27
|
+
export type SvelteKitServerSDKInitConfig<
|
|
28
|
+
Storage extends SvelteKitServerStorage = SvelteKitServerStorage,
|
|
29
|
+
StateStorage extends SDKStorage = SDKStorage,
|
|
30
|
+
> = ServerSDKInitConfig<RequestEvent> & Partial<SvelteKitServerSDKOptions<Storage, StateStorage>>;
|
|
31
|
+
|
|
32
|
+
export type SvelteKitServerSDK = BaseServerSDK<RequestEvent> & {
|
|
33
|
+
/**
|
|
34
|
+
* A SvelteKit `Handle` hook that intercepts and serves the `authUrlPrefix` auth routes.
|
|
35
|
+
* Mount it in `src/hooks.server.ts`, optionally combined with your own hooks via `sequence()`.
|
|
36
|
+
*/
|
|
37
|
+
handle: Handle;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Guard for `+page.server.ts`/`+layout.server.ts` `load()` functions: returns the current session, or throws a SvelteKit `redirect()` to the login page if unauthenticated.
|
|
41
|
+
*
|
|
42
|
+
* @param {RequestEvent} event - The SvelteKit request event.
|
|
43
|
+
* @param {Object} [options] - Optional options for the session requirement.
|
|
44
|
+
* @param {string} [options.returnTo] - Optional relative path to redirect to after login. Defaults to the current page.
|
|
45
|
+
* @returns {Promise<SessionData>} A promise that resolves to the current session data if authenticated.
|
|
46
|
+
* @throws {Redirect} Throws a SvelteKit `redirect()` to the login page if unauthenticated.
|
|
47
|
+
*/
|
|
48
|
+
requireSession(event: RequestEvent, options?: { returnTo?: string }): Promise<SessionData>;
|
|
49
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@strivacity/sdk-core/utils';
|
package/src/types.ts
ADDED
package/svelte.config.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createStyAuthProvider } from '../../src/client/components.svelte';
|
|
3
|
+
import type { StyAuthProviderProps, SDKContext, RedirectFlow, PopupFlow, NativeFlow, EmbeddedFlow, SessionData } from '../../src/types';
|
|
4
|
+
import StrivacityConsumer from './StrivacityConsumer.svelte';
|
|
5
|
+
|
|
6
|
+
type Flow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow;
|
|
7
|
+
|
|
8
|
+
const { options, initialSession = null }: { options: StyAuthProviderProps['options']; initialSession?: SessionData | null } = $props();
|
|
9
|
+
|
|
10
|
+
let session = $state<SessionData | null | undefined>(initialSession);
|
|
11
|
+
|
|
12
|
+
const ctx: SDKContext<Flow> = createStyAuthProvider({ options, session: () => session });
|
|
13
|
+
let childInstance: { getContextValue: () => SDKContext<Flow> } | undefined;
|
|
14
|
+
|
|
15
|
+
export function getContextValue() {
|
|
16
|
+
return ctx;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function setSession(value: SessionData | null | undefined) {
|
|
20
|
+
session = value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// exposes what a descendant reads via useStrivacity(), to verify createStyAuthProvider's context reaches its children
|
|
24
|
+
export function getChildContextValue() {
|
|
25
|
+
return childInstance?.getContextValue();
|
|
26
|
+
}
|
|
27
|
+
</script>
|
|
28
|
+
|
|
29
|
+
<StrivacityConsumer bind:this={childInstance} />
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from 'svelte';
|
|
3
|
+
import type { Component } from 'svelte';
|
|
4
|
+
import { STRIVACITY_SDK } from '../../src/client/hooks.svelte';
|
|
5
|
+
import type { SDKContext, RedirectFlow, PopupFlow, NativeFlow, EmbeddedFlow } from '../../src/types';
|
|
6
|
+
|
|
7
|
+
type Flow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow;
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
context,
|
|
11
|
+
component: Child,
|
|
12
|
+
componentProps = {},
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
}: { context?: SDKContext<Flow>; component: Component<any, any>; componentProps?: Record<string, unknown> } = $props();
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
let instance: any;
|
|
18
|
+
|
|
19
|
+
// setContext must run synchronously during this component's own initialization so the child
|
|
20
|
+
// component instantiated below can read it via getContext during its own initialization.
|
|
21
|
+
if (context) {
|
|
22
|
+
setContext(STRIVACITY_SDK, context);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getInstance() {
|
|
26
|
+
return instance;
|
|
27
|
+
}
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<Child {...componentProps} bind:this={instance} />
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useNativeLogin } from '../../src/client/hooks.svelte';
|
|
3
|
+
import type { UseNativeLoginOptions, LoginContext } from '../../src/types';
|
|
4
|
+
import NativeLoginContextConsumer from './NativeLoginContextConsumer.svelte';
|
|
5
|
+
|
|
6
|
+
const { options = {} }: { options?: UseNativeLoginOptions } = $props();
|
|
7
|
+
|
|
8
|
+
const login: LoginContext = useNativeLogin(options);
|
|
9
|
+
let childInstance: { getContextValue: () => LoginContext } | undefined;
|
|
10
|
+
|
|
11
|
+
export function getLogin() {
|
|
12
|
+
return login;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// exposes what a descendant widget sees via useNativeLoginContext(), to verify it resolves to the same context
|
|
16
|
+
export function getChildContext() {
|
|
17
|
+
return childInstance?.getContextValue();
|
|
18
|
+
}
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<NativeLoginContextConsumer bind:this={childInstance} />
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useNativeLoginContext } from '../../src/client/hooks.svelte';
|
|
3
|
+
import type { LoginContext } from '../../src/types';
|
|
4
|
+
|
|
5
|
+
const context: LoginContext = useNativeLoginContext();
|
|
6
|
+
|
|
7
|
+
export function getContextValue() {
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
</script>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useStrivacity } from '../../src/client/hooks.svelte';
|
|
3
|
+
import type { SDKContext, RedirectFlow, PopupFlow, NativeFlow, EmbeddedFlow } from '../../src/types';
|
|
4
|
+
|
|
5
|
+
const ctx: SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow> = useStrivacity();
|
|
6
|
+
|
|
7
|
+
export function getContextValue() {
|
|
8
|
+
return ctx;
|
|
9
|
+
}
|
|
10
|
+
</script>
|