manfredsteyer / angular-oauth2-oidc

Support for OAuth 2 and OpenId Connect (OIDC) in Angular.
MIT License
1.9k stars 688 forks source link

OAuthService not registered for DI if used in custom wrapper library #801

Open M4n1x opened 4 years ago

M4n1x commented 4 years ago

Describe the bug OAuthService cannot be resolved by dependency injection. I have the need to provide a custom wrapper around your API for company specific use cases. Therefore I want to use this library through a self written library.

Angular Project A should use OpenId

MyOpenIdWrapper is a wrapper around this library.

OAuthService is only used within MyOpenIdWrapper and cannot be resolved inside a service in the MyOpenIdWrapper library.

Using this library directly in Angular Project A is no problem.

Stackblitz example Not provided, since it would require a custom library in between.

To Reproduce Steps to reproduce the behavior:

  1. Create a library project
  2. Add a simple wrapper for initialization of the client
  3. Create an application project
  4. Include the library into the application
  5. Try to call the wrapper from the application.
  6. Exception that OAuthService is not registerd

Expected behavior Possibility to use the library through a custom wrapper library.

Desktop (please complete the following information):

Additional context The following change fixes the Problem: Change

@Injectable()

to

@Injectable({
  providedIn: 'root'
})

in oauth-service.ts and remove OAuthService registration from angular-oauth-oidc.module.ts

beerran commented 4 years ago

We need this exact fix aswell, for the same exact reasons

markusfalk commented 4 years ago

This is especially true when using Nx with libraries :)

Xbloud commented 2 years ago

Still not working, do you plan to fix this? It is quite limiting and forcing me to use another OIDC library.

steffbeckers commented 2 years ago

I've added a provider with DI injection token & useExisting for the OAuthService dependency, then injected it in the other lib by referencing the injection token.

ruvan83 commented 2 years ago

@steffbeckers do you mind sharing your implementation code snippet or gist?

steffbeckers commented 2 years ago

@steffbeckers do you mind sharing your implementation code snippet or gist?

@ruvan83, the following solution worked for me, not sure if this is a best practice. I've added the following provider to the module where the OAuthModule.forRoot() is imported:

@NgModule({
  imports: [
    OAuthModule.forRoot()
  ],
  providers: [
    {
      provide: 'OAUTH_SERVICE',
      useExisting: OAuthService
    }
  ]
})
export class AppModule {}

You can now inject the OAuthService in another library (which is used by the app) using the existing instance of the service.

@Injectable()
export class SomeServiceInOtherLib {
  constructor(@Inject('OAUTH_SERVICE') private oAuthService: OAuthService) {}
}

For the 'OAUTH_SERVICE' string you could also use an injection token: https://angular.io/api/core/InjectionToken

I hope this works for you 😄