authenticationaccess-tokenstronglooploopback

how to skip access token validation automatic in loopback


I have two User models (A and B) and two accesstoken models, the original and a custom (base: "AccessToken"), I managed to make that the tokens of each User model be saved in each table (mysql).

But the problem in this moment is that loopback validates the accesstoken only in a model (table), and I want it validates in the two models (tables).

I am trying to do the first validation by myself before loopback validates in the default model (table), but I dont know how to skip access token validation automatic if I find that the accesstoken is correct in my first validation.

Any idea about it?

server/server.js

app.use(function(req, res, next) {

 const CustomAccessToken = app.models.CustomAccessToken;

 CustomAccessToken.resolve(req.headers.authorization, function(err, token){
     if(err){
        console.log(err);
     }else{
        console.log(token, "Correct!");
        // Skip default accesstoken valitation
     }
     return next();
  });

});


Solution

  • I already solved it.

    app.use(function(req, res, next) { 
      
      const adminaccesstoken = app.models.adminaccesstoken; 
      var currentToken = req.headers.authorization; 
    
      if (typeof currentToken != 'undefined') { 
         
         adminaccesstoken.resolve(currentToken, function(err, cToken){ 
            if(err){ return next(err); } 
            if (typeof cToken != 'undefined') { 
              req.accessToken = cToken; 
            } 
            return next(); 
         });
     
       } else { return next(); } 
    
    });