Add src/app/api/auth/login/route.ts
This commit is contained in:
61
src/app/api/auth/login/route.ts
Normal file
61
src/app/api/auth/login/route.ts
Normal 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user