This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
qoverflow/server/routes/user/Register.js
2022-08-01 22:28:25 -05:00

37 lines
1.1 KiB
JavaScript

const createRequest = require('server/utils/api');
const deriveKeyFromPassword = require('server/utils/auth');
const config = require('server/config.json');
const User = require('server/db/models/User');
async function Register(req, res) {
const { username, email, password } = req.body;
if (!username || !email || !password) {
return res.status(400).send(config.errorIncomplete);
}
const { salt, key } = await deriveKeyFromPassword(password);
const { success, error, user } = await createRequest('post', '/users', {
username,
email,
salt,
key,
});
if (!success)
switch (error) {
case 'an item with that "username" already exists':
return res.status(409).send({ error });
case 'an item with that "email" already exists':
return res.status(409).send({ error });
default:
return res.status(500).send(config.errorGeneric);
}
await User.create(user);
return res.sendStatus(201);
}
module.exports = Register;