Add src/app/api/auth/login/route.ts

This commit is contained in:
2026-03-11 20:10:37 +00:00
parent 5dae6a5a55
commit 0e34fba85e

View File

@@ -0,0 +1,61 @@
import { NextRequest, NextResponse } from "next/server";
import crypto from "crypto";
// Mock user database - replace with actual database
const mockUsers = [
{
id: "user_1", email: "teste@fitflow.com", passwordHash: crypto.createHash("sha256").update("senha123").digest("hex"),
name: "Usuário Teste"},
];
export async function POST(request: NextRequest) {
try {
const { email, password } = await request.json();
// Validate inputs
if (!email || !password) {
return NextResponse.json(
{ message: "Email e senha são obrigatórios" },
{ status: 400 }
);
}
// Hash password
const passwordHash = crypto
.createHash("sha256")
.update(password)
.digest("hex");
// Find user
const user = mockUsers.find(
(u) => u.email === email && u.passwordHash === passwordHash
);
if (!user) {
return NextResponse.json(
{ message: "Email ou senha incorretos" },
{ status: 401 }
);
}
// Generate token (in production, use JWT)
const token = crypto.randomBytes(32).toString("hex");
return NextResponse.json(
{
token,
user: {
id: user.id,
email: user.email,
name: user.name,
},
},
{ status: 200 }
);
} catch (error) {
return NextResponse.json(
{ message: "Erro interno do servidor" },
{ status: 500 }
);
}
}