Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
77
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.         //ERROR
  44.         // user.verifyPassword(req.body.password, match => {
  45.         //     ^
  46.         // TypeError: user.verifyPassword is not a function
  47.         user.verifyPassword(req.body.password, match => {
  48.             if (!match)
  49.             {
  50.                 return res.json({
  51.                     success:    false,
  52.                     message:    "Email or password was incorrect."
  53.                 });
  54.             }
  55.  
  56.             /**
  57.              * User authenticated!
  58.              */
  59.             req.session.user = user;
  60.             res.json({
  61.                 success: true,
  62.                 message: "Successfully authenticated."
  63.             });
  64.         });
  65.        
  66.     });
  67. });
  68.  
  69. router.get("/", (req, res, next) => {
  70.     var admin = new User({
  71.         name: "admin",
  72.         email: "admin@admin.net",
  73.         password: "admin",
  74.         authorization: constants.authorization.admin
  75.     });
  76.  
  77.     // ERROR
  78.     // if (!user.isModified("password"))
  79.     //            ^
  80.     // TypeError: user.isModified is not a function
  81.     admin.save(function(err) {
  82.         if (err)
  83.         {
  84.             console.log(err);
  85.         }
  86.  
  87.         console.log('User saved successfully');
  88.         res.json({ success: true });
  89.     });
  90. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement