updated user API route

This commit is contained in:
Andreas 2023-07-26 19:24:52 +02:00
parent 284171c7e9
commit ceea334375

View File

@ -17,28 +17,26 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (req.method === 'POST') { if (req.method === 'POST') {
const username = req.body.username; const username = req.body.username;
const password = req.body.password; const password = req.body.password;
console.log(req.body); await MUser.sync()
const user = await MUser.sync() var user = await MUser.findOne({ where: { username: username } });
.then(async f => { if (user != undefined){
return await MUser.findOne({ where: { username: username } }); res.status(500).json("User with that username already exists");
}) return;
.then(async user => { }
if (user == undefined) {
const hash = await hashPassword(password) const hash = await hashPassword(password)
return await MUser.create({ user = await MUser.create({
username: username, username: username,
password: hash password: hash
}) })
res.status(200).json(user);
return;
} }
else{ if (req.method === 'GET') {
throw "User with that username already exists"; const username = req.body.username;
} const password = req.body.password;
}) await MUser.sync()
.then(user =>{ var users = await MUser.findAll();
res.status(200).json(user) res.status(200).json(users);
}) return;
.catch(error => {
res.status(500).json(error);
});
} }
} }