Detalhes do pacote

@auth0/angular-jwt

auth0909.8kMIT5.2.0

JSON Web Token helper library for Angular

angular, angular 2, authentication, jwt

readme (leia-me)

Helper library for handling JWTs in Angular applications

Release codecov Downloads License CircleCI

: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 @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:

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.


<picture> <source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150"> <source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150"> Auth0 Logo </picture>

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.

changelog (log de mudanças)

Change Log

v5.2.0 (2023-10-31)

Full Changelog

Changed

v5.1.2 (2022-12-20)

Full Changelog

Fixed

v5.1.1 (2022-12-15)

Full Changelog

Fixed

Version 5.1.0

Full Changelog

Changed

Note: This release drops support for Angular <12 as those versions are no longer supported by Google themselves. [Read more ...]

Version 5.0.2

Full Changelog

  • Update decodeToken helper type definition to accept a generic.

Version 5.0.1

Full Changelog

  • Remove dependency on the window object for SSR support

Version 5.0.0

Warning: this version has some breaking changes concerning the allowed domains and dissalowed routes!

  • Replace whitelistedDomains to allowedDomains #668
  • Replace blacklistedRoutes to disallowedRoutes #668
  • Removed the url dependency, as this is a Node module in the CommonJS format, and the Angular 10 CLI throws warnings when using dependencies in the CommonJS format. We're using the default URL interface, https://developer.mozilla.org/en-US/docs/Web/API/URL #666

Full Changelog

Version 4.2.0

Full Changelog

  • Allow the authScheme config parameter to be a getter function #659

Version 4.1.2 (2020-05-16)

Full Changelog

  • Support domains with a port other than the default HTTP ports (HTTP: 80, HTTPS: 443) #656

Version 4.1.1 (2020-05-15)

Full Changelog

  • Something went wrong pulishing v4.1.0, this version fixes that.

Version 4.1.0 (2020-05-15)

Full Changelog

  • Use blacklist domains regardless of their protocol #644
  • Pass the HttpRequest to the tokenGetter #649

Version 4.0.0 (2020-02-07)

Full Changelog

From #622 avatsaev:

  • Angular 9 compatibility
  • Angular Ivy compatibility

Version 3.0.1 (2019-10-28)

Full Changelog

Version 3.0.0 (2019-07-16)

Full Changelog

  • Breaking change isTokenExpired now returns false if no expiry date is found inside the token. This is a change to align with the JWT spec, but may break applications that rely on the previous behavior. #562 @atom-morgan

Version 2.1.2 (2019-07-15)

Full Changelog

  • Gracefully handle null/empty tokens #586

Version 2.1.1 (2019-07-01)

Full Changelog

  • Blacklist/Whitelist check fix #538
  • Refactor deep rxjs imports and use named define #608
  • fix(rxjs): remove imports from rxjs/internal #542

Note: historical changelog information has not been recorded in this format. Please see the releases page for information on previous releases.