question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OAuthService not registered for DI if used in custom wrapper library

See original GitHub issue

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):

  • 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
steffbeckerscommented, Nov 3, 2022

@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 😄

1reaction
ruvan83commented, Oct 30, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular (8): Custom-Auth-Module should handle/wrap an ...
Now I trying to create such wrapping module, but my implementation is failing as whatever I do, the external OAuthService is not correctly ......
Read more >
New dependency injection features in .NET 6
Using a custom DI container with WebApplicationBuilder; Detecting if a service is registered in the DI container without creating it; Additional ...
Read more >
angular-oauth2-oidc - Bountysource
OAuthService not registered for DI if used in custom wrapper library $ 0. Created 2 years ago in manfredsteyer/angular-oauth2-oidc with 5 comments. Describe...
Read more >
The complete guide to protecting your APIs with OAuth2 (part 1)
An OAuth grant is a specific flow that results in an access token. Per the specification, a token is an opaque string without...
Read more >
The Client ID and Secret - OAuth 2.0 Simplified
At this point, you've built the application registration screen, ... a secure secret is to use a cryptographically-secure library to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found