@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.js
CHANGED
|
@@ -3,26 +3,6 @@ var AUTH_PROVIDERS = ["google", "github", "gitlab", "bitbucket", "facebook", "sa
|
|
|
3
3
|
|
|
4
4
|
// src/environment.ts
|
|
5
5
|
import GoTrue from "gotrue-js";
|
|
6
|
-
|
|
7
|
-
// src/errors.ts
|
|
8
|
-
var AuthError = class extends Error {
|
|
9
|
-
constructor(message, status, options) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.name = "AuthError";
|
|
12
|
-
this.status = status;
|
|
13
|
-
if (options && "cause" in options) {
|
|
14
|
-
this.cause = options.cause;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
var MissingIdentityError = class extends Error {
|
|
19
|
-
constructor(message = "Identity is not available in this environment") {
|
|
20
|
-
super(message);
|
|
21
|
-
this.name = "MissingIdentityError";
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
// src/environment.ts
|
|
26
6
|
var IDENTITY_PATH = "/.netlify/identity";
|
|
27
7
|
var goTrueClient = null;
|
|
28
8
|
var cachedApiUrl;
|
|
@@ -57,11 +37,6 @@ var getGoTrueClient = () => {
|
|
|
57
37
|
goTrueClient = new GoTrue({ APIUrl: apiUrl, setCookie: isBrowser() });
|
|
58
38
|
return goTrueClient;
|
|
59
39
|
};
|
|
60
|
-
var getClient = () => {
|
|
61
|
-
const client = getGoTrueClient();
|
|
62
|
-
if (!client) throw new MissingIdentityError();
|
|
63
|
-
return client;
|
|
64
|
-
};
|
|
65
40
|
var getIdentityContext = () => {
|
|
66
41
|
const identityContext = globalThis.netlifyIdentityContext;
|
|
67
42
|
if (identityContext?.url) {
|
|
@@ -76,43 +51,6 @@ var getIdentityContext = () => {
|
|
|
76
51
|
return null;
|
|
77
52
|
};
|
|
78
53
|
|
|
79
|
-
// src/cookies.ts
|
|
80
|
-
var NF_JWT_COOKIE = "nf_jwt";
|
|
81
|
-
var NF_REFRESH_COOKIE = "nf_refresh";
|
|
82
|
-
var getCookie = (name) => {
|
|
83
|
-
const match = document.cookie.match(new RegExp(`(?:^|; )${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}=([^;]*)`));
|
|
84
|
-
return match ? decodeURIComponent(match[1]) : null;
|
|
85
|
-
};
|
|
86
|
-
var setAuthCookies = (cookies, accessToken, refreshToken) => {
|
|
87
|
-
cookies.set({
|
|
88
|
-
name: NF_JWT_COOKIE,
|
|
89
|
-
value: accessToken,
|
|
90
|
-
httpOnly: false,
|
|
91
|
-
secure: true,
|
|
92
|
-
path: "/",
|
|
93
|
-
sameSite: "Lax"
|
|
94
|
-
});
|
|
95
|
-
if (refreshToken) {
|
|
96
|
-
cookies.set({
|
|
97
|
-
name: NF_REFRESH_COOKIE,
|
|
98
|
-
value: refreshToken,
|
|
99
|
-
httpOnly: false,
|
|
100
|
-
secure: true,
|
|
101
|
-
path: "/",
|
|
102
|
-
sameSite: "Lax"
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
var deleteAuthCookies = (cookies) => {
|
|
107
|
-
cookies.delete(NF_JWT_COOKIE);
|
|
108
|
-
cookies.delete(NF_REFRESH_COOKIE);
|
|
109
|
-
};
|
|
110
|
-
var getServerCookie = (name) => {
|
|
111
|
-
const cookies = globalThis.Netlify?.context?.cookies;
|
|
112
|
-
if (!cookies || typeof cookies.get !== "function") return null;
|
|
113
|
-
return cookies.get(name) ?? null;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
54
|
// src/user.ts
|
|
117
55
|
var toAuthProvider = (value) => typeof value === "string" && AUTH_PROVIDERS.includes(value) ? value : void 0;
|
|
118
56
|
var toUser = (userData) => {
|
|
@@ -145,42 +83,36 @@ var claimsToUser = (claims) => {
|
|
|
145
83
|
metadata: userMeta
|
|
146
84
|
};
|
|
147
85
|
};
|
|
148
|
-
var decodeJwtPayload = (token) => {
|
|
149
|
-
try {
|
|
150
|
-
const parts = token.split(".");
|
|
151
|
-
if (parts.length !== 3) return null;
|
|
152
|
-
const payload = atob(parts[1].replace(/-/g, "+").replace(/_/g, "/"));
|
|
153
|
-
return JSON.parse(payload);
|
|
154
|
-
} catch {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
86
|
var getUser = () => {
|
|
159
87
|
if (isBrowser()) {
|
|
160
88
|
const client = getGoTrueClient();
|
|
161
89
|
const currentUser = client?.currentUser() ?? null;
|
|
162
|
-
if (currentUser) return
|
|
163
|
-
|
|
164
|
-
if (!jwt2) return null;
|
|
165
|
-
const claims = decodeJwtPayload(jwt2);
|
|
166
|
-
if (!claims) return null;
|
|
167
|
-
return claimsToUser(claims);
|
|
90
|
+
if (!currentUser) return null;
|
|
91
|
+
return toUser(currentUser);
|
|
168
92
|
}
|
|
169
93
|
const identityContext = globalThis.netlifyIdentityContext;
|
|
170
|
-
if (identityContext?.user)
|
|
171
|
-
|
|
94
|
+
if (!identityContext?.user) return null;
|
|
95
|
+
return claimsToUser(identityContext.user);
|
|
96
|
+
};
|
|
97
|
+
var isAuthenticated = () => getUser() !== null;
|
|
98
|
+
|
|
99
|
+
// src/errors.ts
|
|
100
|
+
var AuthError = class extends Error {
|
|
101
|
+
constructor(message, status, options) {
|
|
102
|
+
super(message);
|
|
103
|
+
this.name = "AuthError";
|
|
104
|
+
this.status = status;
|
|
105
|
+
if (options && "cause" in options) {
|
|
106
|
+
this.cause = options.cause;
|
|
107
|
+
}
|
|
172
108
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const claims = decodeJwtPayload(jwt);
|
|
179
|
-
if (claims) return claimsToUser(claims);
|
|
109
|
+
};
|
|
110
|
+
var MissingIdentityError = class extends Error {
|
|
111
|
+
constructor(message = "Identity is not available in this environment") {
|
|
112
|
+
super(message);
|
|
113
|
+
this.name = "MissingIdentityError";
|
|
180
114
|
}
|
|
181
|
-
return null;
|
|
182
115
|
};
|
|
183
|
-
var isAuthenticated = () => getUser() !== null;
|
|
184
116
|
|
|
185
117
|
// src/config.ts
|
|
186
118
|
var getIdentityConfig = () => {
|
|
@@ -190,7 +122,8 @@ var getIdentityConfig = () => {
|
|
|
190
122
|
return getIdentityContext();
|
|
191
123
|
};
|
|
192
124
|
var getSettings = async () => {
|
|
193
|
-
const client =
|
|
125
|
+
const client = getGoTrueClient();
|
|
126
|
+
if (!client) throw new MissingIdentityError();
|
|
194
127
|
try {
|
|
195
128
|
const raw = await client.settings();
|
|
196
129
|
const external = raw.external ?? {};
|
|
@@ -211,408 +144,12 @@ var getSettings = async () => {
|
|
|
211
144
|
throw new AuthError(err instanceof Error ? err.message : "Failed to fetch identity settings", 502, { cause: err });
|
|
212
145
|
}
|
|
213
146
|
};
|
|
214
|
-
|
|
215
|
-
// src/auth.ts
|
|
216
|
-
var getCookies = () => {
|
|
217
|
-
const cookies = globalThis.Netlify?.context?.cookies;
|
|
218
|
-
if (!cookies) {
|
|
219
|
-
throw new AuthError("Server-side auth requires Netlify Functions runtime");
|
|
220
|
-
}
|
|
221
|
-
return cookies;
|
|
222
|
-
};
|
|
223
|
-
var getServerIdentityUrl = () => {
|
|
224
|
-
const ctx = getIdentityContext();
|
|
225
|
-
if (!ctx?.url) {
|
|
226
|
-
throw new AuthError("Could not determine the Identity endpoint URL on the server");
|
|
227
|
-
}
|
|
228
|
-
return ctx.url;
|
|
229
|
-
};
|
|
230
|
-
var persistSession = true;
|
|
231
|
-
var listeners = /* @__PURE__ */ new Set();
|
|
232
|
-
var emitAuthEvent = (event, user) => {
|
|
233
|
-
for (const listener of listeners) {
|
|
234
|
-
listener(event, user);
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
var storageListenerAttached = false;
|
|
238
|
-
var attachStorageListener = () => {
|
|
239
|
-
if (storageListenerAttached) return;
|
|
240
|
-
storageListenerAttached = true;
|
|
241
|
-
window.addEventListener("storage", (event) => {
|
|
242
|
-
if (event.key !== "gotrue.user") return;
|
|
243
|
-
if (event.newValue) {
|
|
244
|
-
const client = getGoTrueClient();
|
|
245
|
-
const currentUser = client?.currentUser();
|
|
246
|
-
emitAuthEvent("login", currentUser ? toUser(currentUser) : null);
|
|
247
|
-
} else {
|
|
248
|
-
emitAuthEvent("logout", null);
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
};
|
|
252
|
-
var onAuthChange = (callback) => {
|
|
253
|
-
if (!isBrowser()) {
|
|
254
|
-
return () => {
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
listeners.add(callback);
|
|
258
|
-
attachStorageListener();
|
|
259
|
-
return () => {
|
|
260
|
-
listeners.delete(callback);
|
|
261
|
-
};
|
|
262
|
-
};
|
|
263
|
-
var login = async (email, password) => {
|
|
264
|
-
if (!isBrowser()) {
|
|
265
|
-
const identityUrl = getServerIdentityUrl();
|
|
266
|
-
const cookies = getCookies();
|
|
267
|
-
const body = new URLSearchParams({
|
|
268
|
-
grant_type: "password",
|
|
269
|
-
username: email,
|
|
270
|
-
password
|
|
271
|
-
});
|
|
272
|
-
let res;
|
|
273
|
-
try {
|
|
274
|
-
res = await fetch(`${identityUrl}/token`, {
|
|
275
|
-
method: "POST",
|
|
276
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
277
|
-
body: body.toString()
|
|
278
|
-
});
|
|
279
|
-
} catch (error) {
|
|
280
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
281
|
-
}
|
|
282
|
-
if (!res.ok) {
|
|
283
|
-
const errorBody = await res.json().catch(() => ({}));
|
|
284
|
-
throw new AuthError(
|
|
285
|
-
errorBody.msg || errorBody.error_description || `Login failed (${res.status})`,
|
|
286
|
-
res.status
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
const data = await res.json();
|
|
290
|
-
const accessToken = data.access_token;
|
|
291
|
-
let userRes;
|
|
292
|
-
try {
|
|
293
|
-
userRes = await fetch(`${identityUrl}/user`, {
|
|
294
|
-
headers: { Authorization: `Bearer ${accessToken}` }
|
|
295
|
-
});
|
|
296
|
-
} catch (error) {
|
|
297
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
298
|
-
}
|
|
299
|
-
if (!userRes.ok) {
|
|
300
|
-
const errorBody = await userRes.json().catch(() => ({}));
|
|
301
|
-
throw new AuthError(
|
|
302
|
-
errorBody.msg || `Failed to fetch user data (${userRes.status})`,
|
|
303
|
-
userRes.status
|
|
304
|
-
);
|
|
305
|
-
}
|
|
306
|
-
const userData = await userRes.json();
|
|
307
|
-
const user = toUser(userData);
|
|
308
|
-
setAuthCookies(cookies, accessToken, data.refresh_token);
|
|
309
|
-
return user;
|
|
310
|
-
}
|
|
311
|
-
const client = getClient();
|
|
312
|
-
try {
|
|
313
|
-
const gotrueUser = await client.login(email, password, persistSession);
|
|
314
|
-
const user = toUser(gotrueUser);
|
|
315
|
-
emitAuthEvent("login", user);
|
|
316
|
-
return user;
|
|
317
|
-
} catch (error) {
|
|
318
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
var signup = async (email, password, data) => {
|
|
322
|
-
if (!isBrowser()) {
|
|
323
|
-
const identityUrl = getServerIdentityUrl();
|
|
324
|
-
const cookies = getCookies();
|
|
325
|
-
let res;
|
|
326
|
-
try {
|
|
327
|
-
res = await fetch(`${identityUrl}/signup`, {
|
|
328
|
-
method: "POST",
|
|
329
|
-
headers: { "Content-Type": "application/json" },
|
|
330
|
-
body: JSON.stringify({ email, password, data })
|
|
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(errorBody.msg || `Signup failed (${res.status})`, res.status);
|
|
338
|
-
}
|
|
339
|
-
const responseData = await res.json();
|
|
340
|
-
const user = toUser(responseData);
|
|
341
|
-
if (responseData.confirmed_at) {
|
|
342
|
-
const responseRecord = responseData;
|
|
343
|
-
const accessToken = responseRecord.access_token;
|
|
344
|
-
if (accessToken) {
|
|
345
|
-
setAuthCookies(cookies, accessToken, responseRecord.refresh_token);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
return user;
|
|
349
|
-
}
|
|
350
|
-
const client = getClient();
|
|
351
|
-
try {
|
|
352
|
-
const response = await client.signup(email, password, data);
|
|
353
|
-
const user = toUser(response);
|
|
354
|
-
if (response.confirmed_at) {
|
|
355
|
-
emitAuthEvent("login", user);
|
|
356
|
-
}
|
|
357
|
-
return user;
|
|
358
|
-
} catch (error) {
|
|
359
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
var logout = async () => {
|
|
363
|
-
if (!isBrowser()) {
|
|
364
|
-
const identityUrl = getServerIdentityUrl();
|
|
365
|
-
const cookies = getCookies();
|
|
366
|
-
const jwt = cookies.get(NF_JWT_COOKIE);
|
|
367
|
-
if (jwt) {
|
|
368
|
-
try {
|
|
369
|
-
await fetch(`${identityUrl}/logout`, {
|
|
370
|
-
method: "POST",
|
|
371
|
-
headers: { Authorization: `Bearer ${jwt}` }
|
|
372
|
-
});
|
|
373
|
-
} catch (error) {
|
|
374
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
deleteAuthCookies(cookies);
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
const client = getClient();
|
|
381
|
-
try {
|
|
382
|
-
const currentUser = client.currentUser();
|
|
383
|
-
if (currentUser) {
|
|
384
|
-
await currentUser.logout();
|
|
385
|
-
}
|
|
386
|
-
emitAuthEvent("logout", null);
|
|
387
|
-
} catch (error) {
|
|
388
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
var oauthLogin = (provider) => {
|
|
392
|
-
if (!isBrowser()) {
|
|
393
|
-
throw new Error("oauthLogin() is only available in the browser");
|
|
394
|
-
}
|
|
395
|
-
const client = getClient();
|
|
396
|
-
window.location.href = client.loginExternalUrl(provider);
|
|
397
|
-
throw new Error("Redirecting to OAuth provider");
|
|
398
|
-
};
|
|
399
|
-
var handleAuthCallback = async () => {
|
|
400
|
-
if (!isBrowser()) return null;
|
|
401
|
-
const hash = window.location.hash.substring(1);
|
|
402
|
-
if (!hash) return null;
|
|
403
|
-
const client = getClient();
|
|
404
|
-
try {
|
|
405
|
-
const params = new URLSearchParams(hash);
|
|
406
|
-
const accessToken = params.get("access_token");
|
|
407
|
-
if (accessToken) {
|
|
408
|
-
const gotrueUser = await client.createUser(
|
|
409
|
-
{
|
|
410
|
-
access_token: accessToken,
|
|
411
|
-
token_type: params.get("token_type") ?? "bearer",
|
|
412
|
-
expires_in: Number(params.get("expires_in")),
|
|
413
|
-
expires_at: Number(params.get("expires_at")),
|
|
414
|
-
refresh_token: params.get("refresh_token") ?? ""
|
|
415
|
-
},
|
|
416
|
-
persistSession
|
|
417
|
-
);
|
|
418
|
-
const user = toUser(gotrueUser);
|
|
419
|
-
clearHash();
|
|
420
|
-
emitAuthEvent("login", user);
|
|
421
|
-
return { type: "oauth", user };
|
|
422
|
-
}
|
|
423
|
-
const confirmationToken = params.get("confirmation_token");
|
|
424
|
-
if (confirmationToken) {
|
|
425
|
-
const gotrueUser = await client.confirm(confirmationToken, persistSession);
|
|
426
|
-
const user = toUser(gotrueUser);
|
|
427
|
-
clearHash();
|
|
428
|
-
emitAuthEvent("login", user);
|
|
429
|
-
return { type: "confirmation", user };
|
|
430
|
-
}
|
|
431
|
-
const recoveryToken = params.get("recovery_token");
|
|
432
|
-
if (recoveryToken) {
|
|
433
|
-
const gotrueUser = await client.recover(recoveryToken, persistSession);
|
|
434
|
-
const user = toUser(gotrueUser);
|
|
435
|
-
clearHash();
|
|
436
|
-
emitAuthEvent("login", user);
|
|
437
|
-
return { type: "recovery", user };
|
|
438
|
-
}
|
|
439
|
-
const inviteToken = params.get("invite_token");
|
|
440
|
-
if (inviteToken) {
|
|
441
|
-
clearHash();
|
|
442
|
-
return { type: "invite", user: null, token: inviteToken };
|
|
443
|
-
}
|
|
444
|
-
const emailChangeToken = params.get("email_change_token");
|
|
445
|
-
if (emailChangeToken) {
|
|
446
|
-
const currentUser = client.currentUser();
|
|
447
|
-
if (!currentUser) {
|
|
448
|
-
throw new AuthError("Email change verification requires an active browser session");
|
|
449
|
-
}
|
|
450
|
-
const jwt = await currentUser.jwt();
|
|
451
|
-
const identityUrl = `${window.location.origin}${IDENTITY_PATH}`;
|
|
452
|
-
const emailChangeRes = await fetch(`${identityUrl}/user`, {
|
|
453
|
-
method: "PUT",
|
|
454
|
-
headers: {
|
|
455
|
-
"Content-Type": "application/json",
|
|
456
|
-
Authorization: `Bearer ${jwt}`
|
|
457
|
-
},
|
|
458
|
-
body: JSON.stringify({ email_change_token: emailChangeToken })
|
|
459
|
-
});
|
|
460
|
-
if (!emailChangeRes.ok) {
|
|
461
|
-
const errorBody = await emailChangeRes.json().catch(() => ({}));
|
|
462
|
-
throw new AuthError(
|
|
463
|
-
errorBody.msg || `Email change verification failed (${emailChangeRes.status})`,
|
|
464
|
-
emailChangeRes.status
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
const emailChangeData = await emailChangeRes.json();
|
|
468
|
-
const user = toUser(emailChangeData);
|
|
469
|
-
clearHash();
|
|
470
|
-
emitAuthEvent("user_updated", user);
|
|
471
|
-
return { type: "email_change", user };
|
|
472
|
-
}
|
|
473
|
-
return null;
|
|
474
|
-
} catch (error) {
|
|
475
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
var clearHash = () => {
|
|
479
|
-
history.replaceState(null, "", window.location.pathname + window.location.search);
|
|
480
|
-
};
|
|
481
|
-
var hydrateSession = async () => {
|
|
482
|
-
if (!isBrowser()) return null;
|
|
483
|
-
const client = getClient();
|
|
484
|
-
const currentUser = client.currentUser();
|
|
485
|
-
if (currentUser) return toUser(currentUser);
|
|
486
|
-
const accessToken = getCookie(NF_JWT_COOKIE);
|
|
487
|
-
if (!accessToken) return null;
|
|
488
|
-
const refreshToken = getCookie(NF_REFRESH_COOKIE) ?? "";
|
|
489
|
-
const gotrueUser = await client.createUser(
|
|
490
|
-
{
|
|
491
|
-
access_token: accessToken,
|
|
492
|
-
token_type: "bearer",
|
|
493
|
-
expires_in: 3600,
|
|
494
|
-
expires_at: Math.floor(Date.now() / 1e3) + 3600,
|
|
495
|
-
refresh_token: refreshToken
|
|
496
|
-
},
|
|
497
|
-
persistSession
|
|
498
|
-
);
|
|
499
|
-
const user = toUser(gotrueUser);
|
|
500
|
-
emitAuthEvent("login", user);
|
|
501
|
-
return user;
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
// src/account.ts
|
|
505
|
-
var ensureCurrentUser = async () => {
|
|
506
|
-
const client = getClient();
|
|
507
|
-
let currentUser = client.currentUser();
|
|
508
|
-
if (!currentUser && isBrowser()) {
|
|
509
|
-
await hydrateSession();
|
|
510
|
-
currentUser = client.currentUser();
|
|
511
|
-
}
|
|
512
|
-
if (!currentUser) throw new AuthError("No user is currently logged in");
|
|
513
|
-
return currentUser;
|
|
514
|
-
};
|
|
515
|
-
var requestPasswordRecovery = async (email) => {
|
|
516
|
-
const client = getClient();
|
|
517
|
-
try {
|
|
518
|
-
await client.requestPasswordRecovery(email);
|
|
519
|
-
} catch (error) {
|
|
520
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
var recoverPassword = async (token, newPassword) => {
|
|
524
|
-
const client = getClient();
|
|
525
|
-
try {
|
|
526
|
-
const gotrueUser = await client.recover(token, persistSession);
|
|
527
|
-
const updatedUser = await gotrueUser.update({ password: newPassword });
|
|
528
|
-
const user = toUser(updatedUser);
|
|
529
|
-
emitAuthEvent("login", user);
|
|
530
|
-
return user;
|
|
531
|
-
} catch (error) {
|
|
532
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
533
|
-
}
|
|
534
|
-
};
|
|
535
|
-
var confirmEmail = async (token) => {
|
|
536
|
-
const client = getClient();
|
|
537
|
-
try {
|
|
538
|
-
const gotrueUser = await client.confirm(token, persistSession);
|
|
539
|
-
const user = toUser(gotrueUser);
|
|
540
|
-
emitAuthEvent("login", user);
|
|
541
|
-
return user;
|
|
542
|
-
} catch (error) {
|
|
543
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
544
|
-
}
|
|
545
|
-
};
|
|
546
|
-
var acceptInvite = async (token, password) => {
|
|
547
|
-
const client = getClient();
|
|
548
|
-
try {
|
|
549
|
-
const gotrueUser = await client.acceptInvite(token, password, persistSession);
|
|
550
|
-
const user = toUser(gotrueUser);
|
|
551
|
-
emitAuthEvent("login", user);
|
|
552
|
-
return user;
|
|
553
|
-
} catch (error) {
|
|
554
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
555
|
-
}
|
|
556
|
-
};
|
|
557
|
-
var verifyEmailChange = async (token) => {
|
|
558
|
-
if (!isBrowser()) throw new AuthError("verifyEmailChange() is only available in the browser");
|
|
559
|
-
const currentUser = await ensureCurrentUser();
|
|
560
|
-
const jwt = await currentUser.jwt();
|
|
561
|
-
const identityUrl = `${window.location.origin}${IDENTITY_PATH}`;
|
|
562
|
-
try {
|
|
563
|
-
const res = await fetch(`${identityUrl}/user`, {
|
|
564
|
-
method: "PUT",
|
|
565
|
-
headers: {
|
|
566
|
-
"Content-Type": "application/json",
|
|
567
|
-
Authorization: `Bearer ${jwt}`
|
|
568
|
-
},
|
|
569
|
-
body: JSON.stringify({ email_change_token: token })
|
|
570
|
-
});
|
|
571
|
-
if (!res.ok) {
|
|
572
|
-
const errorBody = await res.json().catch(() => ({}));
|
|
573
|
-
throw new AuthError(
|
|
574
|
-
errorBody.msg || `Email change verification failed (${res.status})`,
|
|
575
|
-
res.status
|
|
576
|
-
);
|
|
577
|
-
}
|
|
578
|
-
const userData = await res.json();
|
|
579
|
-
const user = toUser(userData);
|
|
580
|
-
emitAuthEvent("user_updated", user);
|
|
581
|
-
return user;
|
|
582
|
-
} catch (error) {
|
|
583
|
-
if (error instanceof AuthError) throw error;
|
|
584
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
var updateUser = async (updates) => {
|
|
588
|
-
const currentUser = await ensureCurrentUser();
|
|
589
|
-
try {
|
|
590
|
-
const updatedUser = await currentUser.update(updates);
|
|
591
|
-
const user = toUser(updatedUser);
|
|
592
|
-
emitAuthEvent("user_updated", user);
|
|
593
|
-
return user;
|
|
594
|
-
} catch (error) {
|
|
595
|
-
throw new AuthError(error.message, void 0, { cause: error });
|
|
596
|
-
}
|
|
597
|
-
};
|
|
598
147
|
export {
|
|
599
148
|
AuthError,
|
|
600
149
|
MissingIdentityError,
|
|
601
|
-
acceptInvite,
|
|
602
|
-
confirmEmail,
|
|
603
150
|
getIdentityConfig,
|
|
604
151
|
getSettings,
|
|
605
152
|
getUser,
|
|
606
|
-
|
|
607
|
-
isAuthenticated,
|
|
608
|
-
login,
|
|
609
|
-
logout,
|
|
610
|
-
oauthLogin,
|
|
611
|
-
onAuthChange,
|
|
612
|
-
recoverPassword,
|
|
613
|
-
requestPasswordRecovery,
|
|
614
|
-
signup,
|
|
615
|
-
updateUser,
|
|
616
|
-
verifyEmailChange
|
|
153
|
+
isAuthenticated
|
|
617
154
|
};
|
|
618
155
|
//# sourceMappingURL=index.js.map
|