Skip to content

access_token null on custom middleware #3416

Closed

Description

@mahdinai

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 :(((

Activity

  1. yshing commented on May 23, 2017

    @yshing
    Member
  2. ydomenjoud commented on Jun 21, 2017

    @ydomenjoud

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

    req.accessToken is always undefined

  3. bitmage commented on Aug 8, 2017

    @bitmage
    Contributor

    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.

  4. stale commented on Oct 7, 2017

    @stale

    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.

  5. stale commented on Oct 21, 2017

    @stale

    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.

  6. stale commented on Oct 21, 2017

    @stale

    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.

  7. stale commented on Oct 21, 2017

    @stale

    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.

  8. stale commented on Oct 21, 2017

    @stale

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions