@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 @@
|
|
|
1
|
+
require("../assets/rolldown-runtime.cjs");var e=require("@strivacity/sdk-core/utils");Object.keys(e).forEach(function(t){t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:function(){return e[t]}})});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@strivacity/sdk-core/utils';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*from"@strivacity/sdk-core/utils";
|
package/dist/types.cjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
"
|
|
2
|
-
//# sourceMappingURL=types.cjs.map
|
|
1
|
+
require("./assets/rolldown-runtime.cjs"),require("./client/types.cjs"),require("./server/types.cjs");var e=require("@strivacity/sdk-core/types");Object.keys(e).forEach(function(t){t!=="default"&&!Object.prototype.hasOwnProperty.call(exports,t)&&Object.defineProperty(exports,t,{enumerable:!0,get:function(){return e[t]}})});
|
package/dist/types.d.ts
CHANGED
|
@@ -1,181 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { RedirectFlow } from '@strivacity/sdk-core/flows/RedirectFlow';
|
|
4
|
-
import { NativeFlow } from '@strivacity/sdk-core/flows/NativeFlow';
|
|
5
|
-
/**
|
|
6
|
-
* Represents the session state, including authentication details and token information.
|
|
7
|
-
*/
|
|
8
|
-
export type State = {
|
|
9
|
-
/**
|
|
10
|
-
* Indicates if the session is being loaded.
|
|
11
|
-
*/
|
|
12
|
-
loading: boolean;
|
|
13
|
-
/**
|
|
14
|
-
* The SDK options used to configure the session.
|
|
15
|
-
*/
|
|
16
|
-
options: SDKOptions;
|
|
17
|
-
/**
|
|
18
|
-
* Indicates whether the user is authenticated.
|
|
19
|
-
*/
|
|
20
|
-
isAuthenticated: boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Claims from the ID token or `null` if not available.
|
|
23
|
-
*/
|
|
24
|
-
idTokenClaims: IdTokenClaims | null;
|
|
25
|
-
/**
|
|
26
|
-
* The access token or `null` if not available.
|
|
27
|
-
*/
|
|
28
|
-
accessToken: string | null;
|
|
29
|
-
/**
|
|
30
|
-
* The refresh token or `null` if not available.
|
|
31
|
-
*/
|
|
32
|
-
refreshToken: string | null;
|
|
33
|
-
/**
|
|
34
|
-
* Indicates if the access token has expired.
|
|
35
|
-
*/
|
|
36
|
-
accessTokenExpired: boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Expiration date of the access token or `null` if not set.
|
|
39
|
-
*/
|
|
40
|
-
accessTokenExpirationDate: number | null;
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* Represents the available authentication flows and operations for Popup-based interactions.
|
|
44
|
-
*/
|
|
45
|
-
export type PopupSDK = {
|
|
46
|
-
/**
|
|
47
|
-
* Represents the SDK instance.
|
|
48
|
-
*/
|
|
49
|
-
sdk: InstanceType<typeof PopupFlow>;
|
|
50
|
-
/**
|
|
51
|
-
* Initiates the login process.
|
|
52
|
-
*/
|
|
53
|
-
login: InstanceType<typeof PopupFlow>['login'];
|
|
54
|
-
/**
|
|
55
|
-
* Registers a new user.
|
|
56
|
-
*/
|
|
57
|
-
register: InstanceType<typeof PopupFlow>['register'];
|
|
58
|
-
/**
|
|
59
|
-
* Initiates the entry process.
|
|
60
|
-
*/
|
|
61
|
-
entry: InstanceType<typeof PopupFlow>['entry'];
|
|
62
|
-
/**
|
|
63
|
-
* Refreshes the user's session.
|
|
64
|
-
*/
|
|
65
|
-
refresh: InstanceType<typeof PopupFlow>['refresh'];
|
|
66
|
-
/**
|
|
67
|
-
* Revokes the current session tokens.
|
|
68
|
-
*/
|
|
69
|
-
revoke: InstanceType<typeof PopupFlow>['revoke'];
|
|
70
|
-
/**
|
|
71
|
-
* Logs out the user.
|
|
72
|
-
*/
|
|
73
|
-
logout: InstanceType<typeof PopupFlow>['logout'];
|
|
74
|
-
/**
|
|
75
|
-
* Handles the callback after authentication or token exchange.
|
|
76
|
-
*/
|
|
77
|
-
handleCallback: InstanceType<typeof PopupFlow>['handleCallback'];
|
|
78
|
-
};
|
|
79
|
-
/**
|
|
80
|
-
* Represents the available authentication flows and operations for Redirect-based interactions.
|
|
81
|
-
*/
|
|
82
|
-
export type RedirectSDK = {
|
|
83
|
-
/**
|
|
84
|
-
* Represents the SDK instance.
|
|
85
|
-
*/
|
|
86
|
-
sdk: InstanceType<typeof RedirectFlow>;
|
|
87
|
-
/**
|
|
88
|
-
* Initiates the login process.
|
|
89
|
-
*/
|
|
90
|
-
login: InstanceType<typeof RedirectFlow>['login'];
|
|
91
|
-
/**
|
|
92
|
-
* Registers a new user.
|
|
93
|
-
*/
|
|
94
|
-
register: InstanceType<typeof RedirectFlow>['register'];
|
|
95
|
-
/**
|
|
96
|
-
* Initiates the entry process.
|
|
97
|
-
*/
|
|
98
|
-
entry: InstanceType<typeof RedirectFlow>['entry'];
|
|
99
|
-
/**
|
|
100
|
-
* Refreshes the user's session.
|
|
101
|
-
*/
|
|
102
|
-
refresh: InstanceType<typeof RedirectFlow>['refresh'];
|
|
103
|
-
/**
|
|
104
|
-
* Revokes the current session tokens.
|
|
105
|
-
*/
|
|
106
|
-
revoke: InstanceType<typeof RedirectFlow>['revoke'];
|
|
107
|
-
/**
|
|
108
|
-
* Logs out the user.
|
|
109
|
-
*/
|
|
110
|
-
logout: InstanceType<typeof RedirectFlow>['logout'];
|
|
111
|
-
/**
|
|
112
|
-
* Handles the callback after authentication or token exchange.
|
|
113
|
-
*/
|
|
114
|
-
handleCallback: InstanceType<typeof RedirectFlow>['handleCallback'];
|
|
115
|
-
};
|
|
116
|
-
/**
|
|
117
|
-
* Represents the available authentication flows and operations for Native-based interactions.
|
|
118
|
-
*/
|
|
119
|
-
export type NativeSDK = {
|
|
120
|
-
/**
|
|
121
|
-
* Represents the SDK instance.
|
|
122
|
-
*/
|
|
123
|
-
sdk: InstanceType<typeof NativeFlow>;
|
|
124
|
-
/**
|
|
125
|
-
* Initiates the login process.
|
|
126
|
-
*/
|
|
127
|
-
login: InstanceType<typeof NativeFlow>['login'];
|
|
128
|
-
/**
|
|
129
|
-
* Registers a new user.
|
|
130
|
-
*/
|
|
131
|
-
register: InstanceType<typeof NativeFlow>['register'];
|
|
132
|
-
/**
|
|
133
|
-
* Initiates the entry process.
|
|
134
|
-
*/
|
|
135
|
-
entry: InstanceType<typeof NativeFlow>['entry'];
|
|
136
|
-
/**
|
|
137
|
-
* Refreshes the user's session.
|
|
138
|
-
*/
|
|
139
|
-
refresh: InstanceType<typeof NativeFlow>['refresh'];
|
|
140
|
-
/**
|
|
141
|
-
* Revokes the current session tokens.
|
|
142
|
-
*/
|
|
143
|
-
revoke: InstanceType<typeof NativeFlow>['revoke'];
|
|
144
|
-
/**
|
|
145
|
-
* Logs out the user.
|
|
146
|
-
*/
|
|
147
|
-
logout: InstanceType<typeof NativeFlow>['logout'];
|
|
148
|
-
/**
|
|
149
|
-
* Handles the callback after authentication or token exchange.
|
|
150
|
-
*/
|
|
151
|
-
handleCallback: InstanceType<typeof NativeFlow>['handleCallback'];
|
|
152
|
-
};
|
|
153
|
-
/**
|
|
154
|
-
* Represents a combined context for Popup-based flows, containing both the Popup SDK and the session state.
|
|
155
|
-
*/
|
|
156
|
-
export type PopupContext = PopupSDK & {
|
|
157
|
-
state: State;
|
|
158
|
-
};
|
|
159
|
-
/**
|
|
160
|
-
* Represents a combined context for Redirect-based flows, containing both the Redirect SDK and the session state.
|
|
161
|
-
*/
|
|
162
|
-
export type RedirectContext = RedirectSDK & {
|
|
163
|
-
state: State;
|
|
164
|
-
};
|
|
165
|
-
/**
|
|
166
|
-
* Represents a combined context for Native-based flows, containing both the Native SDK and the session state.
|
|
167
|
-
*/
|
|
168
|
-
export type NativeContext = NativeSDK & {
|
|
169
|
-
state: State;
|
|
170
|
-
};
|
|
171
|
-
export type NativeFlowContextValue = {
|
|
172
|
-
loading: boolean;
|
|
173
|
-
forms: Record<string, Record<string, unknown>>;
|
|
174
|
-
messages: Record<string, Record<string, LoginFlowMessage>>;
|
|
175
|
-
state: Partial<LoginFlowState>;
|
|
176
|
-
submitForm: (formId: string) => Promise<void>;
|
|
177
|
-
triggerFallback: (hostedUrl?: string) => void;
|
|
178
|
-
triggerClose: () => void;
|
|
179
|
-
setFormValue: (formId: string, widgetId: string, value: unknown) => void;
|
|
180
|
-
setMessage: (formId: string, widgetId: string, value: LoginFlowMessage) => void;
|
|
181
|
-
};
|
|
1
|
+
export * from './client/types';
|
|
2
|
+
export * from './server/types';
|
package/dist/types.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=types.mjs.map
|
|
1
|
+
import"./client/types.mjs";import"./server/types.mjs";export*from"@strivacity/sdk-core/types";
|
package/package.json
CHANGED
|
@@ -1,27 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strivacity/sdk-svelte",
|
|
3
|
-
"version": "
|
|
4
|
-
"license": "MIT",
|
|
3
|
+
"version": "4.0.0-beta.0",
|
|
5
4
|
"description": "Strivacity Svelte SDK client",
|
|
5
|
+
"license": "MIT",
|
|
6
6
|
"author": "strivacity <opensource@strivacity.com>",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "https://github.com/Strivacity/sdk-js"
|
|
10
10
|
},
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
"./client": {
|
|
15
|
+
"types": "./dist/client/index.d.ts",
|
|
16
|
+
"import": "./dist/client/index.mjs",
|
|
17
|
+
"require": "./dist/client/index.cjs",
|
|
18
|
+
"default": "./dist/client/index.mjs"
|
|
19
|
+
},
|
|
20
|
+
"./client/*": {
|
|
21
|
+
"types": "./dist/client/*.d.ts",
|
|
22
|
+
"import": "./dist/client/*.mjs",
|
|
23
|
+
"require": "./dist/client/*.cjs",
|
|
24
|
+
"default": "./dist/client/*.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./client/components": {
|
|
27
|
+
"types": "./dist/client/components.svelte.d.ts",
|
|
28
|
+
"import": "./dist/client/components.svelte.mjs",
|
|
29
|
+
"require": "./dist/client/components.svelte.cjs",
|
|
30
|
+
"default": "./dist/client/components.svelte.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./client/hooks": {
|
|
33
|
+
"types": "./dist/client/hooks.svelte.d.ts",
|
|
34
|
+
"import": "./dist/client/hooks.svelte.mjs",
|
|
35
|
+
"require": "./dist/client/hooks.svelte.cjs",
|
|
36
|
+
"default": "./dist/client/hooks.svelte.mjs"
|
|
37
|
+
},
|
|
38
|
+
"./server": {
|
|
39
|
+
"types": "./dist/server/index.d.ts",
|
|
40
|
+
"import": "./dist/server/index.mjs",
|
|
41
|
+
"require": "./dist/server/index.cjs",
|
|
42
|
+
"default": "./dist/server/index.mjs"
|
|
43
|
+
},
|
|
44
|
+
"./server/*": {
|
|
45
|
+
"types": "./dist/server/*.d.ts",
|
|
46
|
+
"import": "./dist/server/*.mjs",
|
|
47
|
+
"require": "./dist/server/*.cjs",
|
|
48
|
+
"default": "./dist/server/*.mjs"
|
|
49
|
+
},
|
|
50
|
+
"./errors": {
|
|
51
|
+
"types": "./dist/errors.d.ts",
|
|
52
|
+
"import": "./dist/errors.mjs",
|
|
53
|
+
"require": "./dist/errors.cjs",
|
|
54
|
+
"default": "./dist/errors.mjs"
|
|
55
|
+
},
|
|
56
|
+
"./types": {
|
|
57
|
+
"types": "./dist/types.d.ts",
|
|
58
|
+
"import": "./dist/types.mjs",
|
|
59
|
+
"require": "./dist/types.cjs",
|
|
60
|
+
"default": "./dist/types.mjs"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
11
63
|
"dependencies": {
|
|
12
|
-
"@strivacity/sdk-core": "
|
|
64
|
+
"@strivacity/sdk-core": "4.0.0-beta.0"
|
|
13
65
|
},
|
|
14
66
|
"peerDependencies": {
|
|
67
|
+
"@sveltejs/kit": ">=2",
|
|
15
68
|
"svelte": ">=5"
|
|
16
69
|
},
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
".": {
|
|
21
|
-
"types": "./dist/index.d.ts",
|
|
22
|
-
"import": "./dist/index.mjs",
|
|
23
|
-
"require": "./dist/index.cjs",
|
|
24
|
-
"default": "./dist/index.mjs"
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"@sveltejs/kit": {
|
|
72
|
+
"optional": true
|
|
25
73
|
}
|
|
26
74
|
}
|
|
27
75
|
}
|
package/project.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
3
|
+
"name": "sdk-svelte",
|
|
4
|
+
"projectType": "library",
|
|
5
|
+
"sourceRoot": "packages/sdk-svelte/src",
|
|
6
|
+
"targets": {
|
|
7
|
+
"type-check": {
|
|
8
|
+
"executor": "nx:run-commands",
|
|
9
|
+
"dependsOn": ["sdk-core:build"],
|
|
10
|
+
"options": {
|
|
11
|
+
"cwd": "packages/sdk-svelte",
|
|
12
|
+
"command": "svelte-check --tsconfig ./tsconfig.app.json"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"build": {
|
|
16
|
+
"executor": "nx:run-commands",
|
|
17
|
+
"dependsOn": ["type-check"],
|
|
18
|
+
"outputs": ["{workspaceRoot}/packages/sdk-svelte/dist"],
|
|
19
|
+
"defaultConfiguration": "development",
|
|
20
|
+
"options": {
|
|
21
|
+
"cwd": "packages/sdk-svelte"
|
|
22
|
+
},
|
|
23
|
+
"configurations": {
|
|
24
|
+
"serve": {
|
|
25
|
+
"mode": "development",
|
|
26
|
+
"command": "vite build --watch"
|
|
27
|
+
},
|
|
28
|
+
"development": {
|
|
29
|
+
"mode": "development",
|
|
30
|
+
"command": "vite build"
|
|
31
|
+
},
|
|
32
|
+
"production": {
|
|
33
|
+
"mode": "production",
|
|
34
|
+
"command": "vite build"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"test": {
|
|
39
|
+
"executor": "nx:run-commands",
|
|
40
|
+
"dependsOn": ["build"],
|
|
41
|
+
"options": {
|
|
42
|
+
"cwd": "packages/sdk-svelte",
|
|
43
|
+
"command": "vitest"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { setContext, onDestroy } from 'svelte';
|
|
2
|
+
import { initFlow } from '@strivacity/sdk-core';
|
|
3
|
+
import type { IdTokenClaims, RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, SDKContext, SDKInstance, StyAuthProviderProps } from '../types';
|
|
4
|
+
import { STRIVACITY_SDK } from './hooks.svelte';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The `StyAuthProvider` component is a Svelte context provider that initializes the Strivacity SDK and provides its state and methods to the rest of the application.
|
|
8
|
+
*
|
|
9
|
+
* @param {StyAuthProviderProps} props - The properties for the `StyAuthProvider` component.
|
|
10
|
+
* @param {SDKInitConfig} props.options - The configuration options for initializing the Strivacity SDK.
|
|
11
|
+
* @param {() => SessionData | null | undefined} [props.session] - Optional getter for session data to initialize/refresh the SDK with.
|
|
12
|
+
*
|
|
13
|
+
* @returns {SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow>} A Svelte context provider that wraps its children and provides access to the Strivacity SDK.
|
|
14
|
+
*/
|
|
15
|
+
export function createStyAuthProvider(props: StyAuthProviderProps): SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow> {
|
|
16
|
+
let sdk: SDKInstance;
|
|
17
|
+
let subscriptions: ReturnType<SDKInstance['subscribeToAllEvents']> | null = null;
|
|
18
|
+
|
|
19
|
+
let loading = $state<boolean>(true);
|
|
20
|
+
let language = $state<string>(globalThis.navigator?.language ?? 'en-US');
|
|
21
|
+
let isAuthenticated = $state<boolean>(false);
|
|
22
|
+
let idTokenClaims = $state<IdTokenClaims | null>(null);
|
|
23
|
+
let accessToken = $state<string | null>(null);
|
|
24
|
+
let refreshToken = $state<string | null>(null);
|
|
25
|
+
let accessTokenExpired = $state<boolean>(true);
|
|
26
|
+
let accessTokenExpirationDate = $state<number | null>(null);
|
|
27
|
+
|
|
28
|
+
const updateSession = async () => {
|
|
29
|
+
const authenticated = await sdk.isAuthenticated;
|
|
30
|
+
|
|
31
|
+
if (loading) {
|
|
32
|
+
loading = false;
|
|
33
|
+
}
|
|
34
|
+
if (sdk.language !== language) {
|
|
35
|
+
language = sdk.language;
|
|
36
|
+
}
|
|
37
|
+
if (authenticated !== isAuthenticated) {
|
|
38
|
+
isAuthenticated = authenticated;
|
|
39
|
+
}
|
|
40
|
+
if (sdk.idTokenClaims !== idTokenClaims) {
|
|
41
|
+
idTokenClaims = sdk.idTokenClaims || null;
|
|
42
|
+
}
|
|
43
|
+
if (sdk.accessToken !== accessToken) {
|
|
44
|
+
accessToken = sdk.accessToken || null;
|
|
45
|
+
}
|
|
46
|
+
if (sdk.refreshToken !== refreshToken) {
|
|
47
|
+
refreshToken = sdk.refreshToken || null;
|
|
48
|
+
}
|
|
49
|
+
if (sdk.accessTokenExpired !== accessTokenExpired) {
|
|
50
|
+
accessTokenExpired = sdk.accessTokenExpired;
|
|
51
|
+
}
|
|
52
|
+
if (sdk.accessTokenExpirationDate !== accessTokenExpirationDate) {
|
|
53
|
+
accessTokenExpirationDate = sdk.accessTokenExpirationDate || null;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function onInit() {
|
|
58
|
+
sdk = initFlow(props.options as never);
|
|
59
|
+
subscriptions = sdk.subscribeToAllEvents(updateSession);
|
|
60
|
+
|
|
61
|
+
$effect(() => {
|
|
62
|
+
const session = props.session?.();
|
|
63
|
+
|
|
64
|
+
if (session) {
|
|
65
|
+
sdk.session = session;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
void updateSession();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
onDestroy(() => {
|
|
73
|
+
subscriptions?.dispose();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
if (typeof window !== 'undefined') {
|
|
77
|
+
onInit();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const contextValue: SDKContext<RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow> = {
|
|
81
|
+
get sdk() {
|
|
82
|
+
return sdk;
|
|
83
|
+
},
|
|
84
|
+
get loading() {
|
|
85
|
+
return loading;
|
|
86
|
+
},
|
|
87
|
+
get language() {
|
|
88
|
+
return language;
|
|
89
|
+
},
|
|
90
|
+
get isAuthenticated() {
|
|
91
|
+
return isAuthenticated;
|
|
92
|
+
},
|
|
93
|
+
get idTokenClaims() {
|
|
94
|
+
return idTokenClaims;
|
|
95
|
+
},
|
|
96
|
+
get accessToken() {
|
|
97
|
+
return accessToken;
|
|
98
|
+
},
|
|
99
|
+
get refreshToken() {
|
|
100
|
+
return refreshToken;
|
|
101
|
+
},
|
|
102
|
+
get accessTokenExpired() {
|
|
103
|
+
return accessTokenExpired;
|
|
104
|
+
},
|
|
105
|
+
get accessTokenExpirationDate() {
|
|
106
|
+
return accessTokenExpirationDate;
|
|
107
|
+
},
|
|
108
|
+
init: () => sdk.init(),
|
|
109
|
+
subscribeToEvent: ((...args: Parameters<typeof sdk.subscribeToEvent>) => sdk.subscribeToEvent(...args)) as typeof sdk.subscribeToEvent,
|
|
110
|
+
subscribeToAllEvents: (...args: Parameters<typeof sdk.subscribeToAllEvents>) => sdk.subscribeToAllEvents(...args),
|
|
111
|
+
checkAuthentication: (...args: Parameters<typeof sdk.checkAuthentication>) => sdk.checkAuthentication(...args),
|
|
112
|
+
tokenExchange: (...args: Parameters<typeof sdk.tokenExchange>) => sdk.tokenExchange(...args),
|
|
113
|
+
handleCallback: (...args: Parameters<typeof sdk.handleCallback>) => sdk.handleCallback(...args),
|
|
114
|
+
refresh: () => sdk.refresh(),
|
|
115
|
+
revoke: () => sdk.revoke(),
|
|
116
|
+
logout: (...args: Parameters<typeof sdk.logout>) => sdk.logout(...args),
|
|
117
|
+
login: (...args: Parameters<typeof sdk.login>) => sdk.login(...args),
|
|
118
|
+
register: (...args: Parameters<typeof sdk.register>) => sdk.register(...args),
|
|
119
|
+
entry: (...args: Parameters<typeof sdk.entry>) => sdk.entry(...args),
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
setContext(STRIVACITY_SDK, contextValue);
|
|
123
|
+
|
|
124
|
+
return contextValue;
|
|
125
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { getContext, setContext, onMount, onDestroy } from 'svelte';
|
|
2
|
+
import { SvelteURL } from 'svelte/reactivity';
|
|
3
|
+
import type { RedirectFlow, PopupFlow, EmbeddedFlow, NativeFlow, NativeParams, NativeFlowState, NativeFlowMessage } from '@strivacity/sdk-core/types';
|
|
4
|
+
import type { LoginContext, SDKContext, UseNativeLoginOptions } from '../types';
|
|
5
|
+
import { FallbackError } from '@strivacity/sdk-core/utils/errors';
|
|
6
|
+
import { unflattenObject } from '@strivacity/sdk-core/utils';
|
|
7
|
+
|
|
8
|
+
export const STRIVACITY_SDK = Symbol('strivacity-sdk');
|
|
9
|
+
export const STRIVACITY_LOGIN_CONTEXT = Symbol('strivacity-login-context');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Access the Strivacity SDK context.
|
|
13
|
+
*
|
|
14
|
+
* @template T - The type of the Strivacity SDK flow (RedirectFlow, PopupFlow, NativeFlow, or EmbeddedFlow).
|
|
15
|
+
* @returns {SDKContext<T>} - The Strivacity SDK context.
|
|
16
|
+
* @throws {Error} - If the Strivacity SDK context is not found.
|
|
17
|
+
*/
|
|
18
|
+
export const useStrivacity = <
|
|
19
|
+
T extends RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow = RedirectFlow | PopupFlow | NativeFlow | EmbeddedFlow,
|
|
20
|
+
>(): SDKContext<T> => {
|
|
21
|
+
const context = getContext<SDKContext<T>>(STRIVACITY_SDK);
|
|
22
|
+
|
|
23
|
+
if (!context) {
|
|
24
|
+
throw new Error('Missing SDK context');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return context;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Access the Strivacity SDK context specifically for native login flow.
|
|
32
|
+
*
|
|
33
|
+
* @returns {LoginContext} - The native flow context.
|
|
34
|
+
* @throws {Error} - If the Strivacity SDK context is not found.
|
|
35
|
+
*/
|
|
36
|
+
export const useNativeLoginContext = (): LoginContext => {
|
|
37
|
+
const context = getContext<LoginContext>(STRIVACITY_LOGIN_CONTEXT);
|
|
38
|
+
|
|
39
|
+
if (!context) {
|
|
40
|
+
throw new Error('Missing SDK native login context');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return context;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Manages a native login flow session.
|
|
48
|
+
*
|
|
49
|
+
* @param {UseNativeLoginOptions} [options] - Optional options for the login session.
|
|
50
|
+
* @returns {LoginContext} - The native flow context.
|
|
51
|
+
* @throws {Error} - If the Strivacity SDK context is not found.
|
|
52
|
+
*/
|
|
53
|
+
export function useNativeLogin(options: UseNativeLoginOptions = {}): LoginContext {
|
|
54
|
+
const abortController = new AbortController();
|
|
55
|
+
const ctx = useStrivacity<NativeFlow>();
|
|
56
|
+
|
|
57
|
+
let loading = $state<boolean>(true);
|
|
58
|
+
let forms = $state<Record<string, Record<string, unknown>>>({});
|
|
59
|
+
let messages = $state<Record<string, Record<string, NativeFlowMessage>>>({});
|
|
60
|
+
let state = $state<Partial<NativeFlowState>>({});
|
|
61
|
+
|
|
62
|
+
onMount(async () => {
|
|
63
|
+
await ctx.init();
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
await startSession(options.params);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error instanceof DOMException && error.name === 'AbortError') {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
ctx.sdk.logging?.error('Error fetching authorization URI', error as Error);
|
|
73
|
+
void options.onError?.(error as Error);
|
|
74
|
+
loading = false;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
onDestroy(() => {
|
|
79
|
+
abortController.abort();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
async function startSession(loginParams: NativeParams = {}) {
|
|
83
|
+
try {
|
|
84
|
+
loading = true;
|
|
85
|
+
|
|
86
|
+
const nextState = await ctx.sdk.startSession(loginParams);
|
|
87
|
+
|
|
88
|
+
if (nextState) {
|
|
89
|
+
handleResponse(nextState);
|
|
90
|
+
}
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (error instanceof FallbackError) {
|
|
93
|
+
ctx.sdk.logging?.error('Fallback error occurred', error);
|
|
94
|
+
void options.onFallback?.(error);
|
|
95
|
+
} else {
|
|
96
|
+
ctx.sdk.logging?.error('Error starting session', error as Error);
|
|
97
|
+
void options.onError?.(error as Error);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function handleResponse(nextState: Partial<NativeFlowState>) {
|
|
103
|
+
if (ctx.sdk.session) {
|
|
104
|
+
return void options.onLogin?.(ctx.sdk.session);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const newState: NativeFlowState = {
|
|
108
|
+
hostedUrl: nextState?.hostedUrl ?? state.hostedUrl,
|
|
109
|
+
finalizeUrl: nextState?.finalizeUrl ?? state.finalizeUrl,
|
|
110
|
+
screen: nextState?.screen ?? state.screen,
|
|
111
|
+
forms: nextState?.forms ?? state.forms,
|
|
112
|
+
layout: nextState?.layout ?? state.layout,
|
|
113
|
+
messages: nextState?.messages ?? {},
|
|
114
|
+
branding: nextState?.branding ?? state.branding,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
if (newState.screen !== state.screen) {
|
|
118
|
+
forms = {};
|
|
119
|
+
messages = {};
|
|
120
|
+
|
|
121
|
+
for (const form of newState.forms ?? []) {
|
|
122
|
+
forms[form.id] = {};
|
|
123
|
+
messages[form.id] = {};
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
ctx.sdk.logging?.info(`Updating screen: ${newState.screen}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for (const formId of Object.keys(newState.messages ?? {})) {
|
|
130
|
+
if (formId === 'global') {
|
|
131
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
|
|
132
|
+
void options.onGlobalMessage?.(newState.messages?.global!);
|
|
133
|
+
} else {
|
|
134
|
+
messages[formId] = newState.messages![formId] ?? {};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
state = newState;
|
|
139
|
+
|
|
140
|
+
if (!newState.finalizeUrl) {
|
|
141
|
+
loading = false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function triggerFallback(message?: string) {
|
|
146
|
+
ctx.sdk.logging?.warn(message ? `Triggering fallback due to: ${message}` : 'Triggering fallback');
|
|
147
|
+
|
|
148
|
+
if (!state.hostedUrl) {
|
|
149
|
+
const error = new Error('No hosted URL provided');
|
|
150
|
+
ctx.sdk.logging?.error('Fallback error', error);
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
void options.onFallback?.(new FallbackError(new SvelteURL(state.hostedUrl)));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function triggerClose() {
|
|
158
|
+
ctx.sdk.logging?.debug('Triggering close');
|
|
159
|
+
void options.onClose?.();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function submitForm(formId: string, customBody?: Record<string, unknown>) {
|
|
163
|
+
try {
|
|
164
|
+
loading = true;
|
|
165
|
+
|
|
166
|
+
const nextState = await ctx.sdk.submitForm(formId, customBody ?? unflattenObject(forms[formId] ?? {}));
|
|
167
|
+
|
|
168
|
+
if (nextState) {
|
|
169
|
+
handleResponse(nextState);
|
|
170
|
+
}
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (error instanceof FallbackError) {
|
|
173
|
+
ctx.sdk.logging?.error('Fallback error occurred', error);
|
|
174
|
+
void options.onFallback?.(error);
|
|
175
|
+
} else {
|
|
176
|
+
ctx.sdk.logging?.error('Error submitting form', error as Error);
|
|
177
|
+
void options.onError?.(error as Error);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function setFormValue(formId: string, widgetId: string, value: unknown) {
|
|
183
|
+
if (forms[formId] === undefined) {
|
|
184
|
+
forms[formId] = {};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
forms[formId][widgetId] = value === '' ? null : value;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function setMessage(formId: string, widgetId: string, value: NativeFlowMessage) {
|
|
191
|
+
if (messages[formId] === undefined) {
|
|
192
|
+
messages[formId] = {};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
messages[formId][widgetId] = value;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const context: LoginContext = {
|
|
199
|
+
get loading() {
|
|
200
|
+
return loading;
|
|
201
|
+
},
|
|
202
|
+
get forms() {
|
|
203
|
+
return forms;
|
|
204
|
+
},
|
|
205
|
+
get messages() {
|
|
206
|
+
return messages;
|
|
207
|
+
},
|
|
208
|
+
get state() {
|
|
209
|
+
return state;
|
|
210
|
+
},
|
|
211
|
+
triggerFallback,
|
|
212
|
+
triggerClose,
|
|
213
|
+
submitForm,
|
|
214
|
+
setFormValue,
|
|
215
|
+
setMessage,
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
setContext(STRIVACITY_LOGIN_CONTEXT, context);
|
|
219
|
+
|
|
220
|
+
return context;
|
|
221
|
+
}
|