sciencefyll

Untitled

Apr 22nd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //includes express, mongoose, User, constants. this part is ok.
  2.  
  3. /**
  4.  * Authenticates a user post request
  5.  *
  6.  * @request email string
  7.  * @request password string
  8.  *
  9.  */
  10. router.post("/", (req, res) => {
  11.     /**
  12.      * Retrieve the user
  13.      */
  14.     User.find(
  15.     {
  16.         email: req.body.email
  17.     },
  18.     (err, user) =>
  19.     {
  20.         /**
  21.          * An error occurred
  22.          */
  23.         if (err)
  24.         {
  25.             return res.json({
  26.                 success:    false,
  27.                 message:    "An mongodb error occurred.",
  28.                 error:      err
  29.             });
  30.         }
  31.  
  32.         /**
  33.          * Check for problems with the email or password.
  34.          */
  35.         if (!user)
  36.         {
  37.             return res.json({
  38.                 success:    false,
  39.                 message:    "Email or password was incorrect."
  40.             });
  41.         }
  42.  
  43.         // ##########################################################
  44.         // ERROR
  45.         // user.verifyPassword(req.body.password, match => {
  46.         //     ^
  47.         // TypeError: user.verifyPassword is not a function
  48.         // ##########################################################
  49.         user.verifyPassword(req.body.password, match => {
  50.             if (!match)
  51.             {
  52.                 return res.json({
  53.                     success:    false,
  54.                     message:    "Email or password was incorrect."
  55.                 });
  56.             }
  57.  
  58.             /**
  59.              * User authenticated!
  60.              */
  61.             req.session.user = user;
  62.             res.json({
  63.                 success: true,
  64.                 message: "Successfully authenticated."
  65.             });
  66.         });
  67.        
  68.     });
  69. });
  70.  
  71. router.get("/", (req, res, next) => {
  72.     var admin = new User({
  73.         name: "admin",
  74.         email: "admin@admin.net",
  75.         password: "admin",
  76.         authorization: constants.authorization.admin
  77.     });
  78.  
  79.     // ########################################################
  80.     // ERROR
  81.     // if (!user.isModified("password"))
  82.     //            ^
  83.     // TypeError: user.isModified is not a function
  84.     // #######################################################
  85.     admin.save(function(err) {
  86.         if (err)
  87.         {
  88.             console.log(err);
  89.         }
  90.  
  91.         console.log('User saved successfully');
  92.         res.json({ success: true });
  93.     });
  94. });
Add Comment
Please, Sign In to add comment