OAuthService not registered for DI if used in custom wrapper library
See original GitHub issueDescribe 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:
- Create a library project
- Add a simple wrapper for initialization of the client
- Create an application project
- Include the library into the application
- Try to call the wrapper from the application.
- Exception that OAuthService is not registerd
Expected behavior Possibility to use the library through a custom wrapper library.
Desktop (please complete the following information):
- OS: Windows
- Browser chrome
- Version 81.0.4044.122 (Official Build) (64-bit)
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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6
Top GitHub Comments
@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:
You can now inject the OAuthService in another library (which is used by the app) using the existing instance of the service.
For the ‘OAUTH_SERVICE’ string you could also use an injection token: https://angular.io/api/core/InjectionToken
I hope this works for you 😄
@steffbeckers do you mind sharing your implementation code snippet or gist?