1 | ![Helper library for handling JWTs in Angular applications](https://cdn.auth0.com/website/sdks/banners/angular-jwt-banner.png)
|
2 |
|
3 | ![Release](https://img.shields.io/github/v/release/auth0/angular2-jwt)
|
4 | [![codecov](https://codecov.io/gh/auth0/angular2-jwt/branch/main/graph/badge.svg?token=wnauXldcdE)](https://codecov.io/gh/auth0/angular2-jwt)
|
5 | ![Downloads](https://img.shields.io/npm/dw/@auth0/angular-jwt)
|
6 | [![License](https://img.shields.io/:license-MIT-blue.svg?style=flat)](https://opensource.org/licenses/MIT)
|
7 | [![CircleCI](https://img.shields.io/circleci/build/github/auth0/angular2-jwt)](https://circleci.com/gh/auth0/angular2-jwt)
|
8 |
|
9 | :books: [Documentation](#documentation) - :rocket: [Getting Started](#getting-started) - :computer: [API Reference](#api-reference) - :speech_balloon: [Feedback](#feedback)
|
10 |
|
11 | ## Documentation
|
12 |
|
13 | - [Examples](https://github.com/auth0/angular2-jwt/blob/main/EXAMPLES.md) - code samples for common angular-jwt authentication scenario's.
|
14 | - [Docs site](https://www.auth0.com/docs) - explore our docs site and learn more about Auth0.
|
15 |
|
16 | This library provides an `HttpInterceptor` which automatically attaches a [JSON Web Token](https://jwt.io) to `HttpClient` requests.
|
17 |
|
18 | 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.
|
19 |
|
20 | ## Getting started
|
21 | ### Requirements
|
22 | This project only supports the [actively supported versions of Angular as stated in the Angular documentation](https://angular.io/guide/releases#actively-supported-versions). Whilst other versions might be compatible they are not actively supported
|
23 |
|
24 | ### Installation
|
25 |
|
26 | ```bash
|
27 | # installation with npm
|
28 | npm install @auth0/angular-jwt
|
29 |
|
30 | # installation with yarn
|
31 | yarn add @auth0/angular-jwt
|
32 | ```
|
33 |
|
34 | ## Configure the SDK
|
35 |
|
36 | Import the `JwtModule` module and add it to your imports list. Call the `forRoot` method and 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.
|
37 |
|
38 | Be sure to import the `HttpClientModule` as well.
|
39 |
|
40 | ```ts
|
41 | import { JwtModule } from "@auth0/angular-jwt";
|
42 | import { HttpClientModule } from "@angular/common/http";
|
43 |
|
44 | export function tokenGetter() {
|
45 | return localStorage.getItem("access_token");
|
46 | }
|
47 |
|
48 | @NgModule({
|
49 | bootstrap: [AppComponent],
|
50 | imports: [
|
51 | // ...
|
52 | HttpClientModule,
|
53 | JwtModule.forRoot({
|
54 | config: {
|
55 | tokenGetter: tokenGetter,
|
56 | allowedDomains: ["example.com"],
|
57 | disallowedRoutes: ["http://example.com/examplebadroute/"],
|
58 | },
|
59 | }),
|
60 | ],
|
61 | })
|
62 | export class AppModule {}
|
63 | ```
|
64 |
|
65 | Any requests sent using Angular's `HttpClient` will automatically have a token attached as an `Authorization` header.
|
66 |
|
67 | ```ts
|
68 | import { HttpClient } from "@angular/common/http";
|
69 |
|
70 | export class AppComponent {
|
71 | constructor(public http: HttpClient) {}
|
72 |
|
73 | ping() {
|
74 | this.http.get("http://example.com/api/things").subscribe(
|
75 | (data) => console.log(data),
|
76 | (err) => console.log(err)
|
77 | );
|
78 | }
|
79 | }
|
80 | ```
|
81 |
|
82 | ## API reference
|
83 | Read [our API reference](https://github.com/auth0/angular2-jwt/blob/main/API.md) to get a better understanding on how to use this SDK.
|
84 |
|
85 | ## Feedback
|
86 |
|
87 | ### Contributing
|
88 |
|
89 | We appreciate feedback and contribution to this repo! Before you get started, please see the following:
|
90 |
|
91 | - [Auth0's general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md)
|
92 | - [Auth0's code of conduct guidelines](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md)
|
93 | - [This repo's contribution guide](https://github.com/auth0/angular2-jwt/blob/main/CONTRIBUTING.md)
|
94 | ### Raise an issue
|
95 |
|
96 | To provide feedback or report a bug, please [raise an issue on our issue tracker](https://github.com/auth0/angular2-jwt/issues).
|
97 |
|
98 | ### Vulnerability Reporting
|
99 |
|
100 | Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/responsible-disclosure-policy) details the procedure for disclosing security issues.
|
101 |
|
102 | ---
|
103 |
|
104 | <p align="center">
|
105 | <picture>
|
106 | <source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
107 | <source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150">
|
108 | <img alt="Auth0 Logo" src="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
109 | </picture>
|
110 | </p>
|
111 | <p align="center">Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href="https://auth0.com/why-auth0">Why Auth0?</a></p>
|
112 | <p align="center">
|
113 | This project is licensed under the MIT license. See the <a href="https://github.com/auth0/angular2-jwt/blob/main/LICENSE"> LICENSE</a> file for more info.</p>
|