File size: 796 Bytes
f8ca2a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { sign } from 'jsonwebtoken';
import config from '@/lib/config';

const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key'; // Make sure to set this in your environment variables

export async function POST(req) {
  const { password } = await req.json();

  if (password === config.ADMIN_PASSWORD) {
    // Generate a JWT token
    const token = sign({ role: 'admin' }, JWT_SECRET, { expiresIn: '1h' });
    const headers = new Headers();
    headers.append('Set-Cookie', `adminToken=${token}; HttpOnly; Path=/; Secure; SameSite=Strict`);
    return new Response(JSON.stringify({ message: 'Authenticated' }), {
      status: 200,
      headers: headers,
    });
  } else {
    return new Response(JSON.stringify({ message: 'Incorrect password' }), {
      status: 401,
    });
  }
}