32 lines
906 B
JavaScript
32 lines
906 B
JavaScript
import { hashSync, compareSync } from 'bcryptjs';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
// In-memory store for demo purposes
|
|
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' });
|
|
}
|
|
|
|
// Check if user exists
|
|
const existing = users.find(u => u.username === username);
|
|
if (existing) {
|
|
return res.status(400).json({ message: 'User already exists' });
|
|
}
|
|
|
|
const id = uuidv4();
|
|
const hashedPassword = hashSync(password, 10);
|
|
users.push({ id, username, password: hashedPassword });
|
|
|
|
// Return a fake JWT token (just a string)
|
|
const token = `${id}:${Date.now()}`;
|
|
|
|
res.status(201).json({ token, user: { id, username } });
|
|
} |