- Replace Clerk with self-hosted JWT auth (register/login/verify, bcrypt passwords, Gmail OTP with console fallback) - Add lib/db.ts pg pool + transaction helpers; seed script migrates legacy Clerk-era schema (drop clerk_id, enforce UUID ids and unique email) - Add owner-gated admin API: stats, users, drivers CRUD, rides - Add dashboard/ Vite React owner dashboard (login, overview, users, fleet, rides) with dev-server proxy to avoid Expo CORS middleware - Add scripts/set-owner.mjs for role management
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { createHash } from "crypto";
|
|
|
|
import { sql } from "@/lib/db";
|
|
import { verifyPassword } from "@/lib/password";
|
|
import { issueSession, toProfile } from "@/lib/users";
|
|
|
|
export async function POST(req: Request) {
|
|
const { email, password } = await req.json();
|
|
|
|
if (!email?.trim() || !password) {
|
|
return Response.json(
|
|
{ error: "Email and password are required." },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
const rows = await sql<{
|
|
id: string;
|
|
name: string;
|
|
email: string;
|
|
role: string | null;
|
|
password_hash: string | null;
|
|
email_verified: boolean;
|
|
}>`
|
|
SELECT id, name, email, role, password_hash, email_verified
|
|
FROM users
|
|
WHERE email = ${email.trim().toLowerCase()}
|
|
`;
|
|
|
|
const user = rows[0];
|
|
|
|
if (!user || !user.password_hash || !verifyPassword(password, user.password_hash)) {
|
|
return Response.json(
|
|
{ error: "Invalid email or password." },
|
|
{ status: 401 },
|
|
);
|
|
}
|
|
|
|
if (!user.email_verified) {
|
|
return Response.json(
|
|
{ error: "Please verify your email first." },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
|
|
const session = issueSession(user);
|
|
|
|
return Response.json({
|
|
data: { token: session.token, user: toProfile(user) },
|
|
});
|
|
} catch (error) {
|
|
console.error("[LOGIN]: ", error);
|
|
return Response.json({ error: "Internal Server Error" }, { status: 500 });
|
|
}
|
|
}
|