简体   繁体   中英

How to integrate auth step into feathers service

I'm trying to integrate jwt/local auth into any call to feathers. So by including user/pass in any call it authorizes, rather than requiring a individual previous call to the /authorization endpoint and passing a header to subsequent calls. Ive tried using authenticate, but it doesnt see my auth strategies from within a hook. Here is the current create code, it does return the jwt token but how do i auth from here and integrate user data from the database into context like the auth generally would otherwise?

module.exports = (options = {}) => {
  return async (context) => {
    let promise = new Promise((resolve, reject) => {
      context.app
        .service("authentication")
        .create({
          username: context.data.username,
          password: context.data.password,
          strategy:"local"
        })
        .then((response) => {
          console.log(response)
          context.arguments[1]['Authorization'] = response.accessToken
          resolve(context);
        });
    });
    let result = await promise;
    if (result) {
      return context;
    }
  };
};

The authenticate hook can take any strategy and will look for whatever you pass in params.authentication . In your example the following:

module.exports = (options = {}) => {
  return async (context) => {
    context.params = {
      ...context.params,
      authentication: {
        strategy: 'local',
        username: context.data.username,
        password: context.data.password
      }
    }
    
    return context;
  };
};

Would allow for authenticate('local') to run. If you want to get the authentication information from an HTTP request you can implement the parse(req, res) method in a custom strategy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM