@jjmhalew/angular-jwt
v21.0.1
Published
JSON Web Token helper library for Angular
Maintainers
Readme

:books: Documentation - :rocket: Getting Started - :computer: API Reference - :speech_balloon: Feedback
Documentation
- Examples - code samples for common angular-jwt authentication scenario's.
- Docs site - explore our docs site and learn more about Auth0.
This library provides an HttpInterceptor which automatically attaches a JSON Web Token to HttpClient requests.
This library does not have any functionality for (or opinion about) implementing user authentication and retrieving JWTs to begin with. Those details will vary depending on your setup, but in most cases, you will use a regular HTTP request to authenticate your users and then save their JWTs in local storage or in a cookie if successful.
Getting started
Requirements
This project only supports the actively supported versions of Angular as stated in the Angular documentation. Whilst other versions might be compatible they are not actively supported
Installation
# installation with npm
npm install @jjmhalew/angular-jwt
# installation with yarn
yarn add @jjmhalew/angular-jwtConfigure 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.
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.
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,
provideHttpClientneeds to be called withwithInterceptorsFromDi.
API reference
Read our API reference to get a better understanding on how to use this SDK.
Feedback
Contributing
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
- Auth0's general contribution guidelines
- Auth0's code of conduct guidelines
- This repo's contribution guide
Raise an issue
To provide feedback or report a bug, please raise an issue on our issue tracker.
Vulnerability Reporting
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
