Allow student login using either email or phone number in frontend and backend
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 59s Details

This commit is contained in:
Sidney Gomes 2026-07-23 23:08:25 -03:00
parent 59ce33736b
commit 4ce618c318
2 changed files with 26 additions and 9 deletions

View File

@ -200,17 +200,34 @@ app.post('/api/auth/register', async (req: Request, res: Response) => {
// 2. Auth: Login // 2. Auth: Login
app.post('/api/auth/login', async (req: Request, res: Response) => { 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) { 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; return;
} }
try { try {
const user = await prisma.user.findUnique({ let user;
where: { email: email.toLowerCase() } 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) { if (!user || !user.password) {
res.status(401).json({ error: 'Credenciais inválidas' }); res.status(401).json({ error: 'Credenciais inválidas' });

View File

@ -589,15 +589,15 @@ export default function App() {
/* LOGIN FORM */ /* LOGIN FORM */
<form onSubmit={handleLogin} className="space-y-4"> <form onSubmit={handleLogin} className="space-y-4">
<div className="space-y-1"> <div className="space-y-1">
<label className="text-[10px] text-zinc-400 font-bold uppercase tracking-wider">E-mail</label> <label className="text-[10px] text-zinc-400 font-bold uppercase tracking-wider">E-mail ou WhatsApp</label>
<div className="relative"> <div className="relative">
<Mail className="absolute left-3 top-3 w-4 h-4 text-zinc-500" /> <Mail className="absolute left-3 top-3 w-4 h-4 text-zinc-500" />
<input <input
type="email" type="text"
required required
value={loginEmail} value={loginEmail}
onChange={(e) => setLoginEmail(e.target.value)} onChange={(e) => 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" className="w-full glass-input rounded-lg p-3 pl-10 text-xs text-white focus:outline-none"
/> />
</div> </div>