1

I am using feathers.js for my app. In my app, I use different strategies. For all those strategies the entity is user. But I also want to use local strategy for internal operators. The global entity is set to user but for local strategy which I registered under the name of operator I set the entity to operator (I have user and operator tables in my Postgres database). This is my config/default.js:

}
  ...
  authentication: {
    secret: "JWT_SECRET",
    entity: "user",
    service: "user",
    phone: {
      entity: "user",
      service: "user"
    },
    jwt: {},
    operator: {
      entity: "operator",
      service: "operator",
      usernameField: "email",
      passwordField: "password"
    },
    authStrategies: ["jwt", "phone, "operator"],
    ...
}

This is my authentication.ts file (I omitted some code for brewety):

export default function(app: Application): void {
  authentication.register("phone", new PhoneStrategy());
  authentication.register("jwt", new JwtStrategy());
  authentication.register("operator", new OperatorStrategy());

  app.use("/authentication", authentication);
  app.configure(expressOauth());
}

class OperatorStrategy extends LocalStrategy {
  async authenticate(data: any, params: Params) {
    const operator = await this.app?.service("operator").find({
      query: {
        email: data.email,
        $limit: 1
      }
    });

    if (operator) {
      const encryptedPass = getHash(data.password, operator.id);
      if (operator.password === encryptedPass) {
        const { email, password, ...restOfOperator } = operator;
        return restOfOperator;
      }
    } else {
      throw new NotAuthenticated();
    }
  }
}

I have service called operator and hooks for this service.


const isAuthorized = async (
  context: HookContext
): Promise<HookContext> => {
  console.log("User: ", context.params.user)

  }
  return context;
}

export default {
  before: {
    all: [],
    find: [authenticate("operator"), isAuthorized],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },
  ...
}

So you can see above I am calling authenticate("operator") before find() and then I was going to call isAuthorized to do some checks. However, context.params.user returns undefined.

It looks like for some reason the authentication service uses global entity and service variables when constructing the JWT.

Is there something missing in my configuration to make auth service use operator entity? Or another words is there a way to use operator service for local strategy instead of global user?

buterfly85
  • 163
  • 3
  • 11
  • Not sure if it's because you omitted the code, but the docs suggest a slightly different setup for the auth service (https://docs.feathersjs.com/api/authentication/service.html). Are you defining the auth service with `const authService = new AuthenticationService(app);` before registering the strategies? – cdimitroulas Mar 17 '21 at 21:48
  • yes, I have `const authentication = new MyAuthService(app);` before registering the services. – buterfly85 Mar 17 '21 at 23:19

0 Answers0