Guest User

Untitled

a guest
Apr 22nd, 2016
95
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.             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.          *  Or an error might have happened.
  35.          */
  36.         if (!user ||  (user && !user.verifyPassword(req.body.password, (err, match) => match)))
  37.                                 // the code breaks here, telling me that user.verify... is not a function
  38.         {
  39.             res.json({
  40.                 success:     false,
  41.                 message:    "Email or password was incorrect."
  42.             });
  43.         }
  44.  
  45.         /**
  46.          * User authenticated!
  47.          */
  48.             req.session.user = user;
  49.             res.json({
  50.                 success: true,
  51.                 message: "Successfully authenticated."
  52.             });
  53.     });
  54. });
  55.  
  56. router.get("/", (req, res, next) => {
  57.     var admin = new User({
  58.         name: "admin",
  59.         email: "[email protected]",
  60.         password: "admin",
  61.         authorization: constants.authorization.admin
  62.     });
  63.  
  64. // the code throws an error here, saying that isModified(...) is not a function
  65.     admin.save(function(err) {
  66.         if (err)
  67.         {
  68.             console.log(err);
  69.         }
  70.  
  71.         console.log('User saved successfully');
  72.         res.json({ success: true });
  73.     });
  74. });
Advertisement
Add Comment
Please, Sign In to add comment