diff --git a/client/src/config.js b/client/src/config.js index 6157f31..64b1c83 100644 --- a/client/src/config.js +++ b/client/src/config.js @@ -1,4 +1,6 @@ -export const API_BASE = 'https://t0080w08wcockgs44ws8w880.b.selfhosted.hackclub.com/api/v1'; +export const API_BASE = window.location.hostname === 'localhost' || window.location.hostname.includes('github.dev') + ? `${window.location.protocol}//${window.location.host}/api/v1` + : 'https://t0080w08wcockgs44ws8w880.b.selfhosted.hackclub.com/api/v1'; export const ERROR_MESSAGES = { NETWORK_ERROR: 'Network error. Please try again.', diff --git a/client/src/lib/Auth.svelte b/client/src/lib/Auth.svelte index 00e1217..d2e70da 100644 --- a/client/src/lib/Auth.svelte +++ b/client/src/lib/Auth.svelte @@ -26,7 +26,7 @@ headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({ email }), + body: JSON.stringify({ email, mode }), }); const data = await response.json(); diff --git a/src/api/users/auth.route.js b/src/api/users/auth.route.js index ea43cd0..ed5954b 100644 --- a/src/api/users/auth.route.js +++ b/src/api/users/auth.route.js @@ -16,18 +16,18 @@ router.get('/send', (req, res) => { }); }); -// POST /api/v1/users/send +// POST /api/v1/users/send router.post('/send', /* strictLimiter, */ async (req, res) => { try { - const { email } = req.body; - + const { email, mode } = req.body; + if (!email) { return res.status(400).json({ success: false, message: 'Email is required' }); } - + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) { return res.status(400).json({ @@ -35,9 +35,22 @@ router.post('/send', /* strictLimiter, */ async (req, res) => { message: 'Invalid email format' }); } - + + if (mode === 'login') { + const user = await pg('users') + .where('email', email) + .first(); + + if (!user) { + return res.status(404).json({ + success: false, + message: 'No account found with this email. Please sign up first.' + }); + } + } + const result = await sendEmail(email); - + res.status(200).json({ success: true, message: 'Verification code sent successfully', @@ -45,7 +58,7 @@ router.post('/send', /* strictLimiter, */ async (req, res) => { email: result.email, } }); - + } catch (error) { console.error('Error in /send route:', error); res.status(500).json({