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
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 59s
Details
This commit is contained in:
parent
59ce33736b
commit
4ce618c318
27
server.ts
27
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' });
|
||||
|
|
|
|||
|
|
@ -589,15 +589,15 @@ export default function App() {
|
|||
/* LOGIN FORM */
|
||||
<form onSubmit={handleLogin} className="space-y-4">
|
||||
<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">
|
||||
<Mail className="absolute left-3 top-3 w-4 h-4 text-zinc-500" />
|
||||
<input
|
||||
type="email"
|
||||
type="text"
|
||||
required
|
||||
value={loginEmail}
|
||||
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"
|
||||
/>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue