Skip to content

Instantly share code, notes, and snippets.

@mars3142
Created June 9, 2021 19:55
Show Gist options
  • Save mars3142/53dac924f07bddf45e565fd8fa993a72 to your computer and use it in GitHub Desktop.
Save mars3142/53dac924f07bddf45e565fd8fa993a72 to your computer and use it in GitHub Desktop.
signup
router.post("/signup", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
if (username !== undefined && username.trim().length > 0) {
const user = await database().ref(`accounts/${username}`).get();
if (user.exists()) {
res.status(400).json({
error: {
message: "Username already exists",
},
});
return;
}
} else {
res.status(412).json({
error: {
message: "Username can't be empty",
},
});
return;
}
if (password !== undefined && password.trim().length > 0) {
const salt = crypto.randomBytes(16).toString("base64");
const pw = await scrypt.hash(password, salt);
const user = {
created: database.ServerValue.TIMESTAMP,
password: pw,
salt: salt,
};
await database().ref(`accounts/${username}`).set(user);
await sendToken(res, username);
} else {
res.status(412).json({
error: {
message: "Password can't be empty",
},
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment