refactored
This commit is contained in:
parent
73685614ab
commit
79406667f6
@ -32,55 +32,27 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
const username = auth[0];
|
const username = auth[0];
|
||||||
const password = auth[1];
|
const password = auth[1];
|
||||||
// console.log(req.body);
|
// console.log(req.body);
|
||||||
MUser.sync()
|
await MUser.sync();
|
||||||
.then(async user => {
|
await MAuth.sync();
|
||||||
// console.log(user);
|
|
||||||
return await MAuth.sync();
|
let user = await MUser.findOne({ where: { username: username } });
|
||||||
})
|
if (user == undefined) {
|
||||||
.then(async auth => {
|
res.status(401).json("User does not exist");
|
||||||
// console.log(auth);
|
return;
|
||||||
return await MUser.findOne({ where: { username: username } });
|
}
|
||||||
})
|
|
||||||
.then(async user => {
|
if (!(await validatePassword(password, user.password))) {
|
||||||
// console.log(user);
|
res.status(401).json("Invalid password");
|
||||||
if (user == undefined) {
|
return;
|
||||||
throw "no such user exists";
|
}
|
||||||
}
|
|
||||||
else {
|
let authtoken = await MAuth.findOne({ where: { user_id: user.id } });
|
||||||
return user;
|
if (authtoken == undefined) {
|
||||||
}
|
if (user.id != undefined) {
|
||||||
})
|
authtoken = await MAuth.create({ user_id: user.id });
|
||||||
.then(async user => {
|
}
|
||||||
const passIsValid = await validatePassword(password, user.password);
|
}
|
||||||
return { passIsValid, user };
|
|
||||||
})
|
res.status(200).json(authtoken);
|
||||||
.then(async ({ passIsValid, user }) => {
|
|
||||||
if (passIsValid) {
|
|
||||||
const authtoken = await MAuth.findOne({ where: { user_id: user.id } });
|
|
||||||
return { authtoken, user }
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw ("invalid password");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(async ({ authtoken, user }) => {
|
|
||||||
if (authtoken == undefined) {
|
|
||||||
if (user.id != undefined) {
|
|
||||||
// console.log("creating new auth token")
|
|
||||||
return await MAuth.create({ user_id: user.id });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return authtoken
|
|
||||||
}
|
|
||||||
}).then(authtoken => {
|
|
||||||
if (authtoken != null) {
|
|
||||||
// console.log(authtoken);
|
|
||||||
res.status(200).json(authtoken);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
res.status(500).json(error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user