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.

Issue with JwtHelperService in new implementation

See original GitHub issue

Im not sure if the JwtHelperService can be used together with the Interceptor. I want to use it in another service I have the following issue.

Cannot instantiate cyclic dependency! JWT_OPTIONS (“[ERROR ->]”): in NgModule CoreModule

Settings

CoreModule

    JwtModule.forRoot({
      jwtOptionsProvider: {
        provide: JWT_OPTIONS,
        useFactory: jwtOptionsFactory,
        deps: [TokenHandler]
      }
    }),

TokenHandler (own class for jwt decode …)

constructor(private cookieService: CookieService, private jwtHelper: JwtHelperService) { }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

6reactions
lehoffmacommented, Nov 13, 2017

I have the same issue, but I still don’t know how to resolve it.

I import the JwtModule with the forRoot() stuff in my main app.module like this:

JwtModule.forRoot(
	{
		jwtOptionsProvider: {
			provide: JWT_OPTIONS,
			useFactory: jwtOptionsFactory,
			deps: [AuthService]
		}
	}
)

The jwtOptionsFactory looks like this:


export function jwtOptionsFactory(tokenService: AuthService) {
	return {
		tokenGetter: () => {
			return tokenService.getToken();
		}
	}
}

Both the JwtHelperService and the HttpClient get injected into the AuthService, because it implements the token refreshing logic, too.

The resulting error looks like this:

Uncaught Error: Provider parse errors:
Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
Cannot instantiate cyclic dependency! InjectionToken_JWT_OPTIONS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1

The AuthService is placed inside its own AuthModule, which is also included (with forRoot()) in the appModule. The JwtModule is only included in the AppModule though.

Oh another maybe useful info: I’m using my own (second) interceptor to handle token refresh logic, which depends on the AuthService, too.

Is there something I did wrong?

3reactions
xavaducommented, Jul 13, 2018

Hi all, FTR, I was facing the same issue, the problem is that we need to inject a service in the jwtOptionsFactory but this service is also provide in the module providers.

it was tricky, maybe not the best, but I can solved the issue on that way:

app.module.ts

// [..]
import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt';
import { jwtOptionsFactory } from './authentication/jwtOptions.factory'
// [..]
import { AuthService } from './authentication/auth.service';
import { ConfigService } from './services/config.service';

@NgModule({
    imports: [
        JwtModule.forRoot({
            jwtOptionsProvider: {
                provide: JWT_OPTIONS,
                useClass: jwtOptionsFactory
            }
        })
    ],
    // [...]
    providers: [
        AuthService,
        ConfigService,
// [..]

jwtOptions.factory.ts

import { Injector, Injectable } from '@angular/core';

import { AuthService } from './auth.service';
import { ConfigService } from '../services/config.service';

// https://github.com/auth0/angular2-jwt/issues/416
@Injectable()
export class jwtOptionsFactory {
    private authService: AuthService;
    private configService: ConfigService;

    public skipWhenExpired: boolean = true;
    public whitelistedDomains: string[];
    public blacklistedRoutes: string[];

    constructor(inj: Injector) {
        // https://github.com/angular/angular/issues/18224
        // https://github.com/angular/angular/issues/18224#issuecomment-316951787
        setTimeout(() => {
            this.authService = inj.get(AuthService);
            this.configService = inj.get(ConfigService);

            this.whitelistedDomains = this.configService.get('environment.jwt.whitelistedDomains');
            this.blacklistedRoutes = this.configService.get('environment.jwt.blacklistedRoutes');
        });
    }

    public tokenGetter = () => this.authService.getToken();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

NullInjectorError: No provider for JwtHelperService
This solved my issue, but since my implementation was in my auth service ... public jwtHelper: JwtHelperService = new JwtHelperService();
Read more >
ASP.NET Core Authentication with JWT and Angular - Part 2
Before we start with the login implementation, we are going to create a new _interfaces folder, and inside a new login.model file:.
Read more >
Client-side Application For JWT Refresh Token In Angular 13
Refresh tokens are the kind of tokens that can be used to get new access tokens. ... import { JwtHelperService } from '@auth0/angular-jwt'; ......
Read more >
Angular(v14) JWT Access Token Authentication & Refresh ...
In this article, we are going to implement JWT(JSON Web Token) authentication in ... jwtService: JwtHelperService = new JwtHelperService(); ...
Read more >
Using JWT with Spring Security OAuth - Baeldung
A practical deep-dive into how to implement logout in a Spring ... It issues JWT tokens by default, so there is no need...
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