kurierjs / kurier

TypeScript framework to create JSON:API compliant APIs
https://kurier.readthedocs.io/en/latest/
MIT License
61 stars 9 forks source link

Support for using a custom SessionProcessor and SessionResource in UserManagementAddon #345

Open joelalejandro opened 1 year ago

joelalejandro commented 1 year ago

Right now, the login callback is very lean and simple, receiving two arguments:

type LoginCallbackSignature = (op: Operation, user: ResourceAttributes) => boolean;

It is possible that more data is required to do a login operation, and that such data lives in other resources. Ideally we'd want to access Knex from here, but we need to resort to function factories to do so:

app.use(UserManagementAddon, {
   // ...
   userLoginCallback: myLogin(app.services.knex)
});

const myLogin = (knex: Knex) => (op: Operation, user: ResourceAttributes) => {
  // knex...
}

Since we already allow the UserProcessor to be customized, we should allow SessionProcessor and SessionResource as well. Processors extending from KnexProcessor have access to the service via this.knex.

Usage of the addon would then look like:

app.use(UserManagementAddon, {
  userResource: MyUser,
  userProcessor: MyUserProcessor,
  sessionResource: MySession,
  sessionProcessor: MySessionProcessor,
});

Such a processor would be defined as:

      class MySessionProcessor<T extends Session> extends JsonApiSessionProcessor<T> {
        public static resourceClass = MySession; // extends Session

        protected async login(op: Operation, userDataSource: ResourceAttributes): Promise<boolean> {
          // Do the login.
        }
      }