@netlify/identity 0.1.1-alpha.8 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -258
- package/dist/index.cjs +25 -500
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -53
- package/dist/index.d.ts +1 -53
- package/dist/index.js +24 -487
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -32,22 +32,10 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
AuthError: () => AuthError,
|
|
34
34
|
MissingIdentityError: () => MissingIdentityError,
|
|
35
|
-
acceptInvite: () => acceptInvite,
|
|
36
|
-
confirmEmail: () => confirmEmail,
|
|
37
35
|
getIdentityConfig: () => getIdentityConfig,
|
|
38
36
|
getSettings: () => getSettings,
|
|
39
37
|
getUser: () => getUser,
|
|
40
|
-
|
|
41
|
-
isAuthenticated: () => isAuthenticated,
|
|
42
|
-
login: () => login,
|
|
43
|
-
logout: () => logout,
|
|
44
|
-
oauthLogin: () => oauthLogin,
|
|
45
|
-
onAuthChange: () => onAuthChange,
|
|
46
|
-
recoverPassword: () => recoverPassword,
|
|
47
|
-
requestPasswordRecovery: () => requestPasswordRecovery,
|
|
48
|
-
signup: () => signup,
|
|
49
|
-
updateUser: () => updateUser,
|
|
50
|
-
verifyEmailChange: () => verifyEmailChange
|
|
38
|
+
isAuthenticated: () => isAuthenticated
|
|
51
39
|
});
|
|
52
40
|
module.exports = __toCommonJS(index_exports);
|
|
53
41
|
|
|
@@ -56,26 +44,6 @@ var AUTH_PROVIDERS = ["google", "github", "gitlab", "bitbucket", "facebook", "sa
|
|
|
56
44
|
|
|
57
45
|
// src/environment.ts
|
|
58
46
|
var import_gotrue_js = __toESM(require("gotrue-js"), 1);
|
|
59
|
-
|
|
60
|
-
// src/errors.ts
|
|
61
|
-
var AuthError = class extends Error {
|
|
62
|
-
constructor(message, status, options) {
|
|
63
|
-
super(message);
|
|
64
|
-
this.name = "AuthError";
|
|
65
|
-
this.status = status;
|
|
66
|
-
if (options && "cause" in options) {
|
|
67
|
-
this.cause = options.cause;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
var MissingIdentityError = class extends Error {
|
|
72
|
-
constructor(message = "Identity is not available in this environment") {
|
|
73
|
-
super(message);
|
|
74
|
-
this.name = "MissingIdentityError";
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
// src/environment.ts
|
|
79
47
|
var IDENTITY_PATH = "/.netlify/identity";
|
|
80
48
|
var goTrueClient = null;
|
|
81
49
|
var cachedApiUrl;
|
|
@@ -110,11 +78,6 @@ var getGoTrueClient = () => {
|
|
|
110
78
|
goTrueClient = new import_gotrue_js.default({ APIUrl: apiUrl, setCookie: isBrowser() });
|
|
111
79
|
return goTrueClient;
|
|
112
80
|
};
|
|
113
|
-
var getClient = () => {
|
|
114
|
-
const client = getGoTrueClient();
|
|
115
|
-
if (!client) throw new MissingIdentityError();
|
|
116
|
-
return client;
|
|
117
|
-
};
|
|
118
81
|
var getIdentityContext = () => {
|
|
119
82
|
const identityContext = globalThis.netlifyIdentityContext;
|
|
120
83
|
if (identityContext?.url) {
|
|
@@ -129,43 +92,6 @@ var getIdentityContext = () => {
|
|
|
129
92
|
return null;
|
|
130
93
|
};
|
|
131
94
|
|
|
132
|
-
// src/cookies.ts
|
|
133
|
-
var NF_JWT_COOKIE = "nf_jwt";
|
|
134
|
-
var NF_REFRESH_COOKIE = "nf_refresh";
|
|
135
|
-
var getCookie = (name) => {
|
|
136
|
-
const match = document.cookie.match(new RegExp(`(?:^|; )${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}=([^;]*)`));
|
|
137
|
-
return match ? decodeURIComponent(match[1]) : null;
|
|
138
|
-
};
|
|
139
|
-
var setAuthCookies = (cookies, accessToken, refreshToken) => {
|
|
140
|
-
cookies.set({
|
|
141
|
-
name: NF_JWT_COOKIE,
|
|
142
|
-
value: accessToken,
|
|
143
|
-
httpOnly: false,
|
|
144
|
-
secure: true,
|
|
145
|
-
path: "/",
|
|
146
|
-
sameSite: "Lax"
|
|
147
|
-
});
|
|
148
|
-
if (refreshToken) {
|
|
149
|
-
cookies.set({
|
|
150
|
-
name: NF_REFRESH_COOKIE,
|
|
151
|
-
value: refreshToken,
|
|
152
|
-
httpOnly: false,
|
|
153
|
-
secure: true,
|
|
154
|
-
path: "/",
|
|
155
|
-
sameSite: "Lax"
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
var deleteAuthCookies = (cookies) => {
|
|
160
|
-
cookies.delete(NF_JWT_COOKIE);
|
|
161
|
-
cookies.delete(NF_REFRESH_COOKIE);
|
|
162
|
-
};
|
|
163
|
-
var getServerCookie = (name) => {
|
|
164
|
-
const cookies = globalThis.Netlify?.context?.cookies;
|
|
165
|
-
if (!cookies || typeof cookies.get !== "function") return null;
|
|
166
|
-
return cookies.get(name) ?? null;
|
|
167
|
-
};
|
|
168
|
-
|
|
169
95
|
// src/user.ts
|
|
170
96
|
var toAuthProvider = (value) => typeof value === "string" && AUTH_PROVIDERS.includes(value) ? value : void 0;
|
|
171
97
|
var toUser = (userData) => {
|
|
@@ -198,42 +124,36 @@ var claimsToUser = (claims) => {
|
|
|
198
124
|
metadata: userMeta
|
|
199
125
|
};
|
|
200
126
|
};
|
|
201
|
-
var decodeJwtPayload = (token) => {
|
|
202
|
-
try {
|
|
203
|
-
const parts = token.split(".");
|
|
204
|
-
if (parts.length !== 3) return null;
|
|
205
|
-
const payload = atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"));
|
|
206
|
-
return JSON.parse(payload);
|
|
207
|
-
} catch {
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
127
|
var getUser = () => {
|
|
212
128
|
if (isBrowser()) {
|
|
213
129
|
const client = getGoTrueClient();
|
|
214
130
|
const currentUser = client?.currentUser() ?? null;
|
|
215
|
-
if (currentUser) return
|
|
216
|
-
|
|
217
|
-
if (!jwt2) return null;
|
|
218
|
-
const claims = decodeJwtPayload(jwt2);
|
|
219
|
-
if (!claims) return null;
|
|
220
|
-
return claimsToUser(claims);
|
|
131
|
+
if (!currentUser) return null;
|
|
132
|
+
return toUser(currentUser);
|
|
221
133
|
}
|
|
222
134
|
const identityContext = globalThis.netlifyIdentityContext;
|
|
223
|
-
if (identityContext?.user)
|
|
224
|
-
|
|
135
|
+
if (!identityContext?.user) return null;
|
|
136
|
+
return claimsToUser(identityContext.user);
|
|
137
|
+
};
|
|
138
|
+
var isAuthenticated = () => getUser() !== null;
|
|
139
|
+
|
|
140
|
+
// src/errors.ts
|
|
141
|
+
var AuthError = class extends Error {
|
|
142
|
+
constructor(message, status, options) {
|
|
143
|
+
super(message);
|
|
144
|
+
this.name = "AuthError";
|
|
145
|
+
this.status = status;
|
|
146
|
+
if (options && "cause" in options) {
|
|
147
|
+
this.cause = options.cause;
|
|
148
|
+
}
|
|
225
149
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const claims = decodeJwtPayload(jwt);
|
|
232
|
-
if (claims) return claimsToUser(claims);
|
|
150
|
+
};
|
|
151
|
+
var MissingIdentityError = class extends Error {
|
|
152
|
+
constructor(message = "Identity is not available in this environment") {
|
|
153
|
+
super(message);
|
|
154
|
+
this.name = "MissingIdentityError";
|
|
233
155
|
}
|
|
234
|
-
return null;
|
|
235
156
|
};
|
|
236
|
-
var isAuthenticated = () => getUser() !== null;
|
|
237
157
|
|
|
238
158
|
// src/config.ts
|
|
239
159
|
var getIdentityConfig = () => {
|
|
@@ -243,7 +163,8 @@ var getIdentityConfig = () => {
|
|
|
243
163
|
return getIdentityContext();
|
|
244
164
|
};
|
|
245
165
|
var getSettings = async () => {
|
|
246
|
-
const client =
|
|
166
|
+
const client = getGoTrueClient();
|
|
167
|
+
if (!client) throw new MissingIdentityError();
|
|
247
168
|
try {
|
|
248
169
|
const raw = await client.settings();
|
|
249
170
|
const external = raw.external ?? {};
|
|
@@ -264,409 +185,13 @@ var getSettings = async () => {
|
|
|
264
185
|
throw new AuthError(err instanceof Error ? err.message : "Failed to fetch identity settings", 502, { cause: err });
|
|
265
186
|
}
|
|
266
187
|
};
|
|
267
|
-
|
|
268
|
-
// src/auth.ts
|
|
269
|
-
var getCookies = () => {
|
|
270
|
-
const cookies = globalThis.Netlify?.context?.cookies;
|
|
271
|
-
if (!cookies) {
|
|
272
|
-
throw new AuthError("Server-side auth requires Netlify Functions runtime");
|
|
273
|
-
}
|
|
274
|
-
return cookies;
|
|
275
|
-
};
|
|
276
|
-
var getServerIdentityUrl = () => {
|
|
277
|
-
const ctx = getIdentityContext();
|
|
278
|
-
if (!ctx?.url) {
|
|
279
|
-
throw new AuthError("Could not determine the Identity endpoint URL on the server");
|
|
280
|
-
}
|
|
281
|
-
return ctx.url;
|
|
282
|
-
};
|
|
283
|
-
var persistSession = true;
|
|
284
|
-
var listeners = /* @__PURE__ */ new Set();
|
|
285
|
-
var emitAuthEvent = (event, user) => {
|
|
286
|
-
for (const listener of listeners) {
|
|
287
|
-
listener(event, user);
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
var storageListenerAttached = false;
|
|
291
|
-
var attachStorageListener = () => {
|
|
292
|
-
if (storageListenerAttached) return;
|
|
293
|
-
storageListenerAttached = true;
|
|
294
|
-
window.addEventListener("storage", (event) => {
|
|
295
|
-
if (event.key !== "gotrue.user") return;
|
|
296
|
-
if (event.newValue) {
|
|
297
|
-
const client = getGoTrueClient();
|
|
298
|
-
const currentUser = client?.currentUser();
|
|
299
|
-
emitAuthEvent("login", currentUser ? toUser(currentUser) : null);
|
|
300
|
-
} else {
|
|
301
|
-
emitAuthEvent("logout", null);
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
};
|
|
305
|
-
var onAuthChange = (callback) => {
|
|
306
|
-
if (!isBrowser()) {
|
|
307
|
-
return () => {
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
listeners.add(callback);
|
|
311
|
-
attachStorageListener();
|
|
312
|
-
return () => {
|
|
313
|
-
listeners.delete(callback);
|
|
314
|
-
};
|
|
315
|
-
};
|
|
316
|
-
var login = async (email, password) => {
|
|
317
|
-
if (!isBrowser()) {
|
|
318
|
-
const identityUrl = getServerIdentityUrl();
|
|
319
|
-
const cookies = getCookies();
|
|
320
|
-
const body = new URLSearchParams({
|
|
321
|
-
grant_type: "password",
|
|
322
|
-
username: email,
|
|
323
|
-
password
|
|
324
|
-
});
|
|
325
|
-
let res;
|
|
326
|
-
try {
|
|
327
|
-
res = await fetch(`${identityUrl}/token`, {
|
|
328
|
-
method: "POST",
|
|
329
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
330
|
-
body: body.toString()
|
|
331
|
-
});
|
|
332
|
-
} catch (error) {
|
|
333
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
334
|
-
}
|
|
335
|
-
if (!res.ok) {
|
|
336
|
-
const errorBody = await res.json().catch(() => ({}));
|
|
337
|
-
throw new AuthError(
|
|
338
|
-
errorBody.msg || errorBody.error_description || `Login failed (${res.status})`,
|
|
339
|
-
res.status
|
|
340
|
-
);
|
|
341
|
-
}
|
|
342
|
-
const data = await res.json();
|
|
343
|
-
const accessToken = data.access_token;
|
|
344
|
-
let userRes;
|
|
345
|
-
try {
|
|
346
|
-
userRes = await fetch(`${identityUrl}/user`, {
|
|
347
|
-
headers: { Authorization: `Bearer ${accessToken}` }
|
|
348
|
-
});
|
|
349
|
-
} catch (error) {
|
|
350
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
351
|
-
}
|
|
352
|
-
if (!userRes.ok) {
|
|
353
|
-
const errorBody = await userRes.json().catch(() => ({}));
|
|
354
|
-
throw new AuthError(
|
|
355
|
-
errorBody.msg || `Failed to fetch user data (${userRes.status})`,
|
|
356
|
-
userRes.status
|
|
357
|
-
);
|
|
358
|
-
}
|
|
359
|
-
const userData = await userRes.json();
|
|
360
|
-
const user = toUser(userData);
|
|
361
|
-
setAuthCookies(cookies, accessToken, data.refresh_token);
|
|
362
|
-
return user;
|
|
363
|
-
}
|
|
364
|
-
const client = getClient();
|
|
365
|
-
try {
|
|
366
|
-
const gotrueUser = await client.login(email, password, persistSession);
|
|
367
|
-
const user = toUser(gotrueUser);
|
|
368
|
-
emitAuthEvent("login", user);
|
|
369
|
-
return user;
|
|
370
|
-
} catch (error) {
|
|
371
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
372
|
-
}
|
|
373
|
-
};
|
|
374
|
-
var signup = async (email, password, data) => {
|
|
375
|
-
if (!isBrowser()) {
|
|
376
|
-
const identityUrl = getServerIdentityUrl();
|
|
377
|
-
const cookies = getCookies();
|
|
378
|
-
let res;
|
|
379
|
-
try {
|
|
380
|
-
res = await fetch(`${identityUrl}/signup`, {
|
|
381
|
-
method: "POST",
|
|
382
|
-
headers: { "Content-Type": "application/json" },
|
|
383
|
-
body: JSON.stringify({ email, password, data })
|
|
384
|
-
});
|
|
385
|
-
} catch (error) {
|
|
386
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
387
|
-
}
|
|
388
|
-
if (!res.ok) {
|
|
389
|
-
const errorBody = await res.json().catch(() => ({}));
|
|
390
|
-
throw new AuthError(errorBody.msg || `Signup failed (${res.status})`, res.status);
|
|
391
|
-
}
|
|
392
|
-
const responseData = await res.json();
|
|
393
|
-
const user = toUser(responseData);
|
|
394
|
-
if (responseData.confirmed_at) {
|
|
395
|
-
const responseRecord = responseData;
|
|
396
|
-
const accessToken = responseRecord.access_token;
|
|
397
|
-
if (accessToken) {
|
|
398
|
-
setAuthCookies(cookies, accessToken, responseRecord.refresh_token);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
return user;
|
|
402
|
-
}
|
|
403
|
-
const client = getClient();
|
|
404
|
-
try {
|
|
405
|
-
const response = await client.signup(email, password, data);
|
|
406
|
-
const user = toUser(response);
|
|
407
|
-
if (response.confirmed_at) {
|
|
408
|
-
emitAuthEvent("login", user);
|
|
409
|
-
}
|
|
410
|
-
return user;
|
|
411
|
-
} catch (error) {
|
|
412
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
var logout = async () => {
|
|
416
|
-
if (!isBrowser()) {
|
|
417
|
-
const identityUrl = getServerIdentityUrl();
|
|
418
|
-
const cookies = getCookies();
|
|
419
|
-
const jwt = cookies.get(NF_JWT_COOKIE);
|
|
420
|
-
if (jwt) {
|
|
421
|
-
try {
|
|
422
|
-
await fetch(`${identityUrl}/logout`, {
|
|
423
|
-
method: "POST",
|
|
424
|
-
headers: { Authorization: `Bearer ${jwt}` }
|
|
425
|
-
});
|
|
426
|
-
} catch (error) {
|
|
427
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
deleteAuthCookies(cookies);
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
const client = getClient();
|
|
434
|
-
try {
|
|
435
|
-
const currentUser = client.currentUser();
|
|
436
|
-
if (currentUser) {
|
|
437
|
-
await currentUser.logout();
|
|
438
|
-
}
|
|
439
|
-
emitAuthEvent("logout", null);
|
|
440
|
-
} catch (error) {
|
|
441
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
var oauthLogin = (provider) => {
|
|
445
|
-
if (!isBrowser()) {
|
|
446
|
-
throw new Error("oauthLogin() is only available in the browser");
|
|
447
|
-
}
|
|
448
|
-
const client = getClient();
|
|
449
|
-
window.location.href = client.loginExternalUrl(provider);
|
|
450
|
-
throw new Error("Redirecting to OAuth provider");
|
|
451
|
-
};
|
|
452
|
-
var handleAuthCallback = async () => {
|
|
453
|
-
if (!isBrowser()) return null;
|
|
454
|
-
const hash = window.location.hash.substring(1);
|
|
455
|
-
if (!hash) return null;
|
|
456
|
-
const client = getClient();
|
|
457
|
-
try {
|
|
458
|
-
const params = new URLSearchParams(hash);
|
|
459
|
-
const accessToken = params.get("access_token");
|
|
460
|
-
if (accessToken) {
|
|
461
|
-
const gotrueUser = await client.createUser(
|
|
462
|
-
{
|
|
463
|
-
access_token: accessToken,
|
|
464
|
-
token_type: params.get("token_type") ?? "bearer",
|
|
465
|
-
expires_in: Number(params.get("expires_in")),
|
|
466
|
-
expires_at: Number(params.get("expires_at")),
|
|
467
|
-
refresh_token: params.get("refresh_token") ?? ""
|
|
468
|
-
},
|
|
469
|
-
persistSession
|
|
470
|
-
);
|
|
471
|
-
const user = toUser(gotrueUser);
|
|
472
|
-
clearHash();
|
|
473
|
-
emitAuthEvent("login", user);
|
|
474
|
-
return { type: "oauth", user };
|
|
475
|
-
}
|
|
476
|
-
const confirmationToken = params.get("confirmation_token");
|
|
477
|
-
if (confirmationToken) {
|
|
478
|
-
const gotrueUser = await client.confirm(confirmationToken, persistSession);
|
|
479
|
-
const user = toUser(gotrueUser);
|
|
480
|
-
clearHash();
|
|
481
|
-
emitAuthEvent("login", user);
|
|
482
|
-
return { type: "confirmation", user };
|
|
483
|
-
}
|
|
484
|
-
const recoveryToken = params.get("recovery_token");
|
|
485
|
-
if (recoveryToken) {
|
|
486
|
-
const gotrueUser = await client.recover(recoveryToken, persistSession);
|
|
487
|
-
const user = toUser(gotrueUser);
|
|
488
|
-
clearHash();
|
|
489
|
-
emitAuthEvent("login", user);
|
|
490
|
-
return { type: "recovery", user };
|
|
491
|
-
}
|
|
492
|
-
const inviteToken = params.get("invite_token");
|
|
493
|
-
if (inviteToken) {
|
|
494
|
-
clearHash();
|
|
495
|
-
return { type: "invite", user: null, token: inviteToken };
|
|
496
|
-
}
|
|
497
|
-
const emailChangeToken = params.get("email_change_token");
|
|
498
|
-
if (emailChangeToken) {
|
|
499
|
-
const currentUser = client.currentUser();
|
|
500
|
-
if (!currentUser) {
|
|
501
|
-
throw new AuthError("Email change verification requires an active browser session");
|
|
502
|
-
}
|
|
503
|
-
const jwt = await currentUser.jwt();
|
|
504
|
-
const identityUrl = `${window.location.origin}${IDENTITY_PATH}`;
|
|
505
|
-
const emailChangeRes = await fetch(`${identityUrl}/user`, {
|
|
506
|
-
method: "PUT",
|
|
507
|
-
headers: {
|
|
508
|
-
"Content-Type": "application/json",
|
|
509
|
-
Authorization: `Bearer ${jwt}`
|
|
510
|
-
},
|
|
511
|
-
body: JSON.stringify({ email_change_token: emailChangeToken })
|
|
512
|
-
});
|
|
513
|
-
if (!emailChangeRes.ok) {
|
|
514
|
-
const errorBody = await emailChangeRes.json().catch(() => ({}));
|
|
515
|
-
throw new AuthError(
|
|
516
|
-
errorBody.msg || `Email change verification failed (${emailChangeRes.status})`,
|
|
517
|
-
emailChangeRes.status
|
|
518
|
-
);
|
|
519
|
-
}
|
|
520
|
-
const emailChangeData = await emailChangeRes.json();
|
|
521
|
-
const user = toUser(emailChangeData);
|
|
522
|
-
clearHash();
|
|
523
|
-
emitAuthEvent("user_updated", user);
|
|
524
|
-
return { type: "email_change", user };
|
|
525
|
-
}
|
|
526
|
-
return null;
|
|
527
|
-
} catch (error) {
|
|
528
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
529
|
-
}
|
|
530
|
-
};
|
|
531
|
-
var clearHash = () => {
|
|
532
|
-
history.replaceState(null, "", window.location.pathname + window.location.search);
|
|
533
|
-
};
|
|
534
|
-
var hydrateSession = async () => {
|
|
535
|
-
if (!isBrowser()) return null;
|
|
536
|
-
const client = getClient();
|
|
537
|
-
const currentUser = client.currentUser();
|
|
538
|
-
if (currentUser) return toUser(currentUser);
|
|
539
|
-
const accessToken = getCookie(NF_JWT_COOKIE);
|
|
540
|
-
if (!accessToken) return null;
|
|
541
|
-
const refreshToken = getCookie(NF_REFRESH_COOKIE) ?? "";
|
|
542
|
-
const gotrueUser = await client.createUser(
|
|
543
|
-
{
|
|
544
|
-
access_token: accessToken,
|
|
545
|
-
token_type: "bearer",
|
|
546
|
-
expires_in: 3600,
|
|
547
|
-
expires_at: Math.floor(Date.now() / 1e3) + 3600,
|
|
548
|
-
refresh_token: refreshToken
|
|
549
|
-
},
|
|
550
|
-
persistSession
|
|
551
|
-
);
|
|
552
|
-
const user = toUser(gotrueUser);
|
|
553
|
-
emitAuthEvent("login", user);
|
|
554
|
-
return user;
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
// src/account.ts
|
|
558
|
-
var ensureCurrentUser = async () => {
|
|
559
|
-
const client = getClient();
|
|
560
|
-
let currentUser = client.currentUser();
|
|
561
|
-
if (!currentUser && isBrowser()) {
|
|
562
|
-
await hydrateSession();
|
|
563
|
-
currentUser = client.currentUser();
|
|
564
|
-
}
|
|
565
|
-
if (!currentUser) throw new AuthError("No user is currently logged in");
|
|
566
|
-
return currentUser;
|
|
567
|
-
};
|
|
568
|
-
var requestPasswordRecovery = async (email) => {
|
|
569
|
-
const client = getClient();
|
|
570
|
-
try {
|
|
571
|
-
await client.requestPasswordRecovery(email);
|
|
572
|
-
} catch (error) {
|
|
573
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
574
|
-
}
|
|
575
|
-
};
|
|
576
|
-
var recoverPassword = async (token, newPassword) => {
|
|
577
|
-
const client = getClient();
|
|
578
|
-
try {
|
|
579
|
-
const gotrueUser = await client.recover(token, persistSession);
|
|
580
|
-
const updatedUser = await gotrueUser.update({ password: newPassword });
|
|
581
|
-
const user = toUser(updatedUser);
|
|
582
|
-
emitAuthEvent("login", user);
|
|
583
|
-
return user;
|
|
584
|
-
} catch (error) {
|
|
585
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
var confirmEmail = async (token) => {
|
|
589
|
-
const client = getClient();
|
|
590
|
-
try {
|
|
591
|
-
const gotrueUser = await client.confirm(token, persistSession);
|
|
592
|
-
const user = toUser(gotrueUser);
|
|
593
|
-
emitAuthEvent("login", user);
|
|
594
|
-
return user;
|
|
595
|
-
} catch (error) {
|
|
596
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
597
|
-
}
|
|
598
|
-
};
|
|
599
|
-
var acceptInvite = async (token, password) => {
|
|
600
|
-
const client = getClient();
|
|
601
|
-
try {
|
|
602
|
-
const gotrueUser = await client.acceptInvite(token, password, persistSession);
|
|
603
|
-
const user = toUser(gotrueUser);
|
|
604
|
-
emitAuthEvent("login", user);
|
|
605
|
-
return user;
|
|
606
|
-
} catch (error) {
|
|
607
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
608
|
-
}
|
|
609
|
-
};
|
|
610
|
-
var verifyEmailChange = async (token) => {
|
|
611
|
-
if (!isBrowser()) throw new AuthError("verifyEmailChange() is only available in the browser");
|
|
612
|
-
const currentUser = await ensureCurrentUser();
|
|
613
|
-
const jwt = await currentUser.jwt();
|
|
614
|
-
const identityUrl = `${window.location.origin}${IDENTITY_PATH}`;
|
|
615
|
-
try {
|
|
616
|
-
const res = await fetch(`${identityUrl}/user`, {
|
|
617
|
-
method: "PUT",
|
|
618
|
-
headers: {
|
|
619
|
-
"Content-Type": "application/json",
|
|
620
|
-
Authorization: `Bearer ${jwt}`
|
|
621
|
-
},
|
|
622
|
-
body: JSON.stringify({ email_change_token: token })
|
|
623
|
-
});
|
|
624
|
-
if (!res.ok) {
|
|
625
|
-
const errorBody = await res.json().catch(() => ({}));
|
|
626
|
-
throw new AuthError(
|
|
627
|
-
errorBody.msg || `Email change verification failed (${res.status})`,
|
|
628
|
-
res.status
|
|
629
|
-
);
|
|
630
|
-
}
|
|
631
|
-
const userData = await res.json();
|
|
632
|
-
const user = toUser(userData);
|
|
633
|
-
emitAuthEvent("user_updated", user);
|
|
634
|
-
return user;
|
|
635
|
-
} catch (error) {
|
|
636
|
-
if (error instanceof AuthError) throw error;
|
|
637
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
638
|
-
}
|
|
639
|
-
};
|
|
640
|
-
var updateUser = async (updates) => {
|
|
641
|
-
const currentUser = await ensureCurrentUser();
|
|
642
|
-
try {
|
|
643
|
-
const updatedUser = await currentUser.update(updates);
|
|
644
|
-
const user = toUser(updatedUser);
|
|
645
|
-
emitAuthEvent("user_updated", user);
|
|
646
|
-
return user;
|
|
647
|
-
} catch (error) {
|
|
648
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
649
|
-
}
|
|
650
|
-
};
|
|
651
188
|
// Annotate the CommonJS export names for ESM import in node:
|
|
652
189
|
0 && (module.exports = {
|
|
653
190
|
AuthError,
|
|
654
191
|
MissingIdentityError,
|
|
655
|
-
acceptInvite,
|
|
656
|
-
confirmEmail,
|
|
657
192
|
getIdentityConfig,
|
|
658
193
|
getSettings,
|
|
659
194
|
getUser,
|
|
660
|
-
|
|
661
|
-
isAuthenticated,
|
|
662
|
-
login,
|
|
663
|
-
logout,
|
|
664
|
-
oauthLogin,
|
|
665
|
-
onAuthChange,
|
|
666
|
-
recoverPassword,
|
|
667
|
-
requestPasswordRecovery,
|
|
668
|
-
signup,
|
|
669
|
-
updateUser,
|
|
670
|
-
verifyEmailChange
|
|
195
|
+
isAuthenticated
|
|
671
196
|
});
|
|
672
197
|
//# sourceMappingURL=index.cjs.map
|