From ceea334375b7221b4f3f2f85c6bea1e5b3929da8 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 26 Jul 2023 19:24:52 +0200 Subject: [PATCH] updated user API route --- src/pages/api/user/index.ts | 44 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/pages/api/user/index.ts b/src/pages/api/user/index.ts index b37be8a..41b4883 100644 --- a/src/pages/api/user/index.ts +++ b/src/pages/api/user/index.ts @@ -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; } } \ No newline at end of file