31 lines
842 B
JavaScript
31 lines
842 B
JavaScript
import { compareSync } from 'bcryptjs';
|
|
|
|
// In-memory store (same as signup)
|
|
let users = [];
|
|
|
|
export default function handler(req, res) {
|
|
if (req.method !== 'POST') {
|
|
return res.status(405).json({ message: 'Method not allowed' });
|
|
}
|
|
|
|
const { username, password } = req.body;
|
|
|
|
if (!username || !password) {
|
|
return res.status(400).json({ message: 'Username and password required' });
|
|
}
|
|
|
|
const user = users.find(u => u.username === username);
|
|
if (!user) {
|
|
return res.status(401).json({ message: 'Invalid credentials' });
|
|
}
|
|
|
|
const valid = compareSync(password, user.password);
|
|
if (!valid) {
|
|
return res.status(401).json({ message: 'Invalid credentials' });
|
|
}
|
|
|
|
// Fake JWT token
|
|
const token = `${user.id}:${Date.now()}`;
|
|
|
|
res.status(200).json({ token, user: { id: user.id, username: user.username } });
|
|
} |