0
module.exports = function(app) {
  var User = app.models.account;

  //login page
  app.get('/', function(req, res) {
    res.render('login');
  });

  //verified
  app.get('/verified', function(req, res) {
    res.render('verified');
  });

  //log a user in
  app.post('/login', function(req, res) {
    User.login({
      email: req.body.email,
      password: req.body.password
    }, 'user', function(err, token) {
      if (err) {
        res.render('response', {
          title: 'Login failed',
          content: err,
          redirectTo: '/',
          redirectToLinkText: 'Try again'
        });
        return;
      }

      res.render('home', {
        email: req.body.email,
        accessToken: token.id
      });
    });
  });
}

i am trying to link my model account which has as a base 'User'. and then call the User.login function etc.

but it says Cannot read property 'email' of undefined why?

this line is what gives the error it says email: req.body.email,

Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82
  • are you using a bodyparser middleware? also are you sure the email is send from the frontend? – eltonkamami Feb 13 '16 at 19:55
  • ah not really i think, i am following up the loopback user management example and trying to impement in my own setup. Do i need such a middleware then? – Petros Kyriakou Feb 13 '16 at 19:58
  • Also how can i check if indeed the stuff i want are send from the frontend? Sorry i am a bit noob on the framework – Petros Kyriakou Feb 13 '16 at 19:58
  • depending on your version this might help [how to use bodyparser on loopback](http://stackoverflow.com/questions/28523782/how-can-i-use-body-parser-with-loopback). for the check if the data go, either use postman so you can be sure they are present or check from the devtools that your request indeed has a payload – eltonkamami Feb 13 '16 at 20:01
  • right thank you, another question. If i were to test the reset password endpoint how would i do that? i know how to give email and have it send a reset email but how do i actually change the password in post man? i put headers the access_token, and body the password and confirmation fields isn't that right? – Petros Kyriakou Feb 13 '16 at 20:03
  • that again depends on how your framework hadles the reset of the password. postman can help shape any type of reaquest with any type of payload and headers going with it. so yes if your setup needs an access_token header and the password as body that can be done – eltonkamami Feb 13 '16 at 20:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103387/discussion-between-petros-kyriakou-and-antoniskamamis). – Petros Kyriakou Feb 13 '16 at 20:07

2 Answers2

1

As loopback is based on the express.js framework the body-parser middleware is needed but not by default enabled

have a look at these links for setting up bodyparser reddit thread
stackoverflow q&a

Community
  • 1
  • 1
eltonkamami
  • 5,134
  • 1
  • 22
  • 30
1

you need to put the body parser middleware in the middleware.json file. It is already included in the node_modules

"parse": {
  "body-parser#json": {},
  "body-parser#urlencoded": {"params": { "extended": true }}
}
gngchrs
  • 468
  • 4
  • 11