Created
June 9, 2021 20:07
-
-
Save mars3142/d77df1db6cbbbb9ef2107f4041b90f65 to your computer and use it in GitHub Desktop.
signin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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