@fayz-ai/auth 0.47.0 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/README.md +4 -0
  2. package/dist/chunk-2MO4QTQA.js +78 -0
  3. package/dist/chunk-2MO4QTQA.js.map +1 -0
  4. package/dist/chunk-2S7KVJQO.js +18 -0
  5. package/dist/chunk-2S7KVJQO.js.map +1 -0
  6. package/dist/chunk-4HVM2UJS.js +30 -0
  7. package/dist/chunk-4HVM2UJS.js.map +1 -0
  8. package/dist/chunk-55RFOMW5.js +15 -0
  9. package/dist/chunk-55RFOMW5.js.map +1 -0
  10. package/dist/chunk-7JYNJQJJ.js +138 -0
  11. package/dist/chunk-7JYNJQJJ.js.map +1 -0
  12. package/dist/chunk-BGCCXJJA.js +174 -0
  13. package/dist/chunk-BGCCXJJA.js.map +1 -0
  14. package/dist/chunk-CV4LQYNX.js +109 -0
  15. package/dist/chunk-CV4LQYNX.js.map +1 -0
  16. package/dist/chunk-ERIPMYL2.js +137 -0
  17. package/dist/chunk-ERIPMYL2.js.map +1 -0
  18. package/dist/chunk-HKILBDG2.js +158 -0
  19. package/dist/chunk-HKILBDG2.js.map +1 -0
  20. package/dist/chunk-KLQDHFVS.js +11 -0
  21. package/dist/chunk-KLQDHFVS.js.map +1 -0
  22. package/dist/chunk-OI74ER2Z.js +31 -0
  23. package/dist/chunk-OI74ER2Z.js.map +1 -0
  24. package/dist/chunk-Q6DFI35B.js +187 -0
  25. package/dist/chunk-Q6DFI35B.js.map +1 -0
  26. package/dist/chunk-QJRTW7Q3.js +90 -0
  27. package/dist/chunk-QJRTW7Q3.js.map +1 -0
  28. package/dist/chunk-REU4D5UF.js +44 -0
  29. package/dist/chunk-REU4D5UF.js.map +1 -0
  30. package/dist/chunk-SMFXGZN7.js +18 -0
  31. package/dist/chunk-SMFXGZN7.js.map +1 -0
  32. package/dist/chunk-W35GQEGU.js +80 -0
  33. package/dist/chunk-W35GQEGU.js.map +1 -0
  34. package/dist/chunk-W7565FXD.js +64 -0
  35. package/dist/chunk-W7565FXD.js.map +1 -0
  36. package/dist/chunk-WZVZMNLB.js +48 -0
  37. package/dist/chunk-WZVZMNLB.js.map +1 -0
  38. package/dist/components/AmbassadorBubbles.d.ts +9 -0
  39. package/dist/components/AmbassadorBubbles.d.ts.map +1 -0
  40. package/dist/components/AmbassadorBubbles.js +3 -0
  41. package/dist/components/AmbassadorBubbles.js.map +1 -0
  42. package/dist/components/TrialPromise.d.ts +12 -0
  43. package/dist/components/TrialPromise.d.ts.map +1 -0
  44. package/dist/components/TrialPromise.js +3 -0
  45. package/dist/components/TrialPromise.js.map +1 -0
  46. package/dist/components/form-classes.d.ts +3 -0
  47. package/dist/components/form-classes.d.ts.map +1 -0
  48. package/dist/components/form-classes.js +3 -0
  49. package/dist/components/form-classes.js.map +1 -0
  50. package/dist/context.js +5 -0
  51. package/dist/context.js.map +1 -0
  52. package/dist/data/mock.d.ts.map +1 -0
  53. package/dist/data/mock.js +3 -0
  54. package/dist/data/mock.js.map +1 -0
  55. package/dist/data/supabase.d.ts.map +1 -0
  56. package/dist/data/supabase.js +4 -0
  57. package/dist/data/supabase.js.map +1 -0
  58. package/dist/domain/auth-config.d.ts +71 -0
  59. package/dist/domain/auth-config.d.ts.map +1 -0
  60. package/dist/domain/auth-config.js +3 -0
  61. package/dist/domain/auth-config.js.map +1 -0
  62. package/dist/domain/auth-link.d.ts +26 -0
  63. package/dist/domain/auth-link.d.ts.map +1 -0
  64. package/dist/domain/auth-link.js +3 -0
  65. package/dist/domain/auth-link.js.map +1 -0
  66. package/dist/domain/password.d.ts +5 -0
  67. package/dist/domain/password.d.ts.map +1 -0
  68. package/dist/domain/password.js +3 -0
  69. package/dist/domain/password.js.map +1 -0
  70. package/dist/index.d.ts +8 -6
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +6 -573
  73. package/dist/index.js.map +1 -1
  74. package/dist/lib/runtime.d.ts +6 -0
  75. package/dist/lib/runtime.d.ts.map +1 -0
  76. package/dist/lib/runtime.js +51 -0
  77. package/dist/lib/runtime.js.map +1 -0
  78. package/dist/lib/url-intent.d.ts +1 -25
  79. package/dist/lib/url-intent.d.ts.map +1 -1
  80. package/dist/lib/url-intent.js +3 -0
  81. package/dist/lib/url-intent.js.map +1 -0
  82. package/dist/store.js +3 -0
  83. package/dist/store.js.map +1 -0
  84. package/dist/views/AuthCallbackPage.d.ts +7 -0
  85. package/dist/views/AuthCallbackPage.d.ts.map +1 -0
  86. package/dist/views/AuthCallbackPage.js +33 -0
  87. package/dist/views/AuthCallbackPage.js.map +1 -0
  88. package/dist/views/AuthGate.d.ts +9 -0
  89. package/dist/views/AuthGate.d.ts.map +1 -0
  90. package/dist/views/AuthGate.js +28 -0
  91. package/dist/views/AuthGate.js.map +1 -0
  92. package/dist/views/ForgotPasswordForm.d.ts +18 -0
  93. package/dist/views/ForgotPasswordForm.d.ts.map +1 -0
  94. package/dist/views/ForgotPasswordForm.js +7 -0
  95. package/dist/views/ForgotPasswordForm.js.map +1 -0
  96. package/dist/views/LoginForm.d.ts +16 -0
  97. package/dist/views/LoginForm.d.ts.map +1 -0
  98. package/dist/views/LoginForm.js +8 -0
  99. package/dist/views/LoginForm.js.map +1 -0
  100. package/dist/views/LoginPage.d.ts +34 -0
  101. package/dist/views/LoginPage.d.ts.map +1 -0
  102. package/dist/views/LoginPage.js +16 -0
  103. package/dist/views/LoginPage.js.map +1 -0
  104. package/dist/views/OAuthButtons.d.ts +18 -0
  105. package/dist/views/OAuthButtons.d.ts.map +1 -0
  106. package/dist/views/OAuthButtons.js +7 -0
  107. package/dist/views/OAuthButtons.js.map +1 -0
  108. package/dist/views/ResetPasswordForm.d.ts +6 -0
  109. package/dist/views/ResetPasswordForm.d.ts.map +1 -0
  110. package/dist/views/ResetPasswordForm.js +53 -0
  111. package/dist/views/ResetPasswordForm.js.map +1 -0
  112. package/dist/views/ResetPasswordPage.d.ts +7 -0
  113. package/dist/views/ResetPasswordPage.d.ts.map +1 -0
  114. package/dist/views/ResetPasswordPage.js +16 -0
  115. package/dist/views/ResetPasswordPage.js.map +1 -0
  116. package/dist/views/SetPasswordPage.d.ts +15 -0
  117. package/dist/views/SetPasswordPage.d.ts.map +1 -0
  118. package/dist/views/SetPasswordPage.js +9 -0
  119. package/dist/views/SetPasswordPage.js.map +1 -0
  120. package/dist/views/SignupForm.d.ts +24 -0
  121. package/dist/views/SignupForm.d.ts.map +1 -0
  122. package/dist/views/SignupForm.js +9 -0
  123. package/dist/views/SignupForm.js.map +1 -0
  124. package/dist/views/VerifyEmailNotice.d.ts +15 -0
  125. package/dist/views/VerifyEmailNotice.d.ts.map +1 -0
  126. package/dist/views/VerifyEmailNotice.js +6 -0
  127. package/dist/views/VerifyEmailNotice.js.map +1 -0
  128. package/dist/views/usePasswordSetupGate.d.ts +20 -0
  129. package/dist/views/usePasswordSetupGate.d.ts.map +1 -0
  130. package/dist/views/usePasswordSetupGate.js +27 -0
  131. package/dist/views/usePasswordSetupGate.js.map +1 -0
  132. package/package.json +14 -3
  133. package/dist/adapters/mock.d.ts.map +0 -1
  134. package/dist/adapters/supabase.d.ts.map +0 -1
  135. /package/dist/{adapters → data}/mock.d.ts +0 -0
  136. /package/dist/{adapters → data}/supabase.d.ts +0 -0
package/dist/index.js CHANGED
@@ -1,575 +1,8 @@
1
- import { createClient } from '@supabase/supabase-js';
2
- import { isImpersonating } from '@fayz-ai/core';
3
- import { create } from 'zustand';
4
- import { createContext, useRef, useEffect, useContext } from 'react';
5
- import { jsx } from 'react/jsx-runtime';
6
-
7
- // src/adapters/supabase.ts
8
-
9
- // src/lib/url-intent.ts
10
- var AUTH_INTENT_PARAM = "fayz_auth";
11
- var RESET_PATH = "/auth/reset-password";
12
- function snapshot() {
13
- const loc = typeof window === "undefined" ? void 0 : window.location;
14
- if (!loc) return null;
15
- return {
16
- pathname: loc.pathname ?? "",
17
- search: loc.search ?? "",
18
- hash: loc.hash ?? ""
19
- };
20
- }
21
- var initialUrl = snapshot();
22
- function hashParams(hash) {
23
- const raw = hash.replace(/^#/, "");
24
- if (!raw || !raw.includes("=")) return new URLSearchParams();
25
- return new URLSearchParams(raw);
26
- }
27
- function readAuthUrlIntent(url) {
28
- const loc = url ?? initialUrl;
29
- if (!loc) return null;
30
- const query = new URLSearchParams(loc.search);
31
- const hash = hashParams(loc.hash);
32
- const param = (key) => hash.get(key) ?? query.get(key);
33
- const errorCode = param("error_code") ?? param("error");
34
- const errorDescription = param("error_description")?.replace(/\+/g, " ");
35
- if (errorCode && /otp|expired|link/i.test(`${errorCode} ${errorDescription ?? ""}`)) {
36
- return { kind: "expired", message: errorDescription ?? void 0 };
37
- }
38
- const marker = query.get(AUTH_INTENT_PARAM);
39
- const type = param("type");
40
- if (type === "recovery" || marker === "recovery") return { kind: "recovery" };
41
- const path = loc.pathname.replace(/\/+$/, "");
42
- if (path.endsWith(RESET_PATH)) return { kind: "recovery" };
43
- return null;
44
- }
45
- var APP_HANDOFF_PARAM = "handoff";
46
- var APP_HANDOFF_REFRESH_PARAM = "handoff_r";
47
- var APP_HANDOFF_TENANT_PARAM = "account";
48
- function readAppHandoff(url) {
49
- const loc = url ?? initialUrl;
50
- if (!loc) return null;
51
- const hash = hashParams(loc.hash);
52
- const accessToken = hash.get(APP_HANDOFF_PARAM);
53
- const refreshToken = hash.get(APP_HANDOFF_REFRESH_PARAM);
54
- if (!accessToken || !refreshToken) return null;
55
- return {
56
- accessToken,
57
- refreshToken,
58
- tenantId: hash.get(APP_HANDOFF_TENANT_PARAM) || null
59
- };
60
- }
61
- function buildAppHandoffUrl(appUrl, session, tenantId) {
62
- if (!session.accessToken || !session.refreshToken) return appUrl;
63
- const params = new URLSearchParams();
64
- params.set(APP_HANDOFF_PARAM, session.accessToken);
65
- params.set(APP_HANDOFF_REFRESH_PARAM, session.refreshToken);
66
- if (tenantId) params.set(APP_HANDOFF_TENANT_PARAM, tenantId);
67
- const base = appUrl.split("#")[0];
68
- return `${base}#${params.toString()}`;
69
- }
70
- function clearAuthUrlTokens() {
71
- if (typeof window === "undefined") return;
72
- const url = new URL(window.location.href);
73
- url.searchParams.delete(AUTH_INTENT_PARAM);
74
- if (url.hash.includes("=")) url.hash = "";
75
- window.history.replaceState(window.history.state, "", url.toString());
76
- }
77
- function withAuthIntent(url, intent) {
78
- const base = typeof window === "undefined" ? "http://localhost" : window.location.origin;
79
- const parsed = new URL(url, base);
80
- parsed.searchParams.set(AUTH_INTENT_PARAM, intent);
81
- return /^https?:\/\//i.test(url) ? parsed.toString() : `${parsed.pathname}${parsed.search}${parsed.hash}`;
82
- }
83
- var NOTHING = { reason: null, expired: false, checking: false };
84
- function decidePasswordSetup(input) {
85
- const { intent, isLoading, isAuthenticated, mustSetPassword } = input;
86
- if (intent?.kind === "expired") return { reason: "recovery", expired: true, checking: false };
87
- if (intent?.kind === "recovery") {
88
- if (isLoading) return { reason: "recovery", expired: false, checking: true };
89
- return { reason: "recovery", expired: !isAuthenticated, checking: false };
90
- }
91
- if (!isLoading && isAuthenticated && mustSetPassword) {
92
- return { reason: "invite", expired: false, checking: false };
93
- }
94
- return NOTHING;
95
- }
96
-
97
- // src/adapters/supabase.ts
98
- var MUST_SET_PASSWORD_FLAG = "fayz_must_set_password";
99
- function mapSupabaseUser(supabaseUser) {
100
- return {
101
- id: supabaseUser.id,
102
- email: supabaseUser.email ?? "",
103
- fullName: supabaseUser.user_metadata?.["full_name"] ?? supabaseUser.user_metadata?.["name"] ?? "",
104
- avatarUrl: supabaseUser.user_metadata?.["avatar_url"],
105
- // An admin viewing as this person must not be pushed into choosing THEIR password.
106
- mustSetPassword: supabaseUser.user_metadata?.[MUST_SET_PASSWORD_FLAG] === true && !isImpersonating()
107
- };
108
- }
109
- function mapSupabaseSession(session) {
110
- return {
111
- accessToken: session.access_token,
112
- refreshToken: session.refresh_token,
113
- expiresAt: session.expires_at ?? Math.floor(Date.now() / 1e3) + (session.expires_in ?? 3600),
114
- user: mapSupabaseUser(session.user)
115
- };
116
- }
117
- function createSupabaseAuthAdapter(config) {
118
- const supabase = config.supabaseClient ?? createClient(config.supabaseUrl, config.supabaseAnonKey);
119
- return {
120
- async getSession() {
121
- const {
122
- data: { session }
123
- } = await supabase.auth.getSession();
124
- if (!session) return null;
125
- const mapped = mapSupabaseSession(session);
126
- return { user: mapped.user, session: mapped };
127
- },
128
- onAuthStateChange(cb) {
129
- const {
130
- data: { subscription }
131
- } = supabase.auth.onAuthStateChange((_event, session) => {
132
- cb(session ? mapSupabaseUser(session.user) : null);
133
- });
134
- return () => subscription.unsubscribe();
135
- },
136
- async signIn(email, password) {
137
- const { data, error } = await supabase.auth.signInWithPassword({ email, password });
138
- if (error) throw error;
139
- return mapSupabaseSession(data.session);
140
- },
141
- /**
142
- * Adopt a session handed over by a sibling application (see url-intent).
143
- *
144
- * `setSession` is the verification, not a formality: GoTrue validates the
145
- * tokens and rotates the refresh token, so a forged or replayed fragment
146
- * fails here rather than being trusted because it looked well-formed.
147
- */
148
- async adoptSession(accessToken, refreshToken) {
149
- const { data, error } = await supabase.auth.setSession({
150
- access_token: accessToken,
151
- refresh_token: refreshToken
152
- });
153
- if (error || !data.session) return null;
154
- return mapSupabaseSession(data.session);
155
- },
156
- async signUp(email, password, fullName) {
157
- const { data, error } = await supabase.auth.signUp({
158
- email,
159
- password,
160
- options: { data: { full_name: fullName } }
161
- });
162
- if (error) throw error;
163
- if (!data.session) {
164
- if (!data.user) throw new Error("Signup failed: no user returned");
165
- return {
166
- accessToken: "",
167
- refreshToken: "",
168
- expiresAt: 0,
169
- user: mapSupabaseUser(data.user)
170
- };
171
- }
172
- return mapSupabaseSession(data.session);
173
- },
174
- async resendConfirmation(email, options) {
175
- const { error } = await supabase.auth.resend({
176
- type: "signup",
177
- email,
178
- options: {
179
- emailRedirectTo: options?.redirectTo ?? config.callbackUrl ?? (typeof window !== "undefined" ? window.location.origin : void 0)
180
- }
181
- });
182
- if (error) throw error;
183
- },
184
- async signOut() {
185
- const { error } = await supabase.auth.signOut();
186
- if (error) throw error;
187
- },
188
- async signInWithOAuth(provider) {
189
- const { error } = await supabase.auth.signInWithOAuth({
190
- provider,
191
- options: {
192
- redirectTo: config.callbackUrl ?? (typeof window !== "undefined" ? `${window.location.origin}/auth/callback` : void 0)
193
- }
194
- });
195
- if (error) throw error;
196
- },
197
- async resetPassword(email, options) {
198
- const explicit = options?.redirectTo ?? config.resetPasswordUrl;
199
- const { error } = await supabase.auth.resetPasswordForEmail(email, {
200
- redirectTo: explicit ? withAuthIntent(explicit, "recovery") : typeof window !== "undefined" ? `${window.location.origin}/` : void 0
201
- });
202
- if (error) throw error;
203
- },
204
- async updatePassword(password) {
205
- const { data, error } = await supabase.auth.updateUser({
206
- password,
207
- data: { [MUST_SET_PASSWORD_FLAG]: false }
208
- });
209
- if (error) throw error;
210
- const {
211
- data: { session }
212
- } = await supabase.auth.getSession();
213
- if (session) return mapSupabaseSession(session);
214
- if (!data.user) return void 0;
215
- return {
216
- accessToken: "",
217
- refreshToken: "",
218
- expiresAt: 0,
219
- user: mapSupabaseUser(data.user)
220
- };
221
- },
222
- async handleCallback(url) {
223
- const target = url ?? (typeof window !== "undefined" ? window.location.href : "");
224
- const parsed = new URL(target);
225
- const fragment = new URLSearchParams(parsed.hash.replace(/^#/, ""));
226
- const param = (key) => parsed.searchParams.get(key) ?? fragment.get(key);
227
- const code = param("code");
228
- if (code) {
229
- const { data, error } = await supabase.auth.exchangeCodeForSession(code);
230
- if (error) throw error;
231
- return data.session ? mapSupabaseSession(data.session) : null;
232
- }
233
- const tokenHash = param("token_hash") ?? param("token");
234
- const type = param("type");
235
- if (tokenHash && type) {
236
- const { data, error } = await supabase.auth.verifyOtp({
237
- token_hash: tokenHash,
238
- type
239
- });
240
- if (error) throw error;
241
- return data.session ? mapSupabaseSession(data.session) : null;
242
- }
243
- const {
244
- data: { session }
245
- } = await supabase.auth.getSession();
246
- return session ? mapSupabaseSession(session) : null;
247
- },
248
- async inviteUser(email, options) {
249
- const { error } = await supabase.auth.signInWithOtp({
250
- email,
251
- options: {
252
- shouldCreateUser: true,
253
- emailRedirectTo: options?.redirectTo ?? (typeof window !== "undefined" ? `${window.location.origin}/` : void 0),
254
- // `data` only lands on users the OTP actually creates, which is exactly
255
- // who needs a password: the shell holds them on the set-password screen
256
- // until they choose one, so they can sign in normally from then on.
257
- data: { ...options?.data, [MUST_SET_PASSWORD_FLAG]: true }
258
- }
259
- });
260
- if (error) throw error;
261
- }
262
- };
263
- }
264
-
265
- // src/adapters/mock.ts
266
- var DEFAULT_MOCK_USER = {
267
- id: "mock-user-id",
268
- email: "demo@fayz.dev",
269
- fullName: "Demo User",
270
- role: "admin"
271
- };
272
- var STORAGE_KEY = "fayz:mock-auth";
273
- function toAuthUser(mockUser) {
274
- return {
275
- id: mockUser.id,
276
- email: mockUser.email,
277
- fullName: mockUser.fullName
278
- };
279
- }
280
- function createMockSession(user) {
281
- return {
282
- accessToken: `mock-token-${user.id}`,
283
- refreshToken: `mock-refresh-${user.id}`,
284
- expiresAt: Math.floor(Date.now() / 1e3) + 3600,
285
- user
286
- };
287
- }
288
- function getStored() {
289
- try {
290
- if (typeof localStorage === "undefined") return null;
291
- const raw = localStorage.getItem(STORAGE_KEY);
292
- if (!raw) return null;
293
- return JSON.parse(raw);
294
- } catch {
295
- return null;
296
- }
297
- }
298
- function setStored(data) {
299
- try {
300
- if (typeof localStorage === "undefined") return;
301
- if (data) {
302
- localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
303
- } else {
304
- localStorage.removeItem(STORAGE_KEY);
305
- }
306
- } catch {
307
- }
308
- }
309
- function createMockAuthAdapter(mockUser = DEFAULT_MOCK_USER) {
310
- const listeners = /* @__PURE__ */ new Set();
311
- function notifyListeners(user) {
312
- for (const cb of listeners) cb(user);
313
- }
314
- return {
315
- async getSession() {
316
- return getStored();
317
- },
318
- onAuthStateChange(cb) {
319
- listeners.add(cb);
320
- return () => {
321
- listeners.delete(cb);
322
- };
323
- },
324
- async signIn(email, _password) {
325
- const stored = getStored();
326
- const user = stored && stored.user.email === email ? stored.user : {
327
- id: email === mockUser.email ? mockUser.id : `mock-${email}`,
328
- email,
329
- fullName: email === mockUser.email ? mockUser.fullName : email.split("@")[0] ?? "Cliente"
330
- };
331
- const session = createMockSession(user);
332
- setStored({ user, session });
333
- notifyListeners(user);
334
- return session;
335
- },
336
- async signUp(email, _password, fullName) {
337
- const user = {
338
- id: `mock-${email}`,
339
- email,
340
- fullName: fullName || (email.split("@")[0] ?? "Cliente")
341
- };
342
- const session = createMockSession(user);
343
- setStored({ user, session });
344
- notifyListeners(user);
345
- return session;
346
- },
347
- async signOut() {
348
- setStored(null);
349
- notifyListeners(null);
350
- },
351
- async signInWithOAuth(_provider) {
352
- const user = toAuthUser(mockUser);
353
- const session = createMockSession(user);
354
- setStored({ user, session });
355
- notifyListeners(user);
356
- },
357
- async resetPassword(_email) {
358
- },
359
- async updatePassword(_password) {
360
- const stored = getStored();
361
- return stored?.session;
362
- },
363
- async handleCallback() {
364
- return getStored()?.session ?? null;
365
- },
366
- async inviteUser(_email, _options) {
367
- }
368
- };
369
- }
370
- var initialState = {
371
- user: null,
372
- session: null,
373
- isLoading: true,
374
- isAuthenticated: false,
375
- error: null,
376
- pendingVerification: null
377
- };
378
- var useAuthStore = create((set) => ({
379
- ...initialState,
380
- setUser: (user) => set({ user, isAuthenticated: user !== null }),
381
- setSession: (session) => set({
382
- session,
383
- user: session?.user ?? null,
384
- isAuthenticated: session !== null,
385
- // A real session arriving is the confirmation landing — the notice it
386
- // would otherwise keep showing is now about an address already verified.
387
- ...session ? { pendingVerification: null } : {}
388
- }),
389
- setLoading: (isLoading) => set({ isLoading }),
390
- setError: (error) => set({ error }),
391
- setPendingVerification: (pendingVerification) => set({ pendingVerification }),
392
- reset: () => set(initialState)
393
- }));
394
- var AuthContext = createContext(null);
395
- function AuthProvider({ adapter, children }) {
396
- const { setSession, setLoading, setError, setPendingVerification, reset } = useAuthStore();
397
- const adapterRef = useRef(adapter);
398
- adapterRef.current = adapter;
399
- useEffect(() => {
400
- let unsubscribe;
401
- async function init() {
402
- try {
403
- setLoading(true);
404
- const handoff = readAppHandoff();
405
- const adopt = adapterRef.current.adoptSession;
406
- let adopted = null;
407
- if (handoff && typeof adopt === "function") {
408
- adopted = await adopt.call(adapterRef.current, handoff.accessToken, handoff.refreshToken);
409
- clearAuthUrlTokens();
410
- }
411
- const result = adopted ? { session: adopted } : await adapterRef.current.getSession();
412
- setSession(result?.session ?? null);
413
- } catch (err) {
414
- setError(err instanceof Error ? err : new Error(String(err)));
415
- } finally {
416
- setLoading(false);
417
- }
418
- unsubscribe = adapterRef.current.onAuthStateChange((user) => {
419
- if (!user) {
420
- reset();
421
- setLoading(false);
422
- return;
423
- }
424
- void adapterRef.current.getSession().then((result) => setSession(result?.session ?? null)).catch(() => {
425
- });
426
- });
427
- }
428
- void init();
429
- return () => {
430
- unsubscribe?.();
431
- };
432
- }, []);
433
- const value = {
434
- async signIn(email, password) {
435
- setError(null);
436
- try {
437
- const session = await adapterRef.current.signIn(email, password);
438
- setSession(session);
439
- return session;
440
- } catch (err) {
441
- const error = err instanceof Error ? err : new Error(String(err));
442
- setError(error);
443
- throw error;
444
- }
445
- },
446
- async signUp(email, password, fullName) {
447
- setError(null);
448
- try {
449
- const session = await adapterRef.current.signUp(email, password, fullName);
450
- if (!session.accessToken) {
451
- setPendingVerification({ email });
452
- return session;
453
- }
454
- setSession(session);
455
- return session;
456
- } catch (err) {
457
- const error = err instanceof Error ? err : new Error(String(err));
458
- setError(error);
459
- throw error;
460
- }
461
- },
462
- async signOut() {
463
- setError(null);
464
- try {
465
- await adapterRef.current.signOut();
466
- reset();
467
- setLoading(false);
468
- } catch (err) {
469
- const error = err instanceof Error ? err : new Error(String(err));
470
- setError(error);
471
- throw error;
472
- }
473
- },
474
- async signInWithOAuth(provider) {
475
- setError(null);
476
- try {
477
- await adapterRef.current.signInWithOAuth(provider);
478
- } catch (err) {
479
- const error = err instanceof Error ? err : new Error(String(err));
480
- setError(error);
481
- throw error;
482
- }
483
- },
484
- async resetPassword(email, options) {
485
- setError(null);
486
- try {
487
- await adapterRef.current.resetPassword(email, options);
488
- } catch (err) {
489
- const error = err instanceof Error ? err : new Error(String(err));
490
- setError(error);
491
- throw error;
492
- }
493
- },
494
- async updatePassword(password) {
495
- setError(null);
496
- try {
497
- const session = await adapterRef.current.updatePassword?.(password);
498
- if (session) setSession(session);
499
- return session;
500
- } catch (err) {
501
- const error = err instanceof Error ? err : new Error(String(err));
502
- setError(error);
503
- throw error;
504
- }
505
- },
506
- async handleCallback(url) {
507
- setError(null);
508
- try {
509
- const session = await adapterRef.current.handleCallback?.(url) ?? null;
510
- if (session) setSession(session);
511
- return session;
512
- } catch (err) {
513
- const error = err instanceof Error ? err : new Error(String(err));
514
- setError(error);
515
- throw error;
516
- }
517
- },
518
- async inviteUser(email, options) {
519
- setError(null);
520
- try {
521
- await adapterRef.current.inviteUser?.(email, options);
522
- } catch (err) {
523
- const error = err instanceof Error ? err : new Error(String(err));
524
- setError(error);
525
- throw error;
526
- }
527
- },
528
- async resendConfirmation(email, options) {
529
- setError(null);
530
- if (!adapterRef.current.resendConfirmation) return false;
531
- try {
532
- await adapterRef.current.resendConfirmation(email, options);
533
- return true;
534
- } catch (err) {
535
- const error = err instanceof Error ? err : new Error(String(err));
536
- setError(error);
537
- throw error;
538
- }
539
- },
540
- clearPendingVerification() {
541
- setPendingVerification(null);
542
- setError(null);
543
- }
544
- };
545
- return /* @__PURE__ */ jsx(AuthContext.Provider, { value, children });
546
- }
547
- function useAuth() {
548
- const ctx = useContext(AuthContext);
549
- if (!ctx) {
550
- throw new Error("useAuth must be used inside <AuthProvider>");
551
- }
552
- const { user, session, isLoading, isAuthenticated, error, pendingVerification } = useAuthStore();
553
- return {
554
- user,
555
- session,
556
- isLoading,
557
- isAuthenticated,
558
- error,
559
- pendingVerification,
560
- signIn: ctx.signIn,
561
- signUp: ctx.signUp,
562
- signOut: ctx.signOut,
563
- signInWithOAuth: ctx.signInWithOAuth,
564
- resetPassword: ctx.resetPassword,
565
- updatePassword: ctx.updatePassword,
566
- handleCallback: ctx.handleCallback,
567
- inviteUser: ctx.inviteUser,
568
- resendConfirmation: ctx.resendConfirmation,
569
- clearPendingVerification: ctx.clearPendingVerification
570
- };
571
- }
572
-
573
- export { APP_HANDOFF_PARAM, APP_HANDOFF_REFRESH_PARAM, APP_HANDOFF_TENANT_PARAM, AUTH_INTENT_PARAM, AuthProvider, MUST_SET_PASSWORD_FLAG, buildAppHandoffUrl, clearAuthUrlTokens, createMockAuthAdapter, createSupabaseAuthAdapter, decidePasswordSetup, readAppHandoff, readAuthUrlIntent, useAuth, useAuthStore, withAuthIntent };
1
+ export { decidePasswordSetup } from './chunk-2S7KVJQO.js';
2
+ export { AuthProvider, useAuth } from './chunk-Q6DFI35B.js';
3
+ export { useAuthStore } from './chunk-OI74ER2Z.js';
4
+ export { createMockAuthAdapter } from './chunk-CV4LQYNX.js';
5
+ export { MUST_SET_PASSWORD_FLAG, createSupabaseAuthAdapter } from './chunk-BGCCXJJA.js';
6
+ export { APP_HANDOFF_PARAM, APP_HANDOFF_REFRESH_PARAM, APP_HANDOFF_TENANT_PARAM, AUTH_INTENT_PARAM, buildAppHandoffUrl, clearAuthUrlTokens, readAppHandoff, readAuthUrlIntent, withAuthIntent } from './chunk-2MO4QTQA.js';
574
7
  //# sourceMappingURL=index.js.map
575
8
  //# sourceMappingURL=index.js.map