chore: Secure routes with auth middleware, update text and fix minor bugs
This commit is contained in:
43
src/app/api/auth/login/route.ts
Normal file
43
src/app/api/auth/login/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { email, password } = body;
|
||||
|
||||
let role = null;
|
||||
|
||||
if (email === "demo@pragativeer.com" && password === "Pv9#kL2$mQ8!vX4@") {
|
||||
role = "user";
|
||||
} else if (email === "admin@pragativeer.com" && password === "admin123") {
|
||||
role = "admin";
|
||||
}
|
||||
|
||||
if (role) {
|
||||
const response = NextResponse.json({ success: true, role });
|
||||
|
||||
// Set HttpOnly cookie
|
||||
response.cookies.set({
|
||||
name: 'auth_token',
|
||||
value: role,
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 60 * 60 * 24 * 7 // 1 week
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Invalid email or password' },
|
||||
{ status: 401 }
|
||||
);
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Bad request' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user