@entro314labs/entro-auth-iris 0.0.0-stage → 1.8.0-beta.12
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/LICENSE.md +20 -0
- package/dist/index.d.mts +674 -0
- package/dist/index.mjs +535 -0
- package/package.json +53 -3
- package/README.md +0 -3
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2024 - present, Bereket Engida
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
this software and associated documentation files (the “Software”), to deal in
|
|
6
|
+
the Software without restriction, including without limitation the rights to
|
|
7
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
18
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
19
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
20
|
+
DEALINGS IN THE SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract of the Iris UI auth blocks (`@/lib/auth-flow` there). Iris UI is
|
|
3
|
+
* copied into an app, so these types are repeated here and not imported; the
|
|
4
|
+
* two are compared by `pnpm check:seam`.
|
|
5
|
+
*/
|
|
6
|
+
/** A single sign-on provider a block can offer. */
|
|
7
|
+
type AuthProvider = "Google" | "Apple" | "GitHub" | "Facebook";
|
|
8
|
+
/** The person a sign in resolved to. */
|
|
9
|
+
interface AuthAccount {
|
|
10
|
+
name: string;
|
|
11
|
+
email: string;
|
|
12
|
+
photo?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A second step after the first credential was accepted. Three take a code;
|
|
16
|
+
* `passkey` takes none, and its handler is called with an empty one.
|
|
17
|
+
*/
|
|
18
|
+
type SecondFactorMethod = "totp" | "otp" | "backup-code" | "passkey";
|
|
19
|
+
/**
|
|
20
|
+
* What a handler resolves with: an account when signed in, `secondFactor` when
|
|
21
|
+
* one more step is needed. Nothing from a single sign-on handler means the
|
|
22
|
+
* browser is leaving for the provider.
|
|
23
|
+
*/
|
|
24
|
+
type AuthResult = void | {
|
|
25
|
+
account?: AuthAccount;
|
|
26
|
+
} | {
|
|
27
|
+
secondFactor: SecondFactorMethod[];
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* What a sign-up handler resolves with: nothing or an account when it is ready,
|
|
31
|
+
* `confirmEmail` when the address has to be confirmed first, by the link in the
|
|
32
|
+
* email or by the code it carries.
|
|
33
|
+
*/
|
|
34
|
+
type SignUpResult = void | {
|
|
35
|
+
account?: AuthAccount;
|
|
36
|
+
} | {
|
|
37
|
+
confirmEmail: "link" | "code";
|
|
38
|
+
};
|
|
39
|
+
/** What every call of an Entro Auth client resolves with. */
|
|
40
|
+
interface ClientResponse<Data> {
|
|
41
|
+
data: Data | null;
|
|
42
|
+
error: {
|
|
43
|
+
message?: string | undefined;
|
|
44
|
+
statusText?: string;
|
|
45
|
+
} | null;
|
|
46
|
+
}
|
|
47
|
+
/** What a sign-in call answers with, as far as the handlers read it. */
|
|
48
|
+
interface SignInData {
|
|
49
|
+
user?: {
|
|
50
|
+
name: string;
|
|
51
|
+
email: string;
|
|
52
|
+
image?: string | null | undefined;
|
|
53
|
+
};
|
|
54
|
+
/** Set by the two-factor plugin when the account needs a second step. */
|
|
55
|
+
twoFactorRedirect?: boolean;
|
|
56
|
+
twoFactorMethods?: string[];
|
|
57
|
+
/** Whether the account holds backup codes to fall back on. */
|
|
58
|
+
twoFactorBackupCodes?: boolean;
|
|
59
|
+
}
|
|
60
|
+
/** What the Iris UI `AccountProfile` block draws. */
|
|
61
|
+
interface AccountProfileData {
|
|
62
|
+
name: string;
|
|
63
|
+
email: string;
|
|
64
|
+
photo?: string;
|
|
65
|
+
emailVerified: boolean;
|
|
66
|
+
}
|
|
67
|
+
/** The handler props of the block. */
|
|
68
|
+
interface AccountProfileHandlers {
|
|
69
|
+
onSave: (name: string) => Promise<void>;
|
|
70
|
+
onChangePhoto?: (file: File) => Promise<void>;
|
|
71
|
+
onRemovePhoto?: () => Promise<void>;
|
|
72
|
+
onChangeEmail?: (email: string) => Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
/** The part of an Entro Auth client the block needs. Every client has it. */
|
|
75
|
+
interface AccountProfileClient {
|
|
76
|
+
getSession(): Promise<ClientResponse<{
|
|
77
|
+
user: {
|
|
78
|
+
name: string;
|
|
79
|
+
email: string;
|
|
80
|
+
emailVerified: boolean;
|
|
81
|
+
image?: string | null | undefined;
|
|
82
|
+
};
|
|
83
|
+
}>>;
|
|
84
|
+
updateUser(input: {
|
|
85
|
+
name: string;
|
|
86
|
+
} | {
|
|
87
|
+
image: string | null;
|
|
88
|
+
}): Promise<ClientResponse<unknown>>;
|
|
89
|
+
changeEmail(input: {
|
|
90
|
+
newEmail: string;
|
|
91
|
+
callbackURL?: string;
|
|
92
|
+
}): Promise<ClientResponse<unknown>>;
|
|
93
|
+
}
|
|
94
|
+
interface AccountProfileOptions {
|
|
95
|
+
/**
|
|
96
|
+
* Offer "Change email". The server needs `user.changeEmail.enabled`, which a
|
|
97
|
+
* client cannot tell, so this is yours to say.
|
|
98
|
+
*/
|
|
99
|
+
changeEmail?: boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Store a photo and answer the URL it is served from. The auth server keeps
|
|
102
|
+
* only that URL, and carries it in the session cookie, so a `data:` URL is
|
|
103
|
+
* refused: it would not fit. Adds "Change photo" and "Remove photo".
|
|
104
|
+
*/
|
|
105
|
+
uploadPhoto?: (file: File) => Promise<string>;
|
|
106
|
+
/** Where the confirmation link of a new address lands. */
|
|
107
|
+
callbackURL?: string;
|
|
108
|
+
/** Called after a change, to load the data again. */
|
|
109
|
+
onChange?: () => void;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Loads what the Iris UI `AccountProfile` block draws. Throws when nobody is
|
|
113
|
+
* signed in.
|
|
114
|
+
*/
|
|
115
|
+
export declare function loadAccountProfile(client: AccountProfileClient): Promise<AccountProfileData>;
|
|
116
|
+
/**
|
|
117
|
+
* The handler props that connect an Iris UI `AccountProfile` block to an Entro
|
|
118
|
+
* Auth client.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```tsx
|
|
122
|
+
* const data = await loadAccountProfile(authClient);
|
|
123
|
+
*
|
|
124
|
+
* <AccountProfile
|
|
125
|
+
* {...data}
|
|
126
|
+
* {...accountProfileHandlers(authClient, { changeEmail: true, onChange: reload })}
|
|
127
|
+
* />
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export declare function accountProfileHandlers(client: AccountProfileClient, options?: AccountProfileOptions): AccountProfileHandlers;
|
|
131
|
+
/** A section of the Iris UI `AccountSecurity` block. */
|
|
132
|
+
type AccountSecuritySection = "password" | "two-factor" | "passkeys" | "sessions" | "connections";
|
|
133
|
+
/** A second step the block can turn on. */
|
|
134
|
+
type TwoFactorMethod = "totp" | "passkey";
|
|
135
|
+
/** What the block draws: one member per section that has data. */
|
|
136
|
+
interface AccountSecurityData {
|
|
137
|
+
twoFactor?: boolean;
|
|
138
|
+
/** Set when `twoFactorMethods` is among the options. */
|
|
139
|
+
twoFactorMethods?: TwoFactorMethod[];
|
|
140
|
+
passkeys?: {
|
|
141
|
+
id: string;
|
|
142
|
+
name: string;
|
|
143
|
+
detail?: string;
|
|
144
|
+
}[];
|
|
145
|
+
sessions?: {
|
|
146
|
+
id: string;
|
|
147
|
+
device: string;
|
|
148
|
+
detail?: string;
|
|
149
|
+
current?: boolean;
|
|
150
|
+
}[];
|
|
151
|
+
connections?: {
|
|
152
|
+
provider: AuthProvider;
|
|
153
|
+
account?: string;
|
|
154
|
+
}[];
|
|
155
|
+
}
|
|
156
|
+
/** The handler props of the block. */
|
|
157
|
+
interface AccountSecurityHandlers {
|
|
158
|
+
onChangePassword?: (current: string, next: string) => Promise<void>;
|
|
159
|
+
onEnableTwoFactor?: (password: string, method: TwoFactorMethod) => Promise<{
|
|
160
|
+
uri?: string;
|
|
161
|
+
backupCodes: string[];
|
|
162
|
+
}>;
|
|
163
|
+
onConfirmTwoFactor?: (code: string, method: TwoFactorMethod) => Promise<void>;
|
|
164
|
+
onDisableTwoFactor?: (password: string) => Promise<void>;
|
|
165
|
+
onAddPasskey?: () => Promise<void>;
|
|
166
|
+
onRemovePasskey?: (id: string) => Promise<void>;
|
|
167
|
+
onRevokeSession?: (id: string) => Promise<void>;
|
|
168
|
+
onRevokeOtherSessions?: () => Promise<void>;
|
|
169
|
+
onConnect?: (provider: AuthProvider) => Promise<void>;
|
|
170
|
+
onDisconnect?: (provider: AuthProvider) => Promise<void>;
|
|
171
|
+
}
|
|
172
|
+
interface StoredSession {
|
|
173
|
+
token: string;
|
|
174
|
+
userAgent?: string | null | undefined;
|
|
175
|
+
ipAddress?: string | null | undefined;
|
|
176
|
+
updatedAt: Date | string;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The part of an Entro Auth client the block needs. `twoFactorClient` adds
|
|
180
|
+
* `twoFactor` and `passkeyClient` adds `passkey`.
|
|
181
|
+
*/
|
|
182
|
+
interface AccountSecurityClient {
|
|
183
|
+
getSession(): Promise<ClientResponse<{
|
|
184
|
+
session: {
|
|
185
|
+
token: string;
|
|
186
|
+
};
|
|
187
|
+
user: {
|
|
188
|
+
id: string;
|
|
189
|
+
twoFactorEnabled?: boolean | null | undefined;
|
|
190
|
+
};
|
|
191
|
+
}>>;
|
|
192
|
+
changePassword(input: {
|
|
193
|
+
currentPassword: string;
|
|
194
|
+
newPassword: string;
|
|
195
|
+
revokeOtherSessions?: boolean;
|
|
196
|
+
}): Promise<ClientResponse<unknown>>;
|
|
197
|
+
listSessions(): Promise<ClientResponse<StoredSession[]>>;
|
|
198
|
+
revokeSession(input: {
|
|
199
|
+
token: string;
|
|
200
|
+
}): Promise<ClientResponse<unknown>>;
|
|
201
|
+
revokeOtherSessions(): Promise<ClientResponse<unknown>>;
|
|
202
|
+
listAccounts(): Promise<ClientResponse<{
|
|
203
|
+
id: string;
|
|
204
|
+
providerId: string;
|
|
205
|
+
}[]>>;
|
|
206
|
+
linkSocial(input: {
|
|
207
|
+
provider: string;
|
|
208
|
+
callbackURL?: string;
|
|
209
|
+
}): Promise<ClientResponse<unknown>>;
|
|
210
|
+
unlinkAccount(input: {
|
|
211
|
+
accountId: string;
|
|
212
|
+
}): Promise<ClientResponse<unknown>>;
|
|
213
|
+
twoFactor?: {
|
|
214
|
+
enable(input: {
|
|
215
|
+
password: string;
|
|
216
|
+
method?: TwoFactorMethod;
|
|
217
|
+
}): Promise<ClientResponse<{
|
|
218
|
+
method?: string;
|
|
219
|
+
totpURI?: string;
|
|
220
|
+
backupCodes?: string[];
|
|
221
|
+
}>>;
|
|
222
|
+
verifyTotp(input: {
|
|
223
|
+
code: string;
|
|
224
|
+
}): Promise<ClientResponse<unknown>>;
|
|
225
|
+
disable(input: {
|
|
226
|
+
password: string;
|
|
227
|
+
}): Promise<ClientResponse<unknown>>;
|
|
228
|
+
};
|
|
229
|
+
passkey?: {
|
|
230
|
+
listUserPasskeys(): Promise<ClientResponse<{
|
|
231
|
+
id: string;
|
|
232
|
+
name?: string | undefined;
|
|
233
|
+
createdAt: Date | string;
|
|
234
|
+
}[]>>;
|
|
235
|
+
addPasskey(): Promise<ClientResponse<unknown> | undefined>;
|
|
236
|
+
deletePasskey(input: {
|
|
237
|
+
id: string;
|
|
238
|
+
}): Promise<ClientResponse<unknown>>;
|
|
239
|
+
verifySecondFactor(): Promise<ClientResponse<unknown>>;
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
interface AccountSecurityOptions {
|
|
243
|
+
/**
|
|
244
|
+
* The sections your server supports. They have to be named: a client answers
|
|
245
|
+
* for any plugin, installed or not, so it cannot be asked.
|
|
246
|
+
*/
|
|
247
|
+
sections: AccountSecuritySection[];
|
|
248
|
+
/**
|
|
249
|
+
* The second steps an account can turn on: the authenticator app when left
|
|
250
|
+
* out. `"passkey"` needs `secondFactor: true` on the server's passkey plugin
|
|
251
|
+
* and `passkeyClient`, and is offered to an account that has a passkey.
|
|
252
|
+
*/
|
|
253
|
+
twoFactorMethods?: TwoFactorMethod[];
|
|
254
|
+
/** Called after a change, to load the data again. */
|
|
255
|
+
onChange?: () => void;
|
|
256
|
+
/** Where the browser lands after connecting a provider. */
|
|
257
|
+
callbackURL?: string;
|
|
258
|
+
/** Sign the other devices out when the password changes. */
|
|
259
|
+
revokeOtherSessions?: boolean;
|
|
260
|
+
/** How a date reads beside a session or a passkey. */
|
|
261
|
+
formatDate?: (date: Date) => string;
|
|
262
|
+
/**
|
|
263
|
+
* How a session is named from what its user agent tells: "Chrome on macOS"
|
|
264
|
+
* when left out. Either part is missing when it could not be told.
|
|
265
|
+
*/
|
|
266
|
+
formatDevice?: (device: SessionDevice) => string;
|
|
267
|
+
}
|
|
268
|
+
/** The browser and the system of a session, as far as its user agent tells them. */
|
|
269
|
+
interface SessionDevice {
|
|
270
|
+
browser?: string;
|
|
271
|
+
system?: string;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Loads what the Iris UI `AccountSecurity` block draws, for the sections
|
|
275
|
+
* named. Call it again after a change (see `onChange`).
|
|
276
|
+
*/
|
|
277
|
+
export declare function loadAccountSecurity(client: AccountSecurityClient, options: AccountSecurityOptions): Promise<AccountSecurityData>;
|
|
278
|
+
/**
|
|
279
|
+
* The handler props that connect an Iris UI `AccountSecurity` block to an
|
|
280
|
+
* Entro Auth client, for the sections named.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```tsx
|
|
284
|
+
* const options = { sections: ["password", "sessions"], onChange: reload };
|
|
285
|
+
* const data = await loadAccountSecurity(authClient, options);
|
|
286
|
+
*
|
|
287
|
+
* <AccountSecurity {...data} {...accountSecurityHandlers(authClient, options)} />
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
export declare function accountSecurityHandlers(client: AccountSecurityClient, options: AccountSecurityOptions): AccountSecurityHandlers;
|
|
291
|
+
/** What the Iris UI `OrganizationMembers` block draws. */
|
|
292
|
+
interface OrganizationMembersData {
|
|
293
|
+
members: {
|
|
294
|
+
id: string;
|
|
295
|
+
name: string;
|
|
296
|
+
email: string;
|
|
297
|
+
photo?: string;
|
|
298
|
+
role: string;
|
|
299
|
+
you?: boolean;
|
|
300
|
+
}[];
|
|
301
|
+
invitations?: {
|
|
302
|
+
id: string;
|
|
303
|
+
email: string;
|
|
304
|
+
role: string;
|
|
305
|
+
detail?: string;
|
|
306
|
+
}[];
|
|
307
|
+
}
|
|
308
|
+
/** The handler props of the block. */
|
|
309
|
+
interface OrganizationMembersHandlers {
|
|
310
|
+
onInvite: (email: string, role: string) => Promise<void>;
|
|
311
|
+
onChangeRole: (memberId: string, role: string) => Promise<void>;
|
|
312
|
+
onRemove: (memberId: string) => Promise<void>;
|
|
313
|
+
onCancelInvitation: (invitationId: string) => Promise<void>;
|
|
314
|
+
}
|
|
315
|
+
interface InOrganization {
|
|
316
|
+
organizationId?: string;
|
|
317
|
+
}
|
|
318
|
+
/** The part of an Entro Auth client the block needs: `organizationClient`. */
|
|
319
|
+
interface OrganizationMembersClient {
|
|
320
|
+
getSession(): Promise<ClientResponse<{
|
|
321
|
+
user: {
|
|
322
|
+
id: string;
|
|
323
|
+
};
|
|
324
|
+
}>>;
|
|
325
|
+
organization?: {
|
|
326
|
+
listMembers(input: {
|
|
327
|
+
query: InOrganization;
|
|
328
|
+
}): Promise<ClientResponse<{
|
|
329
|
+
members: {
|
|
330
|
+
id: string;
|
|
331
|
+
role: string;
|
|
332
|
+
user: {
|
|
333
|
+
id: string;
|
|
334
|
+
name: string;
|
|
335
|
+
email: string;
|
|
336
|
+
image?: string | null | undefined;
|
|
337
|
+
};
|
|
338
|
+
}[];
|
|
339
|
+
}>>;
|
|
340
|
+
listInvitations(input: {
|
|
341
|
+
query: InOrganization;
|
|
342
|
+
}): Promise<ClientResponse<{
|
|
343
|
+
id: string;
|
|
344
|
+
email: string;
|
|
345
|
+
role?: string | null | undefined;
|
|
346
|
+
status: string;
|
|
347
|
+
expiresAt: Date | string;
|
|
348
|
+
}[]>>;
|
|
349
|
+
inviteMember(input: InOrganization & {
|
|
350
|
+
email: string;
|
|
351
|
+
role: string | string[];
|
|
352
|
+
}): Promise<ClientResponse<unknown>>;
|
|
353
|
+
updateMemberRole(input: InOrganization & {
|
|
354
|
+
memberId: string;
|
|
355
|
+
role: string | string[];
|
|
356
|
+
}): Promise<ClientResponse<unknown>>;
|
|
357
|
+
removeMember(input: InOrganization & {
|
|
358
|
+
memberIdOrEmail: string;
|
|
359
|
+
}): Promise<ClientResponse<unknown>>;
|
|
360
|
+
cancelInvitation(input: {
|
|
361
|
+
invitationId: string;
|
|
362
|
+
}): Promise<ClientResponse<unknown>>;
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
interface OrganizationMembersOptions {
|
|
366
|
+
/** The organization to show. The person's active organization by default. */
|
|
367
|
+
organizationId?: string;
|
|
368
|
+
/**
|
|
369
|
+
* Load the pending invitations. Leave it off for people the server does not
|
|
370
|
+
* let read them, or the load is refused.
|
|
371
|
+
*/
|
|
372
|
+
invitations?: boolean;
|
|
373
|
+
/** Called after a change, to load the data again. */
|
|
374
|
+
onChange?: () => void;
|
|
375
|
+
/** How the day an invitation runs out reads beside it. */
|
|
376
|
+
formatExpiry?: (date: Date) => string;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Loads what the Iris UI `OrganizationMembers` block draws. Call it again
|
|
380
|
+
* after a change (see `onChange`).
|
|
381
|
+
*/
|
|
382
|
+
export declare function loadOrganizationMembers(client: OrganizationMembersClient, options?: OrganizationMembersOptions): Promise<OrganizationMembersData>;
|
|
383
|
+
/**
|
|
384
|
+
* The handler props that connect an Iris UI `OrganizationMembers` block to an
|
|
385
|
+
* Entro Auth client. Pass them to the block only for people who may manage the
|
|
386
|
+
* organization; the server refuses the others either way.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```tsx
|
|
390
|
+
* const options = { invitations: true, onChange: reload };
|
|
391
|
+
* const data = await loadOrganizationMembers(authClient, options);
|
|
392
|
+
*
|
|
393
|
+
* <OrganizationMembers
|
|
394
|
+
* {...data}
|
|
395
|
+
* {...organizationMembersHandlers(authClient, options)}
|
|
396
|
+
* />
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
export declare function organizationMembersHandlers(client: OrganizationMembersClient, options?: OrganizationMembersOptions): OrganizationMembersHandlers;
|
|
400
|
+
/** What the Iris UI `OrganizationSwitcher` draws. */
|
|
401
|
+
interface OrganizationSwitcherData {
|
|
402
|
+
organizations: {
|
|
403
|
+
id: string;
|
|
404
|
+
name: string;
|
|
405
|
+
}[];
|
|
406
|
+
/** The organization in use; `null` outside any organization. */
|
|
407
|
+
activeId: string | null;
|
|
408
|
+
}
|
|
409
|
+
/** The handler prop of the switcher. */
|
|
410
|
+
interface OrganizationSwitcherHandlers {
|
|
411
|
+
onSwitch: (id: string | null) => Promise<void>;
|
|
412
|
+
}
|
|
413
|
+
/** The part of an Entro Auth client the switcher needs: `organizationClient`. */
|
|
414
|
+
interface OrganizationSwitcherClient {
|
|
415
|
+
getSession(): Promise<ClientResponse<{
|
|
416
|
+
session: {
|
|
417
|
+
activeOrganizationId?: string | null | undefined;
|
|
418
|
+
};
|
|
419
|
+
}>>;
|
|
420
|
+
organization?: {
|
|
421
|
+
list(): Promise<ClientResponse<{
|
|
422
|
+
id: string;
|
|
423
|
+
name: string;
|
|
424
|
+
}[]>>;
|
|
425
|
+
setActive(input: {
|
|
426
|
+
organizationId: string | null;
|
|
427
|
+
}): Promise<ClientResponse<unknown>>;
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Loads what the Iris UI `OrganizationSwitcher` draws: the organizations the
|
|
432
|
+
* person belongs to, and the one their session is in.
|
|
433
|
+
*/
|
|
434
|
+
export declare function loadOrganizations(client: OrganizationSwitcherClient): Promise<OrganizationSwitcherData>;
|
|
435
|
+
/**
|
|
436
|
+
* The handler prop that connects an Iris UI `OrganizationSwitcher` to an Entro
|
|
437
|
+
* Auth client. Switching changes the session's active organization, which is
|
|
438
|
+
* what the organization endpoints act on by default.
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```tsx
|
|
442
|
+
* const data = await loadOrganizations(authClient);
|
|
443
|
+
*
|
|
444
|
+
* <OrganizationSwitcher
|
|
445
|
+
* {...data}
|
|
446
|
+
* {...organizationSwitcherHandlers(authClient, { onChange: reload })}
|
|
447
|
+
* />
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
450
|
+
export declare function organizationSwitcherHandlers(client: OrganizationSwitcherClient, options?: {
|
|
451
|
+
onChange?: () => void;
|
|
452
|
+
}): OrganizationSwitcherHandlers;
|
|
453
|
+
/** The part of an Entro Auth client the handler calls. Every client has it. */
|
|
454
|
+
interface ResetPasswordClient {
|
|
455
|
+
resetPassword(input: {
|
|
456
|
+
newPassword: string;
|
|
457
|
+
token: string;
|
|
458
|
+
}): Promise<ClientResponse<unknown>>;
|
|
459
|
+
}
|
|
460
|
+
/** The props of the Iris UI `ResetPassword` block. */
|
|
461
|
+
interface ResetPasswordHandlers {
|
|
462
|
+
/** The link carried no token: it was used, or it ran out. */
|
|
463
|
+
expired: boolean;
|
|
464
|
+
onSubmit: (password: string) => Promise<void>;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* The props that connect an Iris UI `ResetPassword` block to an Entro Auth
|
|
468
|
+
* client, on the page named as `resetPasswordURL` in `signInHandlers`. The
|
|
469
|
+
* server sends the browser there with a `token` query parameter, or without one
|
|
470
|
+
* when the link no longer works.
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```tsx
|
|
474
|
+
* const token = new URLSearchParams(location.search).get("token");
|
|
475
|
+
*
|
|
476
|
+
* <ResetPassword
|
|
477
|
+
* {...resetPasswordHandlers(authClient, token)}
|
|
478
|
+
* onSignIn={() => router.push("/sign-in")}
|
|
479
|
+
* />
|
|
480
|
+
* ```
|
|
481
|
+
*/
|
|
482
|
+
export declare function resetPasswordHandlers(client: ResetPasswordClient, token: string | null | undefined): ResetPasswordHandlers;
|
|
483
|
+
type Credentials<Key extends string> = Record<Key | "password", string>;
|
|
484
|
+
/**
|
|
485
|
+
* The part of an Entro Auth client the handlers call. Every client has the
|
|
486
|
+
* required members. Client plugins add the optional ones: `emailOTPClient` the
|
|
487
|
+
* emailed code, `usernameClient` usernames, `magicLinkClient` the sign-in
|
|
488
|
+
* link, `passkeyClient` passkeys and `twoFactorClient` the second step.
|
|
489
|
+
*/
|
|
490
|
+
interface SignInClient {
|
|
491
|
+
emailOtp?: {
|
|
492
|
+
sendVerificationOtp(input: {
|
|
493
|
+
email: string;
|
|
494
|
+
type: "sign-in";
|
|
495
|
+
}): Promise<ClientResponse<unknown>>;
|
|
496
|
+
};
|
|
497
|
+
signIn: {
|
|
498
|
+
emailOtp?(input: {
|
|
499
|
+
email: string;
|
|
500
|
+
otp: string;
|
|
501
|
+
}): Promise<ClientResponse<SignInData>>;
|
|
502
|
+
email(input: Credentials<"email">): Promise<ClientResponse<SignInData>>;
|
|
503
|
+
username?(input: Credentials<"username">): Promise<ClientResponse<SignInData>>;
|
|
504
|
+
magicLink?(input: {
|
|
505
|
+
email: string;
|
|
506
|
+
callbackURL?: string;
|
|
507
|
+
}): Promise<ClientResponse<unknown>>;
|
|
508
|
+
social(input: {
|
|
509
|
+
provider: string;
|
|
510
|
+
callbackURL?: string;
|
|
511
|
+
}): Promise<ClientResponse<unknown>>;
|
|
512
|
+
passkey?(): Promise<ClientResponse<SignInData>>;
|
|
513
|
+
};
|
|
514
|
+
requestPasswordReset(input: {
|
|
515
|
+
email: string;
|
|
516
|
+
redirectTo?: string;
|
|
517
|
+
}): Promise<ClientResponse<unknown>>;
|
|
518
|
+
twoFactor?: {
|
|
519
|
+
verifyTotp(input: {
|
|
520
|
+
code: string;
|
|
521
|
+
}): Promise<ClientResponse<SignInData>>;
|
|
522
|
+
verifyOtp(input: {
|
|
523
|
+
code: string;
|
|
524
|
+
}): Promise<ClientResponse<SignInData>>;
|
|
525
|
+
verifyBackupCode(input: {
|
|
526
|
+
code: string;
|
|
527
|
+
}): Promise<ClientResponse<SignInData>>;
|
|
528
|
+
sendOtp(): Promise<ClientResponse<unknown>>;
|
|
529
|
+
};
|
|
530
|
+
/** `passkeyClient`: a passkey as the second step. */
|
|
531
|
+
passkey?: {
|
|
532
|
+
verifySecondFactor(): Promise<ClientResponse<SignInData>>;
|
|
533
|
+
};
|
|
534
|
+
signOut(): Promise<ClientResponse<unknown>>;
|
|
535
|
+
}
|
|
536
|
+
/** A way in the `SignIn` block can offer. */
|
|
537
|
+
type SignInMethod = "email-code" | "password" | "magic-link" | "passkey" | "social";
|
|
538
|
+
interface SignInHandlerOptions {
|
|
539
|
+
/**
|
|
540
|
+
* The ways in your server has set up. The block offers these and no others.
|
|
541
|
+
* They have to be named: a client answers for any plugin, installed or not,
|
|
542
|
+
* so it cannot be asked.
|
|
543
|
+
*/
|
|
544
|
+
methods: SignInMethod[];
|
|
545
|
+
/** Where the browser lands after single sign-on or a sign-in link. */
|
|
546
|
+
callbackURL?: string;
|
|
547
|
+
/**
|
|
548
|
+
* Your page that takes the reset token and the new password. With the
|
|
549
|
+
* `password` method, adds "Forgot password?".
|
|
550
|
+
*/
|
|
551
|
+
resetPasswordURL?: string;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* The handler props of the Iris UI `SignIn` block. The block offers only the
|
|
555
|
+
* ways in that have a handler here.
|
|
556
|
+
*/
|
|
557
|
+
interface SignInHandlers {
|
|
558
|
+
onSignOut: () => Promise<void>;
|
|
559
|
+
onSendCode?: (email: string) => Promise<void>;
|
|
560
|
+
onVerifyCode?: (email: string, code: string) => Promise<AuthResult>;
|
|
561
|
+
onPassword?: (identifier: string, password: string) => Promise<AuthResult>;
|
|
562
|
+
onForgotPassword?: (email: string) => Promise<void>;
|
|
563
|
+
onSendLink?: (email: string) => Promise<void>;
|
|
564
|
+
onPasskey?: () => Promise<AuthResult>;
|
|
565
|
+
onProvider?: (provider: AuthProvider) => Promise<AuthResult>;
|
|
566
|
+
onSecondFactor?: (code: string, method: SecondFactorMethod) => Promise<AuthResult>;
|
|
567
|
+
onSendSecondFactor?: () => Promise<void>;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* The handler props that connect an Iris UI `SignIn` block to an Entro Auth
|
|
571
|
+
* client, one set per method you name. Pass the block the `providers` your
|
|
572
|
+
* server has configured as well.
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```tsx
|
|
576
|
+
* const handlers = signInHandlers(authClient, {
|
|
577
|
+
* methods: ["password", "email-code", "social"],
|
|
578
|
+
* resetPasswordURL: "/reset-password",
|
|
579
|
+
* callbackURL: "/dashboard",
|
|
580
|
+
* });
|
|
581
|
+
*
|
|
582
|
+
* <SignIn
|
|
583
|
+
* {...handlers}
|
|
584
|
+
* providers={["Google", "Facebook"]}
|
|
585
|
+
* onSignIn={() => router.push("/dashboard")}
|
|
586
|
+
* />
|
|
587
|
+
* ```
|
|
588
|
+
*/
|
|
589
|
+
export declare function signInHandlers(client: SignInClient, options: SignInHandlerOptions): SignInHandlers;
|
|
590
|
+
/** What the Iris UI `SignupForm` block collects. */
|
|
591
|
+
interface SignupDetails {
|
|
592
|
+
name: string;
|
|
593
|
+
email: string;
|
|
594
|
+
password: string;
|
|
595
|
+
productUpdates: boolean;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* The part of an Entro Auth client the handlers call. `emailOTPClient` adds
|
|
599
|
+
* `emailOtp`, which confirming by code needs.
|
|
600
|
+
*/
|
|
601
|
+
interface SignUpClient {
|
|
602
|
+
signUp: {
|
|
603
|
+
email(input: {
|
|
604
|
+
name: string;
|
|
605
|
+
email: string;
|
|
606
|
+
password: string;
|
|
607
|
+
} & Record<string, unknown>): Promise<ClientResponse<{
|
|
608
|
+
token: string | null;
|
|
609
|
+
}>>;
|
|
610
|
+
};
|
|
611
|
+
signIn: {
|
|
612
|
+
social(input: {
|
|
613
|
+
provider: string;
|
|
614
|
+
callbackURL?: string;
|
|
615
|
+
}): Promise<ClientResponse<unknown>>;
|
|
616
|
+
};
|
|
617
|
+
sendVerificationEmail(input: {
|
|
618
|
+
email: string;
|
|
619
|
+
callbackURL?: string;
|
|
620
|
+
}): Promise<ClientResponse<unknown>>;
|
|
621
|
+
emailOtp?: {
|
|
622
|
+
verifyEmail(input: {
|
|
623
|
+
email: string;
|
|
624
|
+
otp: string;
|
|
625
|
+
}): Promise<ClientResponse<unknown>>;
|
|
626
|
+
sendVerificationOtp(input: {
|
|
627
|
+
email: string;
|
|
628
|
+
type: "email-verification";
|
|
629
|
+
}): Promise<ClientResponse<unknown>>;
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
interface SignUpHandlerOptions {
|
|
633
|
+
/**
|
|
634
|
+
* How a new address is confirmed, when the server has
|
|
635
|
+
* `requireEmailVerification` on: by the `link` of `sendVerificationEmail`, or
|
|
636
|
+
* by the `code` the `emailOTP` plugin sends on sign up. Leave it out when
|
|
637
|
+
* accounts are ready at once.
|
|
638
|
+
*/
|
|
639
|
+
confirmEmail?: "link" | "code";
|
|
640
|
+
/** Offer single sign-on. */
|
|
641
|
+
social?: boolean;
|
|
642
|
+
/** Where the browser lands after single sign-on or the confirmation link. */
|
|
643
|
+
callbackURL?: string;
|
|
644
|
+
/**
|
|
645
|
+
* The user fields your server stores beyond name, email and password, such
|
|
646
|
+
* as the product updates choice. Each needs a matching `user.additionalFields`
|
|
647
|
+
* entry on the server.
|
|
648
|
+
*/
|
|
649
|
+
fields?: (details: SignupDetails) => Record<string, unknown>;
|
|
650
|
+
}
|
|
651
|
+
/** The handler props of the Iris UI `SignupForm` block. */
|
|
652
|
+
interface SignUpHandlers {
|
|
653
|
+
onSubmit: (details: SignupDetails) => Promise<SignUpResult>;
|
|
654
|
+
onResendConfirmation?: (email: string) => Promise<void>;
|
|
655
|
+
onConfirmCode?: (email: string, code: string) => Promise<void>;
|
|
656
|
+
onProvider?: (provider: AuthProvider) => Promise<SignUpResult>;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* The handler props that connect an Iris UI `SignupForm` block to an Entro
|
|
660
|
+
* Auth client.
|
|
661
|
+
*
|
|
662
|
+
* @example
|
|
663
|
+
* ```tsx
|
|
664
|
+
* const handlers = signUpHandlers(authClient, {
|
|
665
|
+
* confirmEmail: "code",
|
|
666
|
+
* social: true,
|
|
667
|
+
* callbackURL: "/welcome",
|
|
668
|
+
* });
|
|
669
|
+
*
|
|
670
|
+
* <SignupForm {...handlers} providers={["Google", "Facebook"]} />
|
|
671
|
+
* ```
|
|
672
|
+
*/
|
|
673
|
+
export declare function signUpHandlers(client: SignUpClient, options?: SignUpHandlerOptions): SignUpHandlers;
|
|
674
|
+
export type { AccountProfileClient, AccountProfileData, AccountProfileHandlers, AccountProfileOptions, AccountSecurityClient, AccountSecurityData, AccountSecurityHandlers, AccountSecurityOptions, AccountSecuritySection, AuthAccount, AuthProvider, AuthResult, OrganizationMembersClient, OrganizationMembersData, OrganizationMembersHandlers, OrganizationMembersOptions, OrganizationSwitcherClient, OrganizationSwitcherData, OrganizationSwitcherHandlers, ResetPasswordClient, ResetPasswordHandlers, SecondFactorMethod, SessionDevice, SignInClient, SignInHandlerOptions, SignInHandlers, SignInMethod, SignUpClient, SignUpHandlerOptions, SignUpHandlers, SignUpResult, SignupDetails, TwoFactorMethod };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
// src/response.ts
|
|
2
|
+
/** The data of a client call, or its error thrown for the block to show. */
|
|
3
|
+
function unwrap(response) {
|
|
4
|
+
if (response.error) throw new Error(response.error.message || response.error.statusText || "Request failed");
|
|
5
|
+
return response.data;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Reads a sign-in answer: one more step, or the account that signed in. An
|
|
9
|
+
* account without a name, as a first sign in with an emailed code creates, is
|
|
10
|
+
* left to the block, which names it from the address.
|
|
11
|
+
*/
|
|
12
|
+
function toResult(data) {
|
|
13
|
+
if (data?.twoFactorRedirect) {
|
|
14
|
+
const answered = data.twoFactorMethods ?? [];
|
|
15
|
+
const offered = [
|
|
16
|
+
"passkey",
|
|
17
|
+
"totp",
|
|
18
|
+
"otp"
|
|
19
|
+
].filter((method) => answered.includes(method));
|
|
20
|
+
return { secondFactor: data.twoFactorBackupCodes ? [...offered, "backup-code"] : offered };
|
|
21
|
+
}
|
|
22
|
+
const user = data?.user;
|
|
23
|
+
if (!user?.name) return {};
|
|
24
|
+
return { account: {
|
|
25
|
+
name: user.name,
|
|
26
|
+
email: user.email,
|
|
27
|
+
...user.image ? { photo: user.image } : {}
|
|
28
|
+
} };
|
|
29
|
+
}
|
|
30
|
+
// src/account-profile.ts
|
|
31
|
+
/**
|
|
32
|
+
* Loads what the Iris UI `AccountProfile` block draws. Throws when nobody is
|
|
33
|
+
* signed in.
|
|
34
|
+
*/
|
|
35
|
+
async function loadAccountProfile(client) {
|
|
36
|
+
const user = unwrap(await client.getSession())?.user;
|
|
37
|
+
if (!user) throw new Error("Nobody is signed in.");
|
|
38
|
+
return {
|
|
39
|
+
name: user.name,
|
|
40
|
+
email: user.email,
|
|
41
|
+
emailVerified: user.emailVerified,
|
|
42
|
+
...user.image ? { photo: user.image } : {}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The handler props that connect an Iris UI `AccountProfile` block to an Entro
|
|
47
|
+
* Auth client.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```tsx
|
|
51
|
+
* const data = await loadAccountProfile(authClient);
|
|
52
|
+
*
|
|
53
|
+
* <AccountProfile
|
|
54
|
+
* {...data}
|
|
55
|
+
* {...accountProfileHandlers(authClient, { changeEmail: true, onChange: reload })}
|
|
56
|
+
* />
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
function accountProfileHandlers(client, options = {}) {
|
|
60
|
+
const { callbackURL, onChange, uploadPhoto } = options;
|
|
61
|
+
const handlers = { async onSave(name) {
|
|
62
|
+
unwrap(await client.updateUser({ name }));
|
|
63
|
+
onChange?.();
|
|
64
|
+
} };
|
|
65
|
+
if (uploadPhoto) {
|
|
66
|
+
handlers.onChangePhoto = async (file) => {
|
|
67
|
+
const url = await uploadPhoto(file);
|
|
68
|
+
if (url.startsWith("data:")) throw new Error("uploadPhoto has to answer the URL of the stored photo.");
|
|
69
|
+
unwrap(await client.updateUser({ image: url }));
|
|
70
|
+
onChange?.();
|
|
71
|
+
};
|
|
72
|
+
handlers.onRemovePhoto = async () => {
|
|
73
|
+
unwrap(await client.updateUser({ image: null }));
|
|
74
|
+
onChange?.();
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (options.changeEmail) handlers.onChangeEmail = async (email) => {
|
|
78
|
+
unwrap(await client.changeEmail({
|
|
79
|
+
newEmail: email,
|
|
80
|
+
...callbackURL ? { callbackURL } : {}
|
|
81
|
+
}));
|
|
82
|
+
onChange?.();
|
|
83
|
+
};
|
|
84
|
+
return handlers;
|
|
85
|
+
}
|
|
86
|
+
// src/sign-in.ts
|
|
87
|
+
/** The server's name for each provider the blocks can show. */
|
|
88
|
+
const providerIds = {
|
|
89
|
+
Google: "google",
|
|
90
|
+
Apple: "apple",
|
|
91
|
+
GitHub: "github",
|
|
92
|
+
Facebook: "facebook"
|
|
93
|
+
};
|
|
94
|
+
/** A client member a method needs, or the plugin to add when it is missing. */
|
|
95
|
+
function need(member, plugin) {
|
|
96
|
+
if (!member) throw new Error(`Add ${plugin}() to the auth client's plugins.`);
|
|
97
|
+
return member;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The handler props that connect an Iris UI `SignIn` block to an Entro Auth
|
|
101
|
+
* client, one set per method you name. Pass the block the `providers` your
|
|
102
|
+
* server has configured as well.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```tsx
|
|
106
|
+
* const handlers = signInHandlers(authClient, {
|
|
107
|
+
* methods: ["password", "email-code", "social"],
|
|
108
|
+
* resetPasswordURL: "/reset-password",
|
|
109
|
+
* callbackURL: "/dashboard",
|
|
110
|
+
* });
|
|
111
|
+
*
|
|
112
|
+
* <SignIn
|
|
113
|
+
* {...handlers}
|
|
114
|
+
* providers={["Google", "Facebook"]}
|
|
115
|
+
* onSignIn={() => router.push("/dashboard")}
|
|
116
|
+
* />
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
function signInHandlers(client, options) {
|
|
120
|
+
const { signIn } = client;
|
|
121
|
+
const { callbackURL, resetPasswordURL } = options;
|
|
122
|
+
const offers = (method) => options.methods.includes(method);
|
|
123
|
+
const withCallback = callbackURL ? { callbackURL } : {};
|
|
124
|
+
const handlers = { async onSignOut() {
|
|
125
|
+
unwrap(await client.signOut());
|
|
126
|
+
} };
|
|
127
|
+
if (offers("email-code")) {
|
|
128
|
+
const emailOtp = need(client.emailOtp, "emailOTPClient");
|
|
129
|
+
const verifyCode = need(signIn.emailOtp, "emailOTPClient");
|
|
130
|
+
handlers.onSendCode = async (email) => {
|
|
131
|
+
unwrap(await emailOtp.sendVerificationOtp({
|
|
132
|
+
email,
|
|
133
|
+
type: "sign-in"
|
|
134
|
+
}));
|
|
135
|
+
};
|
|
136
|
+
handlers.onVerifyCode = async (email, code) => toResult(unwrap(await verifyCode({
|
|
137
|
+
email,
|
|
138
|
+
otp: code
|
|
139
|
+
})));
|
|
140
|
+
}
|
|
141
|
+
if (offers("password")) {
|
|
142
|
+
handlers.onPassword = async (identifier, password) => {
|
|
143
|
+
if (identifier.includes("@")) return toResult(unwrap(await signIn.email({
|
|
144
|
+
email: identifier,
|
|
145
|
+
password
|
|
146
|
+
})));
|
|
147
|
+
return toResult(unwrap(await need(signIn.username, "usernameClient")({
|
|
148
|
+
username: identifier,
|
|
149
|
+
password
|
|
150
|
+
})));
|
|
151
|
+
};
|
|
152
|
+
handlers.onSecondFactor = async (code, method) => {
|
|
153
|
+
if (method === "passkey") return toResult(unwrap(await need(client.passkey, "passkeyClient").verifySecondFactor()));
|
|
154
|
+
const twoFactor = need(client.twoFactor, "twoFactorClient");
|
|
155
|
+
return toResult(unwrap(method === "totp" ? await twoFactor.verifyTotp({ code }) : method === "otp" ? await twoFactor.verifyOtp({ code }) : await twoFactor.verifyBackupCode({ code })));
|
|
156
|
+
};
|
|
157
|
+
handlers.onSendSecondFactor = async () => {
|
|
158
|
+
unwrap(await need(client.twoFactor, "twoFactorClient").sendOtp());
|
|
159
|
+
};
|
|
160
|
+
if (resetPasswordURL) handlers.onForgotPassword = async (email) => {
|
|
161
|
+
unwrap(await client.requestPasswordReset({
|
|
162
|
+
email,
|
|
163
|
+
redirectTo: resetPasswordURL
|
|
164
|
+
}));
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
if (offers("magic-link")) {
|
|
168
|
+
const sendLink = need(signIn.magicLink, "magicLinkClient");
|
|
169
|
+
handlers.onSendLink = async (email) => {
|
|
170
|
+
unwrap(await sendLink({
|
|
171
|
+
email,
|
|
172
|
+
...withCallback
|
|
173
|
+
}));
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (offers("passkey")) {
|
|
177
|
+
const withPasskey = need(signIn.passkey, "passkeyClient");
|
|
178
|
+
handlers.onPasskey = async () => toResult(unwrap(await withPasskey()));
|
|
179
|
+
}
|
|
180
|
+
if (offers("social")) handlers.onProvider = async (provider) => {
|
|
181
|
+
unwrap(await signIn.social({
|
|
182
|
+
provider: providerIds[provider],
|
|
183
|
+
...withCallback
|
|
184
|
+
}));
|
|
185
|
+
};
|
|
186
|
+
return handlers;
|
|
187
|
+
}
|
|
188
|
+
// src/account-security.ts
|
|
189
|
+
const browsers = [
|
|
190
|
+
[/Edg\//, "Edge"],
|
|
191
|
+
[/OPR\//, "Opera"],
|
|
192
|
+
[/Firefox\//, "Firefox"],
|
|
193
|
+
[/Chrome\//, "Chrome"],
|
|
194
|
+
[/Safari\//, "Safari"]
|
|
195
|
+
];
|
|
196
|
+
const systems = [
|
|
197
|
+
[/iPhone/, "iPhone"],
|
|
198
|
+
[/iPad/, "iPad"],
|
|
199
|
+
[/Android/, "Android"],
|
|
200
|
+
[/Windows/, "Windows"],
|
|
201
|
+
[/Mac OS X/, "macOS"],
|
|
202
|
+
[/Linux/, "Linux"]
|
|
203
|
+
];
|
|
204
|
+
/** The browser and the system in a user agent, as far as they can be told. */
|
|
205
|
+
function deviceOf(userAgent) {
|
|
206
|
+
const find = (table) => table.find(([pattern]) => pattern.test(userAgent ?? ""))?.[1];
|
|
207
|
+
return {
|
|
208
|
+
browser: find(browsers),
|
|
209
|
+
system: find(systems)
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/** "Chrome on macOS". */
|
|
213
|
+
function deviceName({ browser, system }) {
|
|
214
|
+
if (browser && system) return `${browser} on ${system}`;
|
|
215
|
+
return browser ?? system ?? "Unknown device";
|
|
216
|
+
}
|
|
217
|
+
const providerNames = new Map(Object.entries(providerIds).map(([name, id]) => [id, name]));
|
|
218
|
+
/**
|
|
219
|
+
* Loads what the Iris UI `AccountSecurity` block draws, for the sections
|
|
220
|
+
* named. Call it again after a change (see `onChange`).
|
|
221
|
+
*/
|
|
222
|
+
async function loadAccountSecurity(client, options) {
|
|
223
|
+
const shows = (section) => options.sections.includes(section);
|
|
224
|
+
const formatDate = options.formatDate ?? ((date) => date.toLocaleDateString());
|
|
225
|
+
const formatDevice = options.formatDevice ?? deviceName;
|
|
226
|
+
const data = {};
|
|
227
|
+
const current = unwrap(await client.getSession());
|
|
228
|
+
const listPasskeys = async () => unwrap(await need(client.passkey, "passkeyClient").listUserPasskeys()) ?? [];
|
|
229
|
+
const offersPasskey = shows("two-factor") && !!options.twoFactorMethods?.includes("passkey");
|
|
230
|
+
const passkeys = shows("passkeys") || offersPasskey ? await listPasskeys() : [];
|
|
231
|
+
if (shows("two-factor")) {
|
|
232
|
+
data.twoFactor = !!current?.user.twoFactorEnabled;
|
|
233
|
+
if (options.twoFactorMethods) data.twoFactorMethods = options.twoFactorMethods.filter((method) => method !== "passkey" || passkeys.length > 0);
|
|
234
|
+
}
|
|
235
|
+
if (shows("passkeys")) data.passkeys = passkeys.map(({ id, name, createdAt }) => ({
|
|
236
|
+
id,
|
|
237
|
+
name: name || "Passkey",
|
|
238
|
+
detail: formatDate(new Date(createdAt))
|
|
239
|
+
}));
|
|
240
|
+
if (shows("sessions")) data.sessions = (unwrap(await client.listSessions()) ?? []).map((session) => ({
|
|
241
|
+
id: session.token,
|
|
242
|
+
device: formatDevice(deviceOf(session.userAgent)),
|
|
243
|
+
detail: [session.ipAddress, formatDate(new Date(session.updatedAt))].filter(Boolean).join(" · "),
|
|
244
|
+
current: session.token === current?.session.token
|
|
245
|
+
}));
|
|
246
|
+
if (shows("connections")) data.connections = (unwrap(await client.listAccounts()) ?? []).flatMap(({ providerId }) => {
|
|
247
|
+
const provider = providerNames.get(providerId);
|
|
248
|
+
return provider ? [{ provider }] : [];
|
|
249
|
+
});
|
|
250
|
+
return data;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* The handler props that connect an Iris UI `AccountSecurity` block to an
|
|
254
|
+
* Entro Auth client, for the sections named.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```tsx
|
|
258
|
+
* const options = { sections: ["password", "sessions"], onChange: reload };
|
|
259
|
+
* const data = await loadAccountSecurity(authClient, options);
|
|
260
|
+
*
|
|
261
|
+
* <AccountSecurity {...data} {...accountSecurityHandlers(authClient, options)} />
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
function accountSecurityHandlers(client, options) {
|
|
265
|
+
const shows = (section) => options.sections.includes(section);
|
|
266
|
+
const { callbackURL, onChange } = options;
|
|
267
|
+
/** Runs a client call, throws its error, then asks for the data again. */
|
|
268
|
+
async function change(call) {
|
|
269
|
+
const response = await call;
|
|
270
|
+
const data = response ? unwrap(response) : null;
|
|
271
|
+
onChange?.();
|
|
272
|
+
return data;
|
|
273
|
+
}
|
|
274
|
+
const handlers = {};
|
|
275
|
+
if (shows("password")) handlers.onChangePassword = async (current, next) => {
|
|
276
|
+
await change(client.changePassword({
|
|
277
|
+
currentPassword: current,
|
|
278
|
+
newPassword: next,
|
|
279
|
+
...options.revokeOtherSessions ? { revokeOtherSessions: true } : {}
|
|
280
|
+
}));
|
|
281
|
+
};
|
|
282
|
+
if (shows("two-factor")) {
|
|
283
|
+
const twoFactor = need(client.twoFactor, "twoFactorClient");
|
|
284
|
+
handlers.onEnableTwoFactor = async (password, method) => {
|
|
285
|
+
if (method === "passkey") return { backupCodes: unwrap(await twoFactor.enable({
|
|
286
|
+
password,
|
|
287
|
+
method
|
|
288
|
+
}))?.backupCodes ?? [] };
|
|
289
|
+
const setup = unwrap(await twoFactor.enable({ password }));
|
|
290
|
+
if (!setup?.totpURI) throw new Error("The server has no authenticator app setup.");
|
|
291
|
+
return {
|
|
292
|
+
uri: setup.totpURI,
|
|
293
|
+
backupCodes: setup.backupCodes ?? []
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
handlers.onConfirmTwoFactor = async (code, method) => {
|
|
297
|
+
if (method === "passkey") {
|
|
298
|
+
await change(need(client.passkey, "passkeyClient").verifySecondFactor());
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
await change(twoFactor.verifyTotp({ code }));
|
|
302
|
+
};
|
|
303
|
+
handlers.onDisableTwoFactor = async (password) => {
|
|
304
|
+
await change(twoFactor.disable({ password }));
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
if (shows("passkeys")) {
|
|
308
|
+
const passkey = need(client.passkey, "passkeyClient");
|
|
309
|
+
handlers.onAddPasskey = async () => {
|
|
310
|
+
await change(passkey.addPasskey());
|
|
311
|
+
};
|
|
312
|
+
handlers.onRemovePasskey = async (id) => {
|
|
313
|
+
await change(passkey.deletePasskey({ id }));
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
if (shows("sessions")) {
|
|
317
|
+
handlers.onRevokeSession = async (id) => {
|
|
318
|
+
await change(client.revokeSession({ token: id }));
|
|
319
|
+
};
|
|
320
|
+
handlers.onRevokeOtherSessions = async () => {
|
|
321
|
+
await change(client.revokeOtherSessions());
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
if (shows("connections")) {
|
|
325
|
+
handlers.onConnect = async (provider) => {
|
|
326
|
+
unwrap(await client.linkSocial({
|
|
327
|
+
provider: providerIds[provider],
|
|
328
|
+
...callbackURL ? { callbackURL } : {}
|
|
329
|
+
}));
|
|
330
|
+
};
|
|
331
|
+
handlers.onDisconnect = async (provider) => {
|
|
332
|
+
const account = (unwrap(await client.listAccounts()) ?? []).find(({ providerId }) => providerId === providerIds[provider]);
|
|
333
|
+
if (!account) throw new Error(`${provider} is not connected.`);
|
|
334
|
+
await change(client.unlinkAccount({ accountId: account.id }));
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
return handlers;
|
|
338
|
+
}
|
|
339
|
+
// src/organization-members.ts
|
|
340
|
+
/**
|
|
341
|
+
* Loads what the Iris UI `OrganizationMembers` block draws. Call it again
|
|
342
|
+
* after a change (see `onChange`).
|
|
343
|
+
*/
|
|
344
|
+
async function loadOrganizationMembers(client, options = {}) {
|
|
345
|
+
const organization = need(client.organization, "organizationClient");
|
|
346
|
+
const query = options.organizationId ? { organizationId: options.organizationId } : {};
|
|
347
|
+
const formatExpiry = options.formatExpiry ?? ((date) => `Expires ${date.toLocaleDateString()}`);
|
|
348
|
+
const viewer = unwrap(await client.getSession())?.user.id;
|
|
349
|
+
const data = { members: (unwrap(await organization.listMembers({ query }))?.members ?? []).map(({ id, role, user }) => ({
|
|
350
|
+
id,
|
|
351
|
+
name: user.name,
|
|
352
|
+
email: user.email,
|
|
353
|
+
role,
|
|
354
|
+
...user.image ? { photo: user.image } : {},
|
|
355
|
+
...user.id === viewer ? { you: true } : {}
|
|
356
|
+
})) };
|
|
357
|
+
if (options.invitations) data.invitations = (unwrap(await organization.listInvitations({ query })) ?? []).filter((invitation) => invitation.status === "pending").map(({ id, email, role, expiresAt }) => ({
|
|
358
|
+
id,
|
|
359
|
+
email,
|
|
360
|
+
role: role ?? "member",
|
|
361
|
+
detail: formatExpiry(new Date(expiresAt))
|
|
362
|
+
}));
|
|
363
|
+
return data;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* The handler props that connect an Iris UI `OrganizationMembers` block to an
|
|
367
|
+
* Entro Auth client. Pass them to the block only for people who may manage the
|
|
368
|
+
* organization; the server refuses the others either way.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* ```tsx
|
|
372
|
+
* const options = { invitations: true, onChange: reload };
|
|
373
|
+
* const data = await loadOrganizationMembers(authClient, options);
|
|
374
|
+
*
|
|
375
|
+
* <OrganizationMembers
|
|
376
|
+
* {...data}
|
|
377
|
+
* {...organizationMembersHandlers(authClient, options)}
|
|
378
|
+
* />
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
function organizationMembersHandlers(client, options = {}) {
|
|
382
|
+
const organization = need(client.organization, "organizationClient");
|
|
383
|
+
const inOrganization = options.organizationId ? { organizationId: options.organizationId } : {};
|
|
384
|
+
/** Runs a client call, throws its error, then asks for the data again. */
|
|
385
|
+
async function change(call) {
|
|
386
|
+
unwrap(await call);
|
|
387
|
+
options.onChange?.();
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
onInvite: (email, role) => change(organization.inviteMember({
|
|
391
|
+
...inOrganization,
|
|
392
|
+
email,
|
|
393
|
+
role
|
|
394
|
+
})),
|
|
395
|
+
onChangeRole: (memberId, role) => change(organization.updateMemberRole({
|
|
396
|
+
...inOrganization,
|
|
397
|
+
memberId,
|
|
398
|
+
role
|
|
399
|
+
})),
|
|
400
|
+
onRemove: (memberId) => change(organization.removeMember({
|
|
401
|
+
...inOrganization,
|
|
402
|
+
memberIdOrEmail: memberId
|
|
403
|
+
})),
|
|
404
|
+
onCancelInvitation: (invitationId) => change(organization.cancelInvitation({ invitationId }))
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
// src/organization-switcher.ts
|
|
408
|
+
/**
|
|
409
|
+
* Loads what the Iris UI `OrganizationSwitcher` draws: the organizations the
|
|
410
|
+
* person belongs to, and the one their session is in.
|
|
411
|
+
*/
|
|
412
|
+
async function loadOrganizations(client) {
|
|
413
|
+
const organization = need(client.organization, "organizationClient");
|
|
414
|
+
const session = unwrap(await client.getSession())?.session;
|
|
415
|
+
return {
|
|
416
|
+
organizations: (unwrap(await organization.list()) ?? []).map(({ id, name }) => ({
|
|
417
|
+
id,
|
|
418
|
+
name
|
|
419
|
+
})),
|
|
420
|
+
activeId: session?.activeOrganizationId ?? null
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* The handler prop that connects an Iris UI `OrganizationSwitcher` to an Entro
|
|
425
|
+
* Auth client. Switching changes the session's active organization, which is
|
|
426
|
+
* what the organization endpoints act on by default.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```tsx
|
|
430
|
+
* const data = await loadOrganizations(authClient);
|
|
431
|
+
*
|
|
432
|
+
* <OrganizationSwitcher
|
|
433
|
+
* {...data}
|
|
434
|
+
* {...organizationSwitcherHandlers(authClient, { onChange: reload })}
|
|
435
|
+
* />
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
function organizationSwitcherHandlers(client, options = {}) {
|
|
439
|
+
const organization = need(client.organization, "organizationClient");
|
|
440
|
+
return { async onSwitch(id) {
|
|
441
|
+
try {
|
|
442
|
+
unwrap(await organization.setActive({ organizationId: id }));
|
|
443
|
+
} finally {
|
|
444
|
+
options.onChange?.();
|
|
445
|
+
}
|
|
446
|
+
} };
|
|
447
|
+
}
|
|
448
|
+
// src/reset-password.ts
|
|
449
|
+
/**
|
|
450
|
+
* The props that connect an Iris UI `ResetPassword` block to an Entro Auth
|
|
451
|
+
* client, on the page named as `resetPasswordURL` in `signInHandlers`. The
|
|
452
|
+
* server sends the browser there with a `token` query parameter, or without one
|
|
453
|
+
* when the link no longer works.
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* ```tsx
|
|
457
|
+
* const token = new URLSearchParams(location.search).get("token");
|
|
458
|
+
*
|
|
459
|
+
* <ResetPassword
|
|
460
|
+
* {...resetPasswordHandlers(authClient, token)}
|
|
461
|
+
* onSignIn={() => router.push("/sign-in")}
|
|
462
|
+
* />
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
function resetPasswordHandlers(client, token) {
|
|
466
|
+
return {
|
|
467
|
+
expired: !token,
|
|
468
|
+
async onSubmit(password) {
|
|
469
|
+
unwrap(await client.resetPassword({
|
|
470
|
+
newPassword: password,
|
|
471
|
+
token: token ?? ""
|
|
472
|
+
}));
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
// src/sign-up.ts
|
|
477
|
+
/**
|
|
478
|
+
* The handler props that connect an Iris UI `SignupForm` block to an Entro
|
|
479
|
+
* Auth client.
|
|
480
|
+
*
|
|
481
|
+
* @example
|
|
482
|
+
* ```tsx
|
|
483
|
+
* const handlers = signUpHandlers(authClient, {
|
|
484
|
+
* confirmEmail: "code",
|
|
485
|
+
* social: true,
|
|
486
|
+
* callbackURL: "/welcome",
|
|
487
|
+
* });
|
|
488
|
+
*
|
|
489
|
+
* <SignupForm {...handlers} providers={["Google", "Facebook"]} />
|
|
490
|
+
* ```
|
|
491
|
+
*/
|
|
492
|
+
function signUpHandlers(client, options = {}) {
|
|
493
|
+
const { confirmEmail, callbackURL } = options;
|
|
494
|
+
const withCallback = callbackURL ? { callbackURL } : {};
|
|
495
|
+
const handlers = { async onSubmit(details) {
|
|
496
|
+
const { name, email, password } = details;
|
|
497
|
+
const created = unwrap(await client.signUp.email({
|
|
498
|
+
...options.fields?.(details),
|
|
499
|
+
name,
|
|
500
|
+
email,
|
|
501
|
+
password,
|
|
502
|
+
...withCallback
|
|
503
|
+
}));
|
|
504
|
+
return confirmEmail && !created?.token ? { confirmEmail } : {};
|
|
505
|
+
} };
|
|
506
|
+
if (confirmEmail === "link") handlers.onResendConfirmation = async (email) => {
|
|
507
|
+
unwrap(await client.sendVerificationEmail({
|
|
508
|
+
email,
|
|
509
|
+
...withCallback
|
|
510
|
+
}));
|
|
511
|
+
};
|
|
512
|
+
if (confirmEmail === "code") {
|
|
513
|
+
const emailOtp = need(client.emailOtp, "emailOTPClient");
|
|
514
|
+
handlers.onConfirmCode = async (email, code) => {
|
|
515
|
+
unwrap(await emailOtp.verifyEmail({
|
|
516
|
+
email,
|
|
517
|
+
otp: code
|
|
518
|
+
}));
|
|
519
|
+
};
|
|
520
|
+
handlers.onResendConfirmation = async (email) => {
|
|
521
|
+
unwrap(await emailOtp.sendVerificationOtp({
|
|
522
|
+
email,
|
|
523
|
+
type: "email-verification"
|
|
524
|
+
}));
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
if (options.social) handlers.onProvider = async (provider) => {
|
|
528
|
+
unwrap(await client.signIn.social({
|
|
529
|
+
provider: providerIds[provider],
|
|
530
|
+
...withCallback
|
|
531
|
+
}));
|
|
532
|
+
};
|
|
533
|
+
return handlers;
|
|
534
|
+
}
|
|
535
|
+
export { accountProfileHandlers, accountSecurityHandlers, loadAccountProfile, loadAccountSecurity, loadOrganizationMembers, loadOrganizations, organizationMembersHandlers, organizationSwitcherHandlers, resetPasswordHandlers, signInHandlers, signUpHandlers };
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@entro314labs/entro-auth-iris",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.8.0-beta.12",
|
|
4
|
+
"description": "Handlers that connect an Entro Auth client to the Iris UI sign-in blocks",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/entro314-labs/entro-auth.git",
|
|
10
|
+
"directory": "packages/iris"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"auth",
|
|
14
|
+
"iris-ui",
|
|
15
|
+
"sign-in",
|
|
16
|
+
"react",
|
|
17
|
+
"typescript",
|
|
18
|
+
"entro-auth"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"main": "./dist/index.mjs",
|
|
28
|
+
"module": "./dist/index.mjs",
|
|
29
|
+
"types": "./dist/index.d.mts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"dev-source": "./src/index.ts",
|
|
33
|
+
"types": "./dist/index.d.mts",
|
|
34
|
+
"default": "./dist/index.mjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@better-auth/utils": "0.5.1",
|
|
39
|
+
"@entro314labs/entro-auth-core": "1.8.0-beta.12",
|
|
40
|
+
"@entro314labs/entro-auth-passkey": "1.8.0-beta.12",
|
|
41
|
+
"@entro314labs/entro-auth": "1.8.0-beta.12",
|
|
42
|
+
"happy-dom": "^21.0.4",
|
|
43
|
+
"tsdown": "0.23.1",
|
|
44
|
+
"typescript": "^6.0.3"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsdown",
|
|
48
|
+
"dev": "tsdown --watch",
|
|
49
|
+
"lint:package": "publint run --strict --pack false",
|
|
50
|
+
"lint:types": "attw --profile esm-only --pack .",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "vitest",
|
|
53
|
+
"coverage": "vitest run --coverage --coverage.provider=istanbul",
|
|
54
|
+
"check:seam": "tsc -p seam && vitest run --config seam/vitest.config.ts"
|
|
55
|
+
}
|
|
6
56
|
}
|
package/README.md
DELETED