Created
June 9, 2021 19:55
-
-
Save mars3142/53dac924f07bddf45e565fd8fa993a72 to your computer and use it in GitHub Desktop.
signup
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("/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