lupu60 / passport-dynamic-state

nestjs passportjs dynamic state
5 stars 0 forks source link

Dynamic OAuth providers and settings #1

Open VigneshVaidyanathan opened 3 years ago

VigneshVaidyanathan commented 3 years ago

HELP NEEDED

@lupu60 It is a great workaround for using dynamic values for passport in Nestjs. I am having a slightly different requirement to complete for which I need help/ direction.

I need to build a simple authentication microservice (like Auth0) which will be linked to multiple apps for performing the authentication. This microservice will also contain Oauth providers like Google, Facebook etc and each app will have its own settings for each provider (client id, secret, callback url etc).

When a request to perform oauth authentication is received, the microservice needs to pick the right settings for the provider and app combination and send it to passport to perform authentication.

I am pretty new to Nest Js so I am not sure if we can get it done using Custom providers as you have done but there is a block where passport cannot work with request - it has to be registered on global level. But I would want passport to register and authenticate new strategies on every request.

Please let me know if you have an idea on how it can be done.

lupu60 commented 3 years ago

Hey man, sorry for the long delay didn't see the message. So basically I think you can use the same approach with custom providers and a generic function that will register your user. You will need to create a strategy for every provider FB, google, etc and call the same function in all providers at this lvl https://github.com/lupu60/passport-dynamic-state/blob/c21618e1405ff68a9e56dd9dd13eebfc48d4defd/src/auth/facebook.strategy.ts#L27 the function can look something like this

async socialLogin(options: {
    request: any;
    accessToken: string;
    refreshToken: string;
    profile: any;
    done: Callback;
  }): Promise<Callback> {
// check if user is already in the db 
// register if not
// refresh token if yes
// call done()
}

I will probably add something in this direction in https://github.com/lupu60/nestjs-toolbox but I don't know when I have time. So if you already figured out who to do it feel free to open a pr.