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') {
const username = req.body.username;
const password = req.body.password;
console.log(req.body);
const user = await MUser.sync()
.then(async f => {
return await MUser.findOne({ where: { username: username } });
})
.then(async user => {
if (user == undefined) {
const hash = await hashPassword(password)
return await MUser.create({
username: username,
password: hash
})
}
else{
throw "User with that username already exists";
}
})
.then(user =>{
res.status(200).json(user)
})
.catch(error => {
res.status(500).json(error);
});
await MUser.sync()
var user = await MUser.findOne({ where: { username: username } });
if (user != undefined){
res.status(500).json("User with that username already exists");
return;
}
const hash = await hashPassword(password)
user = await MUser.create({
username: username,
password: hash
})
res.status(200).json(user);
return;
}
if (req.method === 'GET') {
const username = req.body.username;
const password = req.body.password;
await MUser.sync()
var users = await MUser.findAll();
res.status(200).json(users);
return;
}
}