Skip to content

Instantly share code, notes, and snippets.

@mars3142
Created June 9, 2021 20:07
Show Gist options
  • Save mars3142/d77df1db6cbbbb9ef2107f4041b90f65 to your computer and use it in GitHub Desktop.
Save mars3142/d77df1db6cbbbb9ef2107f4041b90f65 to your computer and use it in GitHub Desktop.
signin
router.post("/signin", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
if (await isUserValid(username, password)) {
await sendToken(res, username);
} else {
res.status(401).json({
error: {
message: "Username or password invalid",
},
});
}
});
async function isUserValid(username?: string, password?: string): Promise<boolean> {
if (
username !== undefined &&
username.trim().length > 0 &&
password !== undefined &&
password.trim().length > 0
) {
const user = await database().ref(`accounts/${username}`).get();
if (user.exists()) {
const salt = user.exportVal().salt;
const hash = user.exportVal().password;
return scrypt.verify(password, salt, hash);
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment