JSON Web Token helper library for Angular
npm install @jjmhalew/angular-jwt!Helper library for handling JWTs in Angular applications
!Release


!Downloads


:books: Documentation - :rocket: Getting Started - :computer: API Reference - :speech_balloon: Feedback
HttpInterceptor which automatically attaches a JSON Web Token to HttpClient requests.
bash
installation with npm
npm install @jjmhalew/angular-jwt
installation with yarn
yarn add @jjmhalew/angular-jwt
`
Configure the SDK
Import provideJwtConfig and add it to your imports list. Provide a tokenGetter function. You must also add any domains to the allowedDomains, that you want to make requests to by specifying an allowedDomains array.
Be sure to import the HttpClientModule as well.
`ts
import { JwtModule } from "@jjmhalew/angular-jwt";
import { provideHttpClient, withInterceptorsFromDi } from "@angular/common/http";
export function tokenGetter() {
return localStorage.getItem("access_token");
}
export const appConfig: ApplicationConfig = {
providers: [
// ...
provideHttpClient(withInterceptorsFromDi()),
provideJwtConfig({
tokenGetter: tokenGetter,
allowedDomains: ["example.com"],
disallowedRoutes: ["http://example.com/examplebadroute/"],
}),
],
};
`
Any requests sent using Angular's HttpClient will automatically have a token attached as an Authorization header.
`ts
import { HttpClient } from "@angular/common/http";
import { inject } from "@angular/core";
export class AppComponent {
public http = inject(HttpClient);
ping() {
this.http.get("http://example.com/api/things").subscribe(
(data) => console.log(data),
(err) => console.log(err)
);
}
}
`
- In order to use the SDK's interceptor, provideHttpClient needs to be called with withInterceptorsFromDi`.
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the MIT license. See the LICENSE file for more info.