Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Signs in the user */
  2. router.post("/sign_in", function (req, res , next) {
  3.   var username = req.body.username;
  4.   var password = req.body.password;
  5.   password = sha256(password);
  6.   var token = "";
  7.   //Checks if username and password match
  8.   query.checkUsernamePassword(username, password).then(function (row) {
  9.       if(row === undefined){
  10.           throw "Username and password doesnt match";
  11.       }
  12.       return row.id;
  13.   }).then(function (userId) {
  14.       //Check if user is already logged in.
  15.       return query.isUserLoggedIn(userId);
  16.   }).then(function (rows) {
  17.       //Signs out if user is logged in.
  18.       if(rows[0] !== undefined){
  19.           return query.signOut(rows[0].token);
  20.       }
  21.   }).then(function() {
  22.       //Signs the user in.
  23.       var letters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  24.       for (var i = 0 ; i < 36 ; ++i) {
  25.         token += letters[Math.floor(Math.random() * letters.length)];
  26.       }
  27.       return query.signIn(username, token);
  28.   }).then(function (){
  29.       res.send(getResponseObject(true, "Success signing in", token))
  30.   }).catch(function (error) {
  31.       console.log(error);
  32.       res.send(getResponseObject(false, "error signing in"));
  33.   });
  34. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement