Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Signs in the user */
- router.post("/sign_in", function (req, res , next) {
- var username = req.body.username;
- var password = req.body.password;
- password = sha256(password);
- var token = "";
- //Checks if username and password match
- query.checkUsernamePassword(username, password).then(function (row) {
- if(row === undefined){
- throw "Username and password doesnt match";
- }
- return row.id;
- }).then(function (userId) {
- //Check if user is already logged in.
- return query.isUserLoggedIn(userId);
- }).then(function (rows) {
- //Signs out if user is logged in.
- if(rows[0] !== undefined){
- return query.signOut(rows[0].token);
- }
- }).then(function() {
- //Signs the user in.
- var letters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
- for (var i = 0 ; i < 36 ; ++i) {
- token += letters[Math.floor(Math.random() * letters.length)];
- }
- return query.signIn(username, token);
- }).then(function (){
- res.send(getResponseObject(true, "Success signing in", token))
- }).catch(function (error) {
- console.log(error);
- res.send(getResponseObject(false, "error signing in"));
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement