From 4ce618c318d835a3a8a015c9fc4a65248149174e Mon Sep 17 00:00:00 2001 From: Sidney Gomes Date: Thu, 23 Jul 2026 23:08:25 -0300 Subject: [PATCH] Allow student login using either email or phone number in frontend and backend --- server.ts | 29 +++++++++++++++++++++++------ src/App.tsx | 6 +++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/server.ts b/server.ts index 160f9c4..196c6fb 100644 --- a/server.ts +++ b/server.ts @@ -200,17 +200,34 @@ app.post('/api/auth/register', async (req: Request, res: Response) => { // 2. Auth: Login app.post('/api/auth/login', async (req: Request, res: Response) => { - const { email, password } = req.body; - + const { email, password } = req.body; // email field represents identifier (email or whatsapp) + if (!email || !password) { - res.status(400).json({ error: 'Informe e-mail e senha' }); + res.status(400).json({ error: 'E-mail ou WhatsApp e senha são obrigatórios' }); return; } try { - const user = await prisma.user.findUnique({ - where: { email: email.toLowerCase() } - }); + let user; + if (email.includes('@')) { + user = await prisma.user.findFirst({ + where: { email: email.toLowerCase() } + }); + } else { + const cleanPhone = email.replace(/\D/g, ''); + const withoutDDI = cleanPhone.replace(/^55/, ''); + const withDDI = '55' + withoutDDI; + + user = await prisma.user.findFirst({ + where: { + OR: [ + { whatsapp: cleanPhone }, + { whatsapp: withoutDDI }, + { whatsapp: withDDI } + ] + } + }); + } if (!user || !user.password) { res.status(401).json({ error: 'Credenciais inválidas' }); diff --git a/src/App.tsx b/src/App.tsx index 06fd0ec..7f9fe16 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -589,15 +589,15 @@ export default function App() { /* LOGIN FORM */
- +
setLoginEmail(e.target.value)} - placeholder="email@exemplo.com" + placeholder="email@exemplo.com ou 5585981145217" className="w-full glass-input rounded-lg p-3 pl-10 text-xs text-white focus:outline-none" />