-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
436 lines (356 loc) · 12.5 KB
/
Copy pathserver.js
File metadata and controls
436 lines (356 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* server.js
* Simple Express server to interact with Supabase (anon + optional service role key).
*
* Auth: signUp, signUpVerify, resendOtp, signIn, gglSignIn, forgtPss, resetPssVerify, getUsr, usrExst
*/
try { require('dotenv').config(); } catch (e) {}
const express = require('express');
const cors = require('cors');
const { createClient } = require('@supabase/supabase-js');
const SUPABASE_URL = process.env.SUPABASE_URL || '';
const SUPABASE_ANON_KEY = process.env.SUPABASE_ANON_KEY || '';
const SUPABASE_SERVICE_ROLE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY || '';
const PORT = process.env.PORT || 3024;
const BASE_URL = process.env.BASE_URL || '';
if (!SUPABASE_URL || !SUPABASE_ANON_KEY) {
console.warn('[server] SUPABASE_URL or SUPABASE_ANON_KEY missing');
}
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
const supabaseAdmin = SUPABASE_SERVICE_ROLE_KEY
? createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY)
: null;
/** Get Supabase client - uses custom headers if provided, otherwise uses env defaults */
function getSupabaseClient(req) {
const customUrl = req.headers['x-supabase-url'];
const customKey = req.headers['x-supabase-anon-key'];
if (customUrl && customKey) {
return createClient(customUrl, customKey);
}
return supabase;
}
/** Get Supabase URL - uses custom header if provided, otherwise uses env default */
function getSupabaseUrl(req) {
const url = req.headers['x-supabase-url'] || SUPABASE_URL;
return url.replace(/\/+$/, ''); // Remove trailing slashes
}
/** Get Supabase Anon Key - uses custom header if provided, otherwise uses env default */
function getSupabaseAnonKey(req) {
return req.headers['x-supabase-anon-key'] || SUPABASE_ANON_KEY;
}
/** Get Supabase Service Role Key - uses custom header if provided, otherwise uses env default */
function getSupabaseServiceKey(req) {
return req.headers['x-supabase-service-key'] || SUPABASE_SERVICE_ROLE_KEY;
}
/** Get Supabase Admin client - uses custom headers if provided */
function getSupabaseAdminClient(req) {
const customUrl = req.headers['x-supabase-url'];
const customServiceKey = req.headers['x-supabase-service-key'];
if (customUrl && customServiceKey) {
return createClient(customUrl, customServiceKey);
}
return supabaseAdmin;
}
/** Check if user exists (signup-trick: empty user_metadata/identities = exists). Returns boolean. */
async function checkUserExists(email, client) {
const randomPassword = Math.random().toString(36).slice(-12) + 'A1!';
const { data, error } = await client.auth.signUp({ email, password: randomPassword });
if (error) {
if (error.message.includes('already registered') || error.message.includes('already exists'))
return true;
return false;
}
const user = data?.user;
const hasMetadata = user?.user_metadata && Object.keys(user.user_metadata).length > 0;
const hasIdentities = user?.identities && Array.isArray(user.identities) && user.identities.length > 0;
return !hasMetadata && !hasIdentities;
}
const app = express();
app.use(cors());
app.use(express.json());
/* ---------------- HEALTH ---------------- */
app.get('/health', (req, res) => {
res.json({ status: 'ok', time: new Date().toISOString() });
});
/* ---------------- CONFIG ---------------- */
app.get('/config', (req, res) => {
// Returns BASE_URL from env if set (for production deployments)
const normalizedBaseUrl = BASE_URL ? BASE_URL.replace(/\/+$/, '') : null;
res.json({
baseUrl: normalizedBaseUrl,
hasBaseUrl: !!BASE_URL
});
});
/* ---------------- SUPABASE STATUS ---------------- */
app.get('/supabase-status', async (req, res) => {
const client = getSupabaseClient(req);
const supabaseUrl = getSupabaseUrl(req);
const hasAnonKey = !!(req.headers['x-supabase-anon-key'] || SUPABASE_ANON_KEY);
const hasServiceKey = !!(req.headers['x-supabase-service-key'] || SUPABASE_SERVICE_ROLE_KEY);
// Check if credentials are configured
if (!supabaseUrl || !hasAnonKey) {
return res.status(400).json({
status: 'error',
connected: false,
message: 'Supabase credentials not configured',
details: {
hasUrl: !!supabaseUrl,
hasAnonKey,
hasServiceKey
}
});
}
try {
// Try to get auth settings - this is a lightweight call that tests connectivity
const { data, error } = await client.auth.getSession();
if (error && !error.message.includes('no current session')) {
return res.status(500).json({
status: 'error',
connected: false,
message: 'Supabase connection failed',
error: error.message,
details: {
url: supabaseUrl,
hasAnonKey,
hasServiceKey
}
});
}
return res.json({
status: 'ok',
connected: true,
message: 'Supabase connection successful',
details: {
url: supabaseUrl,
hasAnonKey,
hasServiceKey
}
});
} catch (err) {
return res.status(500).json({
status: 'error',
connected: false,
message: 'Supabase connection failed',
error: err.message,
details: {
url: supabaseUrl,
hasAnonKey,
hasServiceKey
}
});
}
});
/* ---------------- AUTH ---------------- */
app.post('/signUp', async (req, res) => {
const { email, password, data } = req.body || {};
if (!email || !password)
return res.status(400).json({ error: 'email and password required' });
try {
const client = getSupabaseClient(req);
const { data: userData, error } = await client.auth.signUp({
email,
password,
options: { data },
});
if (error) return res.status(400).json({ error: error.message });
return res.json({
status: 'ok',
message: 'Signup successful. Please verify OTP sent to email.',
data: userData,
});
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------- SIGN UP OTP VERIFICATION ---------- */
app.post('/signUpVerify', async (req, res) => {
const { email, token, type } = req.body || {};
if (!email || !token)
return res.status(400).json({ error: 'email and token (OTP) required' });
try {
const client = getSupabaseClient(req);
const { data, error } = await client.auth.verifyOtp({
email,
token,
type: type || 'signup', // default signup
});
if (error) return res.status(400).json({ error: error.message });
return res.json({
status: 'ok',
message: 'Email verified successfully',
session: data.session,
user: data.user,
});
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------- RESEND EMAIL OTP ---------- */
app.post('/resendOtp', async (req, res) => {
const { email, type } = req.body || {};
if (!email)
return res.status(400).json({ error: 'email required' });
try {
const client = getSupabaseClient(req);
// This re-sends OTP email
const { data, error } = await client.auth.signInWithOtp({
email,
options: {
shouldCreateUser: false, // IMPORTANT: prevents new user creation
},
});
if (error) return res.status(400).json({ error: error.message });
return res.json({
status: 'ok',
message: 'OTP resent successfully',
data,
});
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------------- SIGN IN ---------------- */
app.post('/signIn', async (req, res) => {
const { email, password } = req.body || {};
if (!email || !password)
return res.status(400).json({ error: 'email and password required' });
try {
const client = getSupabaseClient(req);
const { data, error } = await client.auth.signInWithPassword({
email,
password,
});
if (error) return res.status(400).json({ error: error.message });
return res.json(data);
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------------- GOOGLE SIGN IN ---------------- */
app.get('/gglSignIn', async (req, res) => {
const redirectTo = req.query.redirectTo || '';
try {
const client = getSupabaseClient(req);
const { data, error } = await client.auth.signInWithOAuth({
provider: 'google',
options: { redirectTo },
});
if (error) return res.status(400).json({ error: error.message });
return res.json(data);
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------------- FORGOT PASSWORD ---------------- */
app.post('/forgtPss', async (req, res) => {
const { email, redirectTo } = req.body || {};
if (!email) return res.status(400).json({ error: 'email required' });
try {
const client = getSupabaseClient(req);
const exists = await checkUserExists(email, client);
if (!exists)
return res.status(400).json({ error: 'User does not exist' });
const { data, error } = await client.auth.resetPasswordForEmail(email, {
redirectTo,
});
if (error) return res.status(400).json({ error: error.message });
return res.json({ status: 'ok', data });
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------- RESET PASSWORD OTP VERIFY (token + new password) ---------- */
app.post('/resetPssVerify', async (req, res) => {
const { email, token, newPassword } = req.body || {};
if (!email || !token || !newPassword)
return res.status(400).json({ error: 'email, token (OTP from reset email), and newPassword required' });
try {
const client = getSupabaseClient(req);
const { data, error } = await client.auth.verifyOtp({
email,
token,
type: 'recovery',
});
if (error) return res.status(400).json({ error: error.message });
if (!data?.session) return res.status(400).json({ error: 'No session from OTP verification' });
const sessionClient = createClient(getSupabaseUrl(req), getSupabaseAnonKey(req));
await sessionClient.auth.setSession({
access_token: data.session.access_token,
refresh_token: data.session.refresh_token,
});
const { error: updateError } = await sessionClient.auth.updateUser({ password: newPassword });
if (updateError) return res.status(400).json({ error: updateError.message });
return res.json({
status: 'ok',
message: 'Password reset successfully',
session: data.session,
user: data.user,
});
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------------- GET USER ---------------- */
app.get('/getUsr', async (req, res) => {
const auth = req.headers.authorization || '';
if (auth.startsWith('Bearer ')) {
const token = auth.split(' ')[1];
try {
const client = getSupabaseClient(req);
const { data, error } = await client.auth.getUser(token);
if (error) return res.status(400).json({ error: error.message });
return res.json(data);
} catch (err) {
return res.status(500).json({ error: err.message });
}
}
const email = req.query.email;
if (!email)
return res
.status(400)
.json({ error: 'Bearer token or ?email required' });
const serviceKey = getSupabaseServiceKey(req);
if (!serviceKey)
return res.status(403).json({ error: 'Service role key missing (provide via header or server env)' });
try {
const baseUrl = getSupabaseUrl(req);
const url = `${baseUrl}/auth/v1/admin/users?email=${encodeURIComponent(email)}`;
const r = await fetch(url, {
headers: {
Authorization: `Bearer ${serviceKey}`,
apikey: serviceKey,
},
});
const json = await r.json();
return res.json(json);
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------------- USER EXISTS ---------------- */
app.post('/usrExst', async (req, res) => {
const { email } = req.body || {};
if (!email) return res.status(400).json({ error: 'email required' });
try {
const client = getSupabaseClient(req);
const exists = await checkUserExists(email, client);
return res.json({ exists, data: null });
} catch (err) {
return res.status(500).json({ error: err.message });
}
});
/* ---------------- STATIC (frontend) ---------------- */
// Serve index.html on root route
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.use(express.static('public'));
/* ---------------- START ---------------- */
// Export for Vercel serverless functions
module.exports = app;
// Start server locally (not in Vercel)
if (require.main === module) {
app.listen(PORT, () => {
console.log(`[server] running at http://localhost:${PORT}`);
});
}