@auth0/angular-jwt
TypeScript icon, indicating that this package has built-in type declarations

5.2.0 • Public • Published

Helper library for handling JWTs in Angular applications

Release codecov Downloads License CircleCI

📚 Documentation - 🚀 Getting Started - 💻 API Reference - 💬 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 @auth0/angular-jwt

# installation with yarn
yarn add @auth0/angular-jwt

Configure the SDK

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.

Be sure to import the HttpClientModule as well.

import { JwtModule } from "@auth0/angular-jwt";
import { HttpClientModule } from "@angular/common/http";

export function tokenGetter() {
  return localStorage.getItem("access_token");
}

@NgModule({
  bootstrap: [AppComponent],
  imports: [
    // ...
    HttpClientModule,
    JwtModule.forRoot({
      config: {
        tokenGetter: tokenGetter,
        allowedDomains: ["example.com"],
        disallowedRoutes: ["http://example.com/examplebadroute/"],
      },
    }),
  ],
})
export class AppModule {}

Any requests sent using Angular's HttpClient will automatically have a token attached as an Authorization header.

import { HttpClient } from "@angular/common/http";

export class AppComponent {
  constructor(public http: HttpClient) {}

  ping() {
    this.http.get("http://example.com/api/things").subscribe(
      (data) => console.log(data),
      (err) => console.log(err)
    );
  }
}

Using with Standalone Components

If you are using bootstrapApplication to bootstrap your application using a standalone component, you will need a slightly different way to integrate our SDK:

import { JwtModule } from "@auth0/angular-jwt";
import { provideHttpClient, withInterceptorsFromDi } from "@angular/common/http";

export function tokenGetter() {
  return localStorage.getItem("access_token");
}

bootstrapApplication(AppComponent, {
    providers: [
        // ...
        importProvidersFrom(
            JwtModule.forRoot({
                config: {
                    tokenGetter: tokenGetter,
                    allowedDomains: ["example.com"],
                    disallowedRoutes: ["http://example.com/examplebadroute/"],
                },
            }),
        ),
        provideHttpClient(
            withInterceptorsFromDi()
        ),
    ],
});

As you can see, the differences are that:

  • The SDK's module is included trough importProvidersFrom.
  • In order to use the SDK's interceptor, provideHttpClient needs to be called with withInterceptorsFromDi.

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:

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.


Auth0 Logo

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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
5.2.0
117,673latest
1.0.0-beta.112beta

Version History

VersionDownloads (Last 7 Days)Published
5.2.0
117,673
5.1.243,408
5.1.1781
5.1.08,964
5.0.220,487
5.0.12,242
5.0.0401
4.2.04,306
4.1.2206
4.1.15
4.1.02
4.0.01,850
3.0.17,939
3.0.01,269
2.1.28,170
2.1.144
2.1.01,076
2.0.0740
1.2.0269
1.1.053
1.0.039
1.0.0-beta.112
1.0.0-beta.101
1.0.0-beta.9130
1.0.0-beta.80
1.0.0-beta.70
1.0.0-beta.60
1.0.0-beta.50
1.0.0-beta.40
1.0.0-beta.30
1.0.0-beta.20
1.0.0-beta.10

Package Sidebar

Install

npm i @auth0/angular-jwt

Weekly Downloads

220,057

Version

5.2.0

License

MIT

Unpacked Size

125 kB

Total Files

19

Last publish

Collaborators

  • npirani_okta
  • soumya.bodavula
  • oktajeffoktajeff
  • amandamdacruz0
  • rithuc23
  • jfromaniello
  • edgarchirivella-okta
  • sanjay.manikandhan
  • ncluer
  • vic-dev
  • enriquepina
  • ece-okta
  • pubalokta
  • dougmiller-okta
  • zak.nour
  • stheller
  • jamescgarrett-okta
  • madhuri.rm23
  • david.renaud.okta
  • auth0-oss
  • codepete
  • ziluvatar
  • iaco
  • auth0npm
  • auth0brokkr
  • hzalaz
  • aaguiarz
  • charlesrea
  • julien.wollscheid
  • cristiandouce
  • sambego
  • sandrinodimattia
  • lzychowski
  • davidpatrick0
  • sergii.biienko
  • tomauth0
  • jpadilla
  • jessele
  • rhamzeh_auth0