@douvery/auth 0.3.1 → 0.3.2
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/package.json +4 -3
- package/src/qwik/index.tsx +271 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@douvery/auth",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "OAuth 2.0/OIDC client for Douvery authentication",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
"types": "./dist/react/index.d.ts"
|
|
17
17
|
},
|
|
18
18
|
"./qwik": {
|
|
19
|
-
"import": "./
|
|
19
|
+
"import": "./src/qwik/index.tsx",
|
|
20
20
|
"types": "./dist/qwik/index.d.ts"
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
|
-
"dist"
|
|
24
|
+
"dist",
|
|
25
|
+
"src/qwik"
|
|
25
26
|
],
|
|
26
27
|
"scripts": {
|
|
27
28
|
"build": "npm run clean && tsup",
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @douvery/auth/qwik - Qwik adapter
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createContextId,
|
|
7
|
+
useContextProvider,
|
|
8
|
+
useContext,
|
|
9
|
+
useSignal,
|
|
10
|
+
useTask$,
|
|
11
|
+
useVisibleTask$,
|
|
12
|
+
component$,
|
|
13
|
+
Slot,
|
|
14
|
+
type Signal,
|
|
15
|
+
} from "@builder.io/qwik";
|
|
16
|
+
import {
|
|
17
|
+
DouveryAuthClient,
|
|
18
|
+
createDouveryAuth,
|
|
19
|
+
type DouveryAuthConfig,
|
|
20
|
+
type AuthState,
|
|
21
|
+
type User,
|
|
22
|
+
type LoginOptions,
|
|
23
|
+
type LogoutOptions,
|
|
24
|
+
type SelectAccountOptions,
|
|
25
|
+
type RegisterOptions,
|
|
26
|
+
type RecoverAccountOptions,
|
|
27
|
+
type VerifyAccountOptions,
|
|
28
|
+
type UpgradeAccountOptions,
|
|
29
|
+
type SetupPasskeyOptions,
|
|
30
|
+
type SetupAddressOptions,
|
|
31
|
+
type AddAccountOptions,
|
|
32
|
+
type RevokeTokenOptions,
|
|
33
|
+
type AuthUrl,
|
|
34
|
+
} from "@douvery/auth";
|
|
35
|
+
|
|
36
|
+
interface DouveryAuthContextValue {
|
|
37
|
+
state: Signal<AuthState>;
|
|
38
|
+
isInitialized: Signal<boolean>;
|
|
39
|
+
isLoading: Signal<boolean>;
|
|
40
|
+
error: Signal<Error | null>;
|
|
41
|
+
client: DouveryAuthClient;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const DouveryAuthContext =
|
|
45
|
+
createContextId<DouveryAuthContextValue>("douvery-auth");
|
|
46
|
+
|
|
47
|
+
export interface DouveryAuthProviderProps {
|
|
48
|
+
config: DouveryAuthConfig;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const DouveryAuthProvider = component$<DouveryAuthProviderProps>(
|
|
52
|
+
({ config }) => {
|
|
53
|
+
const client = createDouveryAuth(config);
|
|
54
|
+
const state = useSignal<AuthState>(client.getState());
|
|
55
|
+
const isInitialized = useSignal(false);
|
|
56
|
+
const isLoading = useSignal(false);
|
|
57
|
+
const error = useSignal<Error | null>(null);
|
|
58
|
+
|
|
59
|
+
useContextProvider(DouveryAuthContext, {
|
|
60
|
+
state,
|
|
61
|
+
isInitialized,
|
|
62
|
+
isLoading,
|
|
63
|
+
error,
|
|
64
|
+
client,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
useVisibleTask$(() => {
|
|
68
|
+
client
|
|
69
|
+
.initialize()
|
|
70
|
+
.then(() => {
|
|
71
|
+
isInitialized.value = true;
|
|
72
|
+
state.value = client.getState();
|
|
73
|
+
})
|
|
74
|
+
.catch((err) => {
|
|
75
|
+
error.value = err instanceof Error ? err : new Error(String(err));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const unsubscribe = client.subscribe((event) => {
|
|
79
|
+
state.value = client.getState();
|
|
80
|
+
if (
|
|
81
|
+
event.type === "LOGIN_ERROR" ||
|
|
82
|
+
event.type === "LOGOUT_ERROR" ||
|
|
83
|
+
event.type === "TOKEN_REFRESH_ERROR"
|
|
84
|
+
) {
|
|
85
|
+
error.value = event.error;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return () => unsubscribe();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return <Slot />;
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
export function useDouveryAuth() {
|
|
97
|
+
return useContext(DouveryAuthContext);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function useUser(): Signal<User | null> {
|
|
101
|
+
const { state } = useDouveryAuth();
|
|
102
|
+
const user = useSignal<User | null>(state.value.user);
|
|
103
|
+
useTask$(({ track }) => {
|
|
104
|
+
track(() => state.value);
|
|
105
|
+
user.value = state.value.user;
|
|
106
|
+
});
|
|
107
|
+
return user;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function useIsAuthenticated(): Signal<boolean> {
|
|
111
|
+
const { state } = useDouveryAuth();
|
|
112
|
+
const isAuth = useSignal(state.value.status === "authenticated");
|
|
113
|
+
useTask$(({ track }) => {
|
|
114
|
+
track(() => state.value);
|
|
115
|
+
isAuth.value = state.value.status === "authenticated";
|
|
116
|
+
});
|
|
117
|
+
return isAuth;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function useAuthActions() {
|
|
121
|
+
const { client, isLoading, error } = useDouveryAuth();
|
|
122
|
+
|
|
123
|
+
const login = async (options?: LoginOptions) => {
|
|
124
|
+
isLoading.value = true;
|
|
125
|
+
error.value = null;
|
|
126
|
+
try {
|
|
127
|
+
await client.login(options);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
error.value = err instanceof Error ? err : new Error(String(err));
|
|
130
|
+
throw err;
|
|
131
|
+
} finally {
|
|
132
|
+
isLoading.value = false;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const logout = async (options?: LogoutOptions) => {
|
|
137
|
+
isLoading.value = true;
|
|
138
|
+
error.value = null;
|
|
139
|
+
try {
|
|
140
|
+
await client.logout(options);
|
|
141
|
+
} catch (err) {
|
|
142
|
+
error.value = err instanceof Error ? err : new Error(String(err));
|
|
143
|
+
throw err;
|
|
144
|
+
} finally {
|
|
145
|
+
isLoading.value = false;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const selectAccount = (options?: SelectAccountOptions) => {
|
|
150
|
+
client.selectAccount(options);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const addAccount = (options?: AddAccountOptions) => {
|
|
154
|
+
client.addAccount(options);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const register = (options?: RegisterOptions) => {
|
|
158
|
+
client.register(options);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const recoverAccount = (options?: RecoverAccountOptions) => {
|
|
162
|
+
client.recoverAccount(options);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const verifyAccount = (options?: VerifyAccountOptions) => {
|
|
166
|
+
client.verifyAccount(options);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const upgradeAccount = (options?: UpgradeAccountOptions) => {
|
|
170
|
+
client.upgradeAccount(options);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const setupPasskey = (options?: SetupPasskeyOptions) => {
|
|
174
|
+
client.setupPasskey(options);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const setupAddress = (options?: SetupAddressOptions) => {
|
|
178
|
+
client.setupAddress(options);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const revokeToken = async (options?: RevokeTokenOptions) => {
|
|
182
|
+
isLoading.value = true;
|
|
183
|
+
error.value = null;
|
|
184
|
+
try {
|
|
185
|
+
await client.revokeToken(options);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
error.value = err instanceof Error ? err : new Error(String(err));
|
|
188
|
+
throw err;
|
|
189
|
+
} finally {
|
|
190
|
+
isLoading.value = false;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
login,
|
|
196
|
+
logout,
|
|
197
|
+
selectAccount,
|
|
198
|
+
addAccount,
|
|
199
|
+
register,
|
|
200
|
+
recoverAccount,
|
|
201
|
+
verifyAccount,
|
|
202
|
+
upgradeAccount,
|
|
203
|
+
setupPasskey,
|
|
204
|
+
setupAddress,
|
|
205
|
+
revokeToken,
|
|
206
|
+
isLoading,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Get URL builders for auth pages (non-redirecting, useful for <a> tags) */
|
|
211
|
+
export function useAuthUrls() {
|
|
212
|
+
const { client } = useDouveryAuth();
|
|
213
|
+
return {
|
|
214
|
+
loginUrl: (options?: LoginOptions): AuthUrl =>
|
|
215
|
+
client.buildLoginUrl(options),
|
|
216
|
+
logoutUrl: (options?: LogoutOptions): AuthUrl =>
|
|
217
|
+
client.buildLogoutUrl(options),
|
|
218
|
+
selectAccountUrl: (options?: SelectAccountOptions): AuthUrl =>
|
|
219
|
+
client.buildSelectAccountUrl(options),
|
|
220
|
+
addAccountUrl: (options?: AddAccountOptions): AuthUrl =>
|
|
221
|
+
client.buildAddAccountUrl(options),
|
|
222
|
+
registerUrl: (options?: RegisterOptions): AuthUrl =>
|
|
223
|
+
client.buildRegisterUrl(options),
|
|
224
|
+
recoverAccountUrl: (options?: RecoverAccountOptions): AuthUrl =>
|
|
225
|
+
client.buildRecoverAccountUrl(options),
|
|
226
|
+
verifyAccountUrl: (options?: VerifyAccountOptions): AuthUrl =>
|
|
227
|
+
client.buildVerifyAccountUrl(options),
|
|
228
|
+
upgradeAccountUrl: (options?: UpgradeAccountOptions): AuthUrl =>
|
|
229
|
+
client.buildUpgradeAccountUrl(options),
|
|
230
|
+
setupPasskeyUrl: (options?: SetupPasskeyOptions): AuthUrl =>
|
|
231
|
+
client.buildSetupPasskeyUrl(options),
|
|
232
|
+
setupAddressUrl: (options?: SetupAddressOptions): AuthUrl =>
|
|
233
|
+
client.buildSetupAddressUrl(options),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Get session status helpers */
|
|
238
|
+
export function useSessionStatus() {
|
|
239
|
+
const { client, state } = useDouveryAuth();
|
|
240
|
+
const isExpired = useSignal(client.isSessionExpired());
|
|
241
|
+
const needsVerification = useSignal(client.needsEmailVerification());
|
|
242
|
+
const isGuest = useSignal(client.isGuestAccount());
|
|
243
|
+
|
|
244
|
+
useTask$(({ track }) => {
|
|
245
|
+
track(() => state.value);
|
|
246
|
+
isExpired.value = client.isSessionExpired();
|
|
247
|
+
needsVerification.value = client.needsEmailVerification();
|
|
248
|
+
isGuest.value = client.isGuestAccount();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
return { isExpired, needsVerification, isGuest };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export { DouveryAuthClient, createDouveryAuth } from "@douvery/auth";
|
|
255
|
+
export type {
|
|
256
|
+
DouveryAuthConfig,
|
|
257
|
+
AuthState,
|
|
258
|
+
User,
|
|
259
|
+
LoginOptions,
|
|
260
|
+
LogoutOptions,
|
|
261
|
+
SelectAccountOptions,
|
|
262
|
+
RegisterOptions,
|
|
263
|
+
RecoverAccountOptions,
|
|
264
|
+
VerifyAccountOptions,
|
|
265
|
+
UpgradeAccountOptions,
|
|
266
|
+
SetupPasskeyOptions,
|
|
267
|
+
SetupAddressOptions,
|
|
268
|
+
AddAccountOptions,
|
|
269
|
+
RevokeTokenOptions,
|
|
270
|
+
AuthUrl,
|
|
271
|
+
} from "@douvery/auth";
|