NullInjectorError: No provider for JwtHelperService
You need to use JwtModule
provided by the @auth0/angular-jwt
, which will add JwtHelperService
to the providers, or you need to add it manually to the modules provider.
Something like
const JWT_Module_Options: JwtModuleOptions = {
config: {
tokenGetter: yourTokenGetter,
whitelistedDomains: yourWhitelistedDomains
}
};
@NgModule({
imports: [
JwtModule.forRoot(JWT_Module_Options)
],
...
Fore more see Documentation
For future reference, if all you want to use JwtHelper
for is decoding, like in this case checking if the token is expired, then you can use this.
import { JwtHelperService } from '@auth0/angular-jwt';
const jwtHelper = new JwtHelperService();
@Injectable()
export class AuthService {
public isAuthenticated(): boolean {
const token = localStorage.getItem('token');
// Check if the token is expired and return true or false
return !this.jwtHelper.isTokenExpired(token);
}
Source: Documentation
A little late to the party, but I ran into the same issue trying to follow the standalone docs and what it doesn't cover is the need to import the options InjectionToken which is referenced in the constructor of the service:
import { JwtHelperService, JWT_OPTIONS } from '@auth0/angular-jwt';
...
providers: [
{ provide: JWT_OPTIONS, useValue: JWT_OPTIONS },
JwtHelperService
]