Loopback: access_token null on custom middleware

Created on 21 May 2017  路  8Comments  路  Source: strongloop/loopback

hi

server.js file:

app.use(cookieParser('secret'));
app.use(loopback.token({
  model: app.models.accessToken,
  cookies: ['access_token']
}));

custom middleware:

'use strict';
var user = require('../../common/proxies/UserProxy');

module.exports = status;
function status() {
  return function(req, res,next) {


    if (req.accessToken) { // access token is null 

      user.getProfile(req.accessToken.userId, function (user_res) {
        if (user_res.result) {
          next();

        }else{

          req.app.locals.msg = {
            hasMessage: true,
            Message: "",
            messageTitle: "",
            alertClassName: "alert-danger",
            app: req.app
          };

          res.redirect('/user/login');
        }
      });
  }else {

      req.app.locals.msg = {
        hasMessage: true,
        Message: "",
        messageTitle: "",
        alertClassName: "alert-danger",
        app: req.app
      };

      res.redirect('/user/login');

    }
  }


}

accesstoken is null in middleware , but work on route :(((

stale

Most helpful comment

actually this is an excellent question : how to get current user in custom middleware ?

req.accessToken is always undefined

All 8 comments

actually this is an excellent question : how to get current user in custom middleware ?

req.accessToken is always undefined

In Loopback, there's not 1 kind of middleware, there's about 5. So you have to sort out what order they all run in and how they interact. Personally I dodged this complexity by wedging my own strategy in. Not ideal, but at least I can reason about the order things run in.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

Was this page helpful?
0 / 5 - 0 ratings