mirror of
https://github.com/BradNut/weddingsite
synced 2025-09-08 17:40:36 +00:00
27 lines
805 B
JavaScript
27 lines
805 B
JavaScript
import { genSalt, hash } from 'bcryptjs';
|
|
import withSession from '../../lib/session';
|
|
import connectDb from '../../utils/db.js';
|
|
import User from '../../models/User';
|
|
|
|
export default withSession(async (req, res) => {
|
|
const { username, password } = await req.body;
|
|
await connectDb();
|
|
|
|
const salt = await genSalt(10);
|
|
const hashedPassword = await hash(password, salt);
|
|
|
|
try {
|
|
const result = await User.create({
|
|
username,
|
|
password: hashedPassword,
|
|
role: 'guest',
|
|
});
|
|
const user = { isLoggedIn: true, id: result?._id };
|
|
req.session.set('user', user);
|
|
await req.session.save();
|
|
res.status(201).json({ success: true });
|
|
} catch (error) {
|
|
const { response: fetchResponse } = error;
|
|
res.status(fetchResponse?.status || 500).json(error.data);
|
|
}
|
|
});
|