feat: sessão #3 — lib (db/auth/email/validations), API routes, NextAuth v5, middleware, páginas account/shelters/shelter-dashboard, Prisma v7 fix
This commit is contained in:
3
app/api/auth/[...nextauth]/route.ts
Normal file
3
app/api/auth/[...nextauth]/route.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { handlers } from '@/lib/auth/config';
|
||||
|
||||
export const { GET, POST } = handlers;
|
||||
56
app/api/auth/register/route.ts
Normal file
56
app/api/auth/register/route.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/db/prisma';
|
||||
import { registerSchema } from '@/lib/validations/auth';
|
||||
import { hashPassword } from '@/lib/auth/password';
|
||||
import { validateAge } from '@/lib/auth/age-validation';
|
||||
import { sendEmail, buildWelcomeHtml } from '@/lib/email';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const parsed = registerSchema.safeParse(body);
|
||||
|
||||
if (!parsed.success) {
|
||||
const errors = parsed.error.flatten().fieldErrors;
|
||||
const firstError = Object.values(errors).flat()[0];
|
||||
return NextResponse.json({ error: firstError ?? 'Dados inválidos.' }, { status: 400 });
|
||||
}
|
||||
|
||||
const { name, email, password, birthdate, district } = parsed.data;
|
||||
|
||||
// Validação de +18 sempre no servidor
|
||||
const ageCheck = validateAge(birthdate);
|
||||
if (!ageCheck.valid) {
|
||||
return NextResponse.json({ error: ageCheck.error }, { status: 400 });
|
||||
}
|
||||
|
||||
// Verificar se email já existe
|
||||
const existing = await prisma.user.findUnique({ where: { email } });
|
||||
if (existing) {
|
||||
return NextResponse.json({ error: 'Este email já está registado.' }, { status: 409 });
|
||||
}
|
||||
|
||||
const hashedPassword = await hashPassword(password);
|
||||
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
name,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
birthdate: new Date(birthdate),
|
||||
district,
|
||||
},
|
||||
select: { id: true, name: true, email: true },
|
||||
});
|
||||
|
||||
// Email de boas-vindas (não bloqueia)
|
||||
sendEmail({
|
||||
to: user.email,
|
||||
subject: 'Bem-vindo à PawLink 🐾',
|
||||
html: buildWelcomeHtml({ userName: user.name }),
|
||||
}).catch(console.error);
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'Conta criada com sucesso.', userId: user.id },
|
||||
{ status: 201 }
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user