@digilogiclabs/platform-core 1.26.0 → 1.27.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.
- package/README.md +12 -6
- package/dist/{ConsoleEmail-iJO7Cb_v.d.ts → ConsoleEmail-BRa76Bfm.d.ts} +3 -106
- package/dist/{ConsoleEmail-CgSIvRdj.d.mts → ConsoleEmail-DSmK5jbu.d.mts} +3 -106
- package/dist/IDatabase-yOhemn5C.d.mts +49 -0
- package/dist/IDatabase-yOhemn5C.d.ts +49 -0
- package/dist/IStorage-CcFAvKDp.d.mts +58 -0
- package/dist/IStorage-CcFAvKDp.d.ts +58 -0
- package/dist/adapters/supabase-auth.d.mts +2 -0
- package/dist/adapters/supabase-auth.d.ts +2 -0
- package/dist/adapters/supabase-auth.js +714 -0
- package/dist/adapters/supabase-auth.js.map +1 -0
- package/dist/adapters/supabase-auth.mjs +687 -0
- package/dist/adapters/supabase-auth.mjs.map +1 -0
- package/dist/adapters/supabase-storage.d.mts +93 -0
- package/dist/adapters/supabase-storage.d.ts +93 -0
- package/dist/adapters/supabase-storage.js +249 -0
- package/dist/adapters/supabase-storage.js.map +1 -0
- package/dist/adapters/supabase-storage.mjs +222 -0
- package/dist/adapters/supabase-storage.mjs.map +1 -0
- package/dist/adapters/supabase.d.mts +24 -0
- package/dist/adapters/supabase.d.ts +24 -0
- package/dist/adapters/supabase.js +218 -0
- package/dist/adapters/supabase.js.map +1 -0
- package/dist/adapters/supabase.mjs +191 -0
- package/dist/adapters/supabase.mjs.map +1 -0
- package/dist/index-cN9dgBsn.d.mts +612 -0
- package/dist/index-cN9dgBsn.d.ts +612 -0
- package/dist/index.d.mts +10 -717
- package/dist/index.d.ts +10 -717
- package/dist/index.js +12 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -5
- package/dist/index.mjs.map +1 -1
- package/dist/migrate.js +0 -0
- package/dist/testing.d.mts +5 -2
- package/dist/testing.d.ts +5 -2
- package/dist/testing.js +1 -0
- package/dist/testing.js.map +1 -1
- package/dist/testing.mjs +1 -0
- package/dist/testing.mjs.map +1 -1
- package/package.json +26 -11
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
// src/interfaces/IAuth.ts
|
|
2
|
+
function createAuthError(code, message, originalError) {
|
|
3
|
+
return { code, message, originalError };
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/adapters/supabase-auth/SupabaseAuth.ts
|
|
7
|
+
function mapSupabaseError(error) {
|
|
8
|
+
const message = error.message || "An unknown error occurred";
|
|
9
|
+
if (message.includes("Invalid login credentials")) {
|
|
10
|
+
return createAuthError("invalid_credentials", "Invalid email or password");
|
|
11
|
+
}
|
|
12
|
+
if (message.includes("User not found")) {
|
|
13
|
+
return createAuthError("user_not_found", "User not found");
|
|
14
|
+
}
|
|
15
|
+
if (message.includes("Email not confirmed")) {
|
|
16
|
+
return createAuthError(
|
|
17
|
+
"email_not_confirmed",
|
|
18
|
+
"Please verify your email address"
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
if (message.includes("already registered") || message.includes("already exists")) {
|
|
22
|
+
return createAuthError(
|
|
23
|
+
"email_already_exists",
|
|
24
|
+
"An account with this email already exists"
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
if (message.includes("Password should be")) {
|
|
28
|
+
return createAuthError("weak_password", "Password is too weak");
|
|
29
|
+
}
|
|
30
|
+
if (message.includes("Token expired") || message.includes("expired")) {
|
|
31
|
+
return createAuthError("expired_token", "Token has expired");
|
|
32
|
+
}
|
|
33
|
+
if (message.includes("Invalid token") || message.includes("invalid")) {
|
|
34
|
+
return createAuthError("invalid_token", "Invalid token");
|
|
35
|
+
}
|
|
36
|
+
if (error.status === 429 || message.includes("rate limit")) {
|
|
37
|
+
return createAuthError(
|
|
38
|
+
"rate_limited",
|
|
39
|
+
"Too many requests. Please try again later."
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
if (error.status && error.status >= 500) {
|
|
43
|
+
return createAuthError("server_error", "Server error. Please try again.");
|
|
44
|
+
}
|
|
45
|
+
if (message.includes("disabled")) {
|
|
46
|
+
return createAuthError("user_disabled", "This account has been disabled");
|
|
47
|
+
}
|
|
48
|
+
return createAuthError("unknown_error", message, error);
|
|
49
|
+
}
|
|
50
|
+
function mapSupabaseUser(user) {
|
|
51
|
+
return {
|
|
52
|
+
id: user.id,
|
|
53
|
+
email: user.email || null,
|
|
54
|
+
emailVerified: !!user.email_confirmed_at,
|
|
55
|
+
phone: user.phone || null,
|
|
56
|
+
phoneVerified: !!user.phone_confirmed_at,
|
|
57
|
+
displayName: user.user_metadata?.full_name || user.user_metadata?.name || null,
|
|
58
|
+
avatarUrl: user.user_metadata?.avatar_url || null,
|
|
59
|
+
metadata: user.user_metadata || {},
|
|
60
|
+
createdAt: new Date(user.created_at),
|
|
61
|
+
lastSignInAt: user.last_sign_in_at ? new Date(user.last_sign_in_at) : void 0
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function mapSupabaseSession(session) {
|
|
65
|
+
return {
|
|
66
|
+
accessToken: session.access_token,
|
|
67
|
+
refreshToken: session.refresh_token,
|
|
68
|
+
tokenType: session.token_type,
|
|
69
|
+
expiresAt: new Date(session.expires_at * 1e3),
|
|
70
|
+
user: mapSupabaseUser(session.user)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
var SupabaseAuth = class {
|
|
74
|
+
client;
|
|
75
|
+
constructor(client) {
|
|
76
|
+
this.client = client;
|
|
77
|
+
}
|
|
78
|
+
// ─────────────────────────────────────────────────────────────
|
|
79
|
+
// Session Management
|
|
80
|
+
// ─────────────────────────────────────────────────────────────
|
|
81
|
+
async getCurrentUser() {
|
|
82
|
+
try {
|
|
83
|
+
const {
|
|
84
|
+
data: { user },
|
|
85
|
+
error
|
|
86
|
+
} = await this.client.auth.getUser();
|
|
87
|
+
if (error || !user) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
return mapSupabaseUser(user);
|
|
91
|
+
} catch {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async getSession() {
|
|
96
|
+
try {
|
|
97
|
+
const {
|
|
98
|
+
data: { session },
|
|
99
|
+
error
|
|
100
|
+
} = await this.client.auth.getSession();
|
|
101
|
+
if (error || !session) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return mapSupabaseSession(session);
|
|
105
|
+
} catch {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async refreshSession() {
|
|
110
|
+
try {
|
|
111
|
+
const {
|
|
112
|
+
data: { session },
|
|
113
|
+
error
|
|
114
|
+
} = await this.client.auth.refreshSession();
|
|
115
|
+
if (error || !session) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
return mapSupabaseSession(session);
|
|
119
|
+
} catch {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async signOut(options) {
|
|
124
|
+
await this.client.auth.signOut({
|
|
125
|
+
scope: options?.scope
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
// ─────────────────────────────────────────────────────────────
|
|
129
|
+
// Email/Password Authentication
|
|
130
|
+
// ─────────────────────────────────────────────────────────────
|
|
131
|
+
async signInWithEmail(options) {
|
|
132
|
+
try {
|
|
133
|
+
const { data, error } = await this.client.auth.signInWithPassword({
|
|
134
|
+
email: options.email,
|
|
135
|
+
password: options.password,
|
|
136
|
+
options: options.captchaToken ? { captchaToken: options.captchaToken } : void 0
|
|
137
|
+
});
|
|
138
|
+
if (error || !data.user) {
|
|
139
|
+
return {
|
|
140
|
+
success: false,
|
|
141
|
+
error: error ? mapSupabaseError(error) : createAuthError("invalid_credentials", "Invalid credentials")
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
success: true,
|
|
146
|
+
user: mapSupabaseUser(data.user),
|
|
147
|
+
session: data.session ? mapSupabaseSession(data.session) : void 0
|
|
148
|
+
};
|
|
149
|
+
} catch (error) {
|
|
150
|
+
return {
|
|
151
|
+
success: false,
|
|
152
|
+
error: createAuthError(
|
|
153
|
+
"server_error",
|
|
154
|
+
error instanceof Error ? error.message : "Server error"
|
|
155
|
+
)
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async signUpWithEmail(options) {
|
|
160
|
+
try {
|
|
161
|
+
const { data, error } = await this.client.auth.signUp({
|
|
162
|
+
email: options.email,
|
|
163
|
+
password: options.password,
|
|
164
|
+
options: {
|
|
165
|
+
data: options.metadata,
|
|
166
|
+
emailRedirectTo: options.emailRedirectTo,
|
|
167
|
+
captchaToken: options.captchaToken
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
if (error) {
|
|
171
|
+
return {
|
|
172
|
+
success: false,
|
|
173
|
+
error: mapSupabaseError(error)
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
success: true,
|
|
178
|
+
user: data.user ? mapSupabaseUser(data.user) : void 0,
|
|
179
|
+
session: data.session ? mapSupabaseSession(data.session) : void 0,
|
|
180
|
+
needsEmailVerification: !data.session
|
|
181
|
+
// No session means email confirmation required
|
|
182
|
+
};
|
|
183
|
+
} catch (error) {
|
|
184
|
+
return {
|
|
185
|
+
success: false,
|
|
186
|
+
error: createAuthError(
|
|
187
|
+
"server_error",
|
|
188
|
+
error instanceof Error ? error.message : "Server error"
|
|
189
|
+
)
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async resetPassword(options) {
|
|
194
|
+
try {
|
|
195
|
+
const { error } = await this.client.auth.resetPasswordForEmail(
|
|
196
|
+
options.email,
|
|
197
|
+
{
|
|
198
|
+
redirectTo: options.redirectTo,
|
|
199
|
+
captchaToken: options.captchaToken
|
|
200
|
+
}
|
|
201
|
+
);
|
|
202
|
+
if (error) {
|
|
203
|
+
return {
|
|
204
|
+
success: false,
|
|
205
|
+
error: mapSupabaseError(error)
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return { success: true };
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return {
|
|
211
|
+
success: false,
|
|
212
|
+
error: createAuthError(
|
|
213
|
+
"server_error",
|
|
214
|
+
error instanceof Error ? error.message : "Server error"
|
|
215
|
+
)
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async updatePassword(options) {
|
|
220
|
+
try {
|
|
221
|
+
const { data, error } = await this.client.auth.updateUser({
|
|
222
|
+
password: options.password
|
|
223
|
+
});
|
|
224
|
+
if (error) {
|
|
225
|
+
return {
|
|
226
|
+
success: false,
|
|
227
|
+
error: mapSupabaseError(error)
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
success: true,
|
|
232
|
+
user: data.user ? mapSupabaseUser(data.user) : void 0
|
|
233
|
+
};
|
|
234
|
+
} catch (error) {
|
|
235
|
+
return {
|
|
236
|
+
success: false,
|
|
237
|
+
error: createAuthError(
|
|
238
|
+
"server_error",
|
|
239
|
+
error instanceof Error ? error.message : "Server error"
|
|
240
|
+
)
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// ─────────────────────────────────────────────────────────────
|
|
245
|
+
// OAuth Authentication
|
|
246
|
+
// ─────────────────────────────────────────────────────────────
|
|
247
|
+
async signInWithOAuth(provider, options) {
|
|
248
|
+
const { data, error } = await this.client.auth.signInWithOAuth({
|
|
249
|
+
provider,
|
|
250
|
+
options: {
|
|
251
|
+
redirectTo: options?.redirectTo,
|
|
252
|
+
scopes: options?.scopes,
|
|
253
|
+
queryParams: options?.queryParams
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
if (error || !data.url) {
|
|
257
|
+
throw new Error(error?.message || "Failed to initiate OAuth");
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
url: data.url,
|
|
261
|
+
provider
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
async handleOAuthCallback(code) {
|
|
265
|
+
try {
|
|
266
|
+
const { data, error } = await this.client.auth.exchangeCodeForSession(code);
|
|
267
|
+
if (error || !data.user) {
|
|
268
|
+
return {
|
|
269
|
+
success: false,
|
|
270
|
+
error: error ? mapSupabaseError(error) : createAuthError("oauth_error", "Failed to exchange code")
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
success: true,
|
|
275
|
+
user: mapSupabaseUser(data.user),
|
|
276
|
+
session: data.session ? mapSupabaseSession(data.session) : void 0
|
|
277
|
+
};
|
|
278
|
+
} catch (error) {
|
|
279
|
+
return {
|
|
280
|
+
success: false,
|
|
281
|
+
error: createAuthError(
|
|
282
|
+
"server_error",
|
|
283
|
+
error instanceof Error ? error.message : "Server error"
|
|
284
|
+
)
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// ─────────────────────────────────────────────────────────────
|
|
289
|
+
// Magic Link Authentication
|
|
290
|
+
// ─────────────────────────────────────────────────────────────
|
|
291
|
+
async signInWithMagicLink(options) {
|
|
292
|
+
try {
|
|
293
|
+
const { error } = await this.client.auth.signInWithOtp({
|
|
294
|
+
email: options.email,
|
|
295
|
+
options: {
|
|
296
|
+
emailRedirectTo: options.redirectTo,
|
|
297
|
+
captchaToken: options.captchaToken
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
if (error) {
|
|
301
|
+
return {
|
|
302
|
+
success: false,
|
|
303
|
+
error: mapSupabaseError(error)
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
return { success: true };
|
|
307
|
+
} catch (error) {
|
|
308
|
+
return {
|
|
309
|
+
success: false,
|
|
310
|
+
error: createAuthError(
|
|
311
|
+
"server_error",
|
|
312
|
+
error instanceof Error ? error.message : "Server error"
|
|
313
|
+
)
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async verifyMagicLink(token) {
|
|
318
|
+
try {
|
|
319
|
+
const { data, error } = await this.client.auth.verifyOtp({
|
|
320
|
+
token_hash: token,
|
|
321
|
+
type: "email"
|
|
322
|
+
});
|
|
323
|
+
if (error || !data.user) {
|
|
324
|
+
return {
|
|
325
|
+
success: false,
|
|
326
|
+
error: error ? mapSupabaseError(error) : createAuthError("invalid_token", "Invalid token")
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
success: true,
|
|
331
|
+
user: mapSupabaseUser(data.user),
|
|
332
|
+
session: data.session ? mapSupabaseSession(data.session) : void 0
|
|
333
|
+
};
|
|
334
|
+
} catch (error) {
|
|
335
|
+
return {
|
|
336
|
+
success: false,
|
|
337
|
+
error: createAuthError(
|
|
338
|
+
"server_error",
|
|
339
|
+
error instanceof Error ? error.message : "Server error"
|
|
340
|
+
)
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
// ─────────────────────────────────────────────────────────────
|
|
345
|
+
// Phone Authentication
|
|
346
|
+
// ─────────────────────────────────────────────────────────────
|
|
347
|
+
async signInWithPhone(options) {
|
|
348
|
+
try {
|
|
349
|
+
const { error } = await this.client.auth.signInWithOtp({
|
|
350
|
+
phone: options.phone,
|
|
351
|
+
options: {
|
|
352
|
+
captchaToken: options.captchaToken
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
if (error) {
|
|
356
|
+
return {
|
|
357
|
+
success: false,
|
|
358
|
+
error: mapSupabaseError(error)
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
return { success: true };
|
|
362
|
+
} catch (error) {
|
|
363
|
+
return {
|
|
364
|
+
success: false,
|
|
365
|
+
error: createAuthError(
|
|
366
|
+
"server_error",
|
|
367
|
+
error instanceof Error ? error.message : "Server error"
|
|
368
|
+
)
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
async verifyPhoneOtp(options) {
|
|
373
|
+
try {
|
|
374
|
+
const { data, error } = await this.client.auth.verifyOtp({
|
|
375
|
+
phone: options.phone,
|
|
376
|
+
token: options.token,
|
|
377
|
+
type: options.type === "phone_change" ? "phone_change" : "sms"
|
|
378
|
+
});
|
|
379
|
+
if (error || !data.user) {
|
|
380
|
+
return {
|
|
381
|
+
success: false,
|
|
382
|
+
error: error ? mapSupabaseError(error) : createAuthError("invalid_token", "Invalid token")
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
success: true,
|
|
387
|
+
user: mapSupabaseUser(data.user),
|
|
388
|
+
session: data.session ? mapSupabaseSession(data.session) : void 0
|
|
389
|
+
};
|
|
390
|
+
} catch (error) {
|
|
391
|
+
return {
|
|
392
|
+
success: false,
|
|
393
|
+
error: createAuthError(
|
|
394
|
+
"server_error",
|
|
395
|
+
error instanceof Error ? error.message : "Server error"
|
|
396
|
+
)
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
// ─────────────────────────────────────────────────────────────
|
|
401
|
+
// Email Verification
|
|
402
|
+
// ─────────────────────────────────────────────────────────────
|
|
403
|
+
async verifyEmail(token) {
|
|
404
|
+
try {
|
|
405
|
+
const { data, error } = await this.client.auth.verifyOtp({
|
|
406
|
+
token_hash: token,
|
|
407
|
+
type: "email"
|
|
408
|
+
});
|
|
409
|
+
if (error) {
|
|
410
|
+
return {
|
|
411
|
+
success: false,
|
|
412
|
+
error: mapSupabaseError(error)
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
return {
|
|
416
|
+
success: true,
|
|
417
|
+
user: data.user ? mapSupabaseUser(data.user) : void 0,
|
|
418
|
+
session: data.session ? mapSupabaseSession(data.session) : void 0
|
|
419
|
+
};
|
|
420
|
+
} catch (error) {
|
|
421
|
+
return {
|
|
422
|
+
success: false,
|
|
423
|
+
error: createAuthError(
|
|
424
|
+
"server_error",
|
|
425
|
+
error instanceof Error ? error.message : "Server error"
|
|
426
|
+
)
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
async resendVerification(email) {
|
|
431
|
+
try {
|
|
432
|
+
const { error } = await this.client.auth.resend({
|
|
433
|
+
type: "signup",
|
|
434
|
+
email
|
|
435
|
+
});
|
|
436
|
+
if (error) {
|
|
437
|
+
return {
|
|
438
|
+
success: false,
|
|
439
|
+
error: mapSupabaseError(error)
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
return { success: true };
|
|
443
|
+
} catch (error) {
|
|
444
|
+
return {
|
|
445
|
+
success: false,
|
|
446
|
+
error: createAuthError(
|
|
447
|
+
"server_error",
|
|
448
|
+
error instanceof Error ? error.message : "Server error"
|
|
449
|
+
)
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
// ─────────────────────────────────────────────────────────────
|
|
454
|
+
// Multi-Factor Authentication
|
|
455
|
+
// ─────────────────────────────────────────────────────────────
|
|
456
|
+
async listMfaFactors() {
|
|
457
|
+
const { data, error } = await this.client.auth.mfa.listFactors();
|
|
458
|
+
if (error || !data) {
|
|
459
|
+
return [];
|
|
460
|
+
}
|
|
461
|
+
return data.all.map((factor) => ({
|
|
462
|
+
id: factor.id,
|
|
463
|
+
type: factor.factor_type,
|
|
464
|
+
name: factor.friendly_name || void 0,
|
|
465
|
+
verified: factor.status === "verified",
|
|
466
|
+
createdAt: new Date(factor.created_at)
|
|
467
|
+
}));
|
|
468
|
+
}
|
|
469
|
+
async enrollMfa(options) {
|
|
470
|
+
if (options.type === "totp") {
|
|
471
|
+
const { data: data2, error: error2 } = await this.client.auth.mfa.enroll({
|
|
472
|
+
factorType: "totp",
|
|
473
|
+
friendlyName: options.friendlyName
|
|
474
|
+
});
|
|
475
|
+
if (error2 || !data2) {
|
|
476
|
+
throw new Error(error2?.message || "Failed to enroll MFA");
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
factorId: data2.id,
|
|
480
|
+
totpSecret: data2.totp.secret,
|
|
481
|
+
totpQrCode: data2.totp.qr_code,
|
|
482
|
+
needsVerification: true
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
const { data, error } = await this.client.auth.mfa.enroll({
|
|
486
|
+
factorType: "phone",
|
|
487
|
+
friendlyName: options.friendlyName,
|
|
488
|
+
phone: options.phone
|
|
489
|
+
});
|
|
490
|
+
if (error || !data) {
|
|
491
|
+
throw new Error(error?.message || "Failed to enroll MFA");
|
|
492
|
+
}
|
|
493
|
+
return {
|
|
494
|
+
factorId: data.id,
|
|
495
|
+
needsVerification: true
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
async verifyMfa(options) {
|
|
499
|
+
try {
|
|
500
|
+
let challengeId = options.challengeId;
|
|
501
|
+
if (!challengeId) {
|
|
502
|
+
const { data: challengeData, error: challengeError } = await this.client.auth.mfa.challenge({
|
|
503
|
+
factorId: options.factorId
|
|
504
|
+
});
|
|
505
|
+
if (challengeError || !challengeData) {
|
|
506
|
+
return {
|
|
507
|
+
success: false,
|
|
508
|
+
error: createAuthError(
|
|
509
|
+
"mfa_invalid",
|
|
510
|
+
challengeError?.message || "Failed to create challenge"
|
|
511
|
+
)
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
challengeId = challengeData.id;
|
|
515
|
+
}
|
|
516
|
+
const { data, error } = await this.client.auth.mfa.verify({
|
|
517
|
+
factorId: options.factorId,
|
|
518
|
+
challengeId,
|
|
519
|
+
code: options.code
|
|
520
|
+
});
|
|
521
|
+
if (error || !data) {
|
|
522
|
+
return {
|
|
523
|
+
success: false,
|
|
524
|
+
error: createAuthError(
|
|
525
|
+
"mfa_invalid",
|
|
526
|
+
error?.message || "Invalid MFA code"
|
|
527
|
+
)
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
const user = await this.getCurrentUser();
|
|
531
|
+
const session = await this.getSession();
|
|
532
|
+
return {
|
|
533
|
+
success: true,
|
|
534
|
+
user: user || void 0,
|
|
535
|
+
session: session || void 0
|
|
536
|
+
};
|
|
537
|
+
} catch (error) {
|
|
538
|
+
return {
|
|
539
|
+
success: false,
|
|
540
|
+
error: createAuthError(
|
|
541
|
+
"server_error",
|
|
542
|
+
error instanceof Error ? error.message : "Server error"
|
|
543
|
+
)
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
async unenrollMfa(factorId) {
|
|
548
|
+
const { error } = await this.client.auth.mfa.unenroll({
|
|
549
|
+
factorId
|
|
550
|
+
});
|
|
551
|
+
if (error) {
|
|
552
|
+
throw new Error(error.message);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
async createMfaChallenge(factorId) {
|
|
556
|
+
const { data, error } = await this.client.auth.mfa.challenge({
|
|
557
|
+
factorId
|
|
558
|
+
});
|
|
559
|
+
if (error || !data) {
|
|
560
|
+
throw new Error(error?.message || "Failed to create challenge");
|
|
561
|
+
}
|
|
562
|
+
return {
|
|
563
|
+
id: data.id,
|
|
564
|
+
factorId,
|
|
565
|
+
type: "totp",
|
|
566
|
+
// Supabase currently only supports TOTP
|
|
567
|
+
expiresAt: new Date(data.expires_at)
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
// ─────────────────────────────────────────────────────────────
|
|
571
|
+
// User Management
|
|
572
|
+
// ─────────────────────────────────────────────────────────────
|
|
573
|
+
async updateUser(options) {
|
|
574
|
+
try {
|
|
575
|
+
const updateData = {};
|
|
576
|
+
if (options.email) {
|
|
577
|
+
updateData.email = options.email;
|
|
578
|
+
}
|
|
579
|
+
if (options.phone) {
|
|
580
|
+
updateData.phone = options.phone;
|
|
581
|
+
}
|
|
582
|
+
if (options.displayName || options.avatarUrl || options.metadata) {
|
|
583
|
+
updateData.data = {
|
|
584
|
+
...options.displayName && { full_name: options.displayName },
|
|
585
|
+
...options.avatarUrl && { avatar_url: options.avatarUrl },
|
|
586
|
+
...options.metadata
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
const { data, error } = await this.client.auth.updateUser(updateData);
|
|
590
|
+
if (error) {
|
|
591
|
+
return {
|
|
592
|
+
success: false,
|
|
593
|
+
error: mapSupabaseError(error)
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
success: true,
|
|
598
|
+
user: data.user ? mapSupabaseUser(data.user) : void 0
|
|
599
|
+
};
|
|
600
|
+
} catch (error) {
|
|
601
|
+
return {
|
|
602
|
+
success: false,
|
|
603
|
+
error: createAuthError(
|
|
604
|
+
"server_error",
|
|
605
|
+
error instanceof Error ? error.message : "Server error"
|
|
606
|
+
)
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
async deleteAccount(_options) {
|
|
611
|
+
try {
|
|
612
|
+
await this.signOut();
|
|
613
|
+
return { success: true };
|
|
614
|
+
} catch (error) {
|
|
615
|
+
return {
|
|
616
|
+
success: false,
|
|
617
|
+
error: createAuthError(
|
|
618
|
+
"server_error",
|
|
619
|
+
error instanceof Error ? error.message : "Server error"
|
|
620
|
+
)
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
async reauthenticate(password) {
|
|
625
|
+
const user = await this.getCurrentUser();
|
|
626
|
+
if (!user || !user.email) {
|
|
627
|
+
return {
|
|
628
|
+
success: false,
|
|
629
|
+
error: createAuthError("session_expired", "Not authenticated")
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
return this.signInWithEmail({
|
|
633
|
+
email: user.email,
|
|
634
|
+
password
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
// ─────────────────────────────────────────────────────────────
|
|
638
|
+
// Auth State
|
|
639
|
+
// ─────────────────────────────────────────────────────────────
|
|
640
|
+
onAuthStateChange(callback) {
|
|
641
|
+
const {
|
|
642
|
+
data: { subscription }
|
|
643
|
+
} = this.client.auth.onAuthStateChange((event, session) => {
|
|
644
|
+
let mappedEvent = null;
|
|
645
|
+
switch (event) {
|
|
646
|
+
case "SIGNED_IN":
|
|
647
|
+
case "INITIAL_SESSION":
|
|
648
|
+
mappedEvent = "SIGNED_IN";
|
|
649
|
+
break;
|
|
650
|
+
case "SIGNED_OUT":
|
|
651
|
+
mappedEvent = "SIGNED_OUT";
|
|
652
|
+
break;
|
|
653
|
+
case "TOKEN_REFRESHED":
|
|
654
|
+
mappedEvent = "TOKEN_REFRESHED";
|
|
655
|
+
break;
|
|
656
|
+
case "USER_UPDATED":
|
|
657
|
+
mappedEvent = "USER_UPDATED";
|
|
658
|
+
break;
|
|
659
|
+
case "PASSWORD_RECOVERY":
|
|
660
|
+
mappedEvent = "PASSWORD_RECOVERY";
|
|
661
|
+
break;
|
|
662
|
+
case "MFA_CHALLENGE_VERIFIED":
|
|
663
|
+
mappedEvent = "MFA_CHALLENGE_VERIFIED";
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
if (mappedEvent) {
|
|
667
|
+
callback(mappedEvent, session ? mapSupabaseSession(session) : null);
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
return () => subscription.unsubscribe();
|
|
671
|
+
}
|
|
672
|
+
// ─────────────────────────────────────────────────────────────
|
|
673
|
+
// Health
|
|
674
|
+
// ─────────────────────────────────────────────────────────────
|
|
675
|
+
async healthCheck() {
|
|
676
|
+
try {
|
|
677
|
+
const { error } = await this.client.auth.getSession();
|
|
678
|
+
return !error;
|
|
679
|
+
} catch {
|
|
680
|
+
return false;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
export {
|
|
685
|
+
SupabaseAuth
|
|
686
|
+
};
|
|
687
|
+
//# sourceMappingURL=supabase-auth.mjs.map
|