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