Installations
npm install angular-jwt-async
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.15.3
NPM Version
6.9.0
Score
66
Supply Chain
95.4
Quality
75
Maintenance
100
Vulnerability
98.9
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (93.24%)
JavaScript (5.92%)
HTML (0.66%)
SCSS (0.18%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
auth0
Download Statistics
Total Downloads
612
Last Day
1
Last Week
2
Last Month
13
Last Year
61
GitHub Statistics
MIT License
2,632 Stars
529 Commits
485 Forks
87 Watchers
11 Branches
103 Contributors
Updated on Feb 13, 2025
Bundle Size
4.77 kB
Minified
1.86 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.0.1
Package Id
angular-jwt-async@0.0.1
Unpacked Size
285.67 kB
Size
178.19 kB
File Count
22
NPM Version
6.9.0
Node Version
10.15.3
Total Downloads
Cumulative downloads
Total Downloads
612
Last Day
0%
1
Compared to previous day
Last Week
-75%
2
Compared to previous week
Last Month
160%
13
Compared to previous month
Last Year
-72.6%
61
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
1
Dev Dependencies
31
@auth0/angular-jwt
NOTE: This library is now at version 3 and is published on npm as @auth0/angular-jwt
. If you're looking for the pre-v1.0 version of this library, it can be found in the pre-v1.0
branch and on npm as angular2-jwt
.
@auth0/angular-jwt v3 is to be used with Angular v6+ and RxJS v6+. For Angular v4.3 to v5+, use @auth0/angular-jwt v1
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.
Note: This library can only be used with Angular 4.3 and higher because it relies on an
HttpInterceptor
from Angular'sHttpClient
. This feature is not available on lower versions.
Installation
1# installation with npm 2npm install @auth0/angular-jwt 3 4# installation with yarn 5yarn add @auth0/angular-jwt
Usage: Standalone
If you are only interested in the JWT Decoder, and are not interested in extended injectable features, you can simply create an instance of the utility and use it directly:
1import { JwtHelperService } from "@auth0/angular-jwt"; 2 3const helper = new JwtHelperService(); 4 5const decodedToken = helper.decodeToken(myRawToken); 6const expirationDate = helper.getTokenExpirationDate(myRawToken); 7const isExpired = helper.isTokenExpired(myRawToken);
Usage: Injection
Import the JwtModule
module and add it to your imports list. Call the forRoot
method and provide a tokenGetter
function. You must also whitelist any domains that you want to make requests to by specifying a whitelistedDomains
array.
Be sure to import the HttpClientModule
as well.
1import { JwtModule } from "@auth0/angular-jwt"; 2import { HttpClientModule } from "@angular/common/http"; 3 4export function tokenGetter() { 5 return localStorage.getItem("access_token"); 6} 7 8@NgModule({ 9 bootstrap: [AppComponent], 10 imports: [ 11 // ... 12 HttpClientModule, 13 JwtModule.forRoot({ 14 config: { 15 tokenGetter: tokenGetter, 16 whitelistedDomains: ["example.com"], 17 blacklistedRoutes: ["example.com/examplebadroute/"] 18 } 19 }) 20 ] 21}) 22export class AppModule {}
Any requests sent using Angular's HttpClient
will automatically have a token attached as an Authorization
header.
1import { HttpClient } from "@angular/common/http"; 2 3export class AppComponent { 4 constructor(public http: HttpClient) {} 5 6 ping() { 7 this.http 8 .get("http://example.com/api/things") 9 .subscribe(data => console.log(data), err => console.log(err)); 10 } 11}
Configuration Options
tokenGetter: function
The tokenGetter
is a function which returns the user's token. This function simply needs to make a retrieval call to wherever the token is stored. In many cases, the token will be stored in local storage or session storage.
1// ... 2JwtModule.forRoot({ 3 config: { 4 // ... 5 tokenGetter: () => { 6 return localStorage.getItem("access_token"); 7 } 8 } 9});
whitelistedDomains: array
Authenticated requests should only be sent to domains you know and trust. Many applications make requests to APIs from multiple domains, some of which are not controlled by the developer. Since there is no way to know what the API being called will do with the information contained in the request, it is best to not send the user's token any and all APIs in a blind fashion.
List any domains you wish to allow authenticated requests to be sent to by specifying them in the the whitelistedDomains
array. Note that standard http port 80 and https port 443 requests don't require a port to be specified. A port is only required in the whitelisted host name if you are authenticating against a non-standard port e.g. localhost:3001
1// ... 2JwtModule.forRoot({ 3 config: { 4 // ... 5 whitelistedDomains: ["localhost:3001", "foo.com", "bar.com"] 6 } 7});
blacklistedRoutes: array
If you do not want to replace the authorization headers for specific routes, list them here. This can be useful if your initial auth route(s) are on a whitelisted domain and take basic auth headers.
1// ...
2JwtModule.forRoot({
3 config: {
4 // ...
5 blacklistedRoutes: [
6 "localhost:3001/auth/",
7 "foo.com/bar/",
8 /localhost:3001\/foo\/far.*/
9 ] // strings and regular expressions
10 }
11});
Note: If requests are sent to the same domain that is serving your Angular application, you do not need to add that domain to the whitelistedDomains
array. However, this is only the case if you don't specify the domain in the Http
request.
For example, the following request assumes that the domain is the same as the one serving your app. It doesn't need to be whitelisted in this case.
1this.http.get('/api/things') 2 .subscribe(...)
However, if you are serving your API at the same domain as that which is serving your Angular app and you are specifying that domain in Http
requests, then it does need to be whitelisted.
1// Both the Angular app and the API are served at 2// localhost:4200 but because that domain is specified 3// in the request, it must be whitelisted 4this.http.get('http://localhost:4200/api/things') 5 .subscribe(...)
headerName: string
The default header name is Authorization
. This can be changed by specifying a custom headerName
which is to be a string value.
1// ... 2JwtModule.forRoot({ 3 config: { 4 // ... 5 headerName: "Your Header Name" 6 } 7});
authScheme: string
The default authorization scheme is Bearer
followed by a single space. This can be changed by specifying a custom authScheme
which is to be a string.
1// ... 2JwtModule.forRoot({ 3 config: { 4 // ... 5 authScheme: "Your Auth Scheme" 6 } 7});
throwNoTokenError: boolean
Setting throwNoTokenError
to true
will result in an error being thrown if a token cannot be retrieved with the tokenGetter
function. Defaults to false
.
1// ... 2JwtModule.forRoot({ 3 config: { 4 // ... 5 throwNoTokenError: true 6 } 7});
skipWhenExpired: boolean
By default, the user's JWT will be sent in HttpClient
requests even if it is expired. You may choose to not allow the token to be sent if it is expired by setting skipWhenExpired
to true.
1// ... 2JwtModule.forRoot({ 3 config: { 4 // ... 5 skipWhenExpired: true 6 } 7});
Using a Custom Options Factory Function
In some cases, you may need to provide a custom factory function to properly handle your configuration options. This is the case if your tokenGetter
function relies on a service or if you are using an asynchronous storage mechanism (like Ionic's Storage
).
Import the JWT_OPTIONS
InjectionToken
so that you can instruct it to use your custom factory function.
Create a factory function and specify the options as you normally would if you were using JwtModule.forRoot
directly. If you need to use a service in the function, list it as a parameter in the function and pass it in the deps
array when you provide the function.
1import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt'; 2import { TokenService } from './app.tokenservice'; 3 4// ... 5 6export function jwtOptionsFactory(tokenService) { 7 return { 8 tokenGetter: () => { 9 return tokenService.getAsyncToken(); 10 } 11 } 12} 13 14// ... 15 16@NgModule({ 17 // ... 18 imports: [ 19 JwtModule.forRoot({ 20 jwtOptionsProvider: { 21 provide: JWT_OPTIONS, 22 useFactory: jwtOptionsFactory, 23 deps: [TokenService] 24 } 25 }) 26 ], 27 providers: [TokenService] 28})
NOTE: If a jwtOptionsFactory
is defined, then config
is ignored. Both configuration alternatives can't be defined at the same time.
Configuration for Ionic 2+
The custom factory function approach described above can be used to get a token asynchronously with Ionic's Storage
.
1import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt'; 2import { Storage } from '@ionic/storage'; 3 4export function jwtOptionsFactory(storage) { 5 return { 6 tokenGetter: () => { 7 return storage.get('access_token'); 8 } 9 } 10} 11 12// ... 13 14@NgModule({ 15 // ... 16 imports: [ 17 JwtModule.forRoot({ 18 jwtOptionsProvider: { 19 provide: JWT_OPTIONS, 20 useFactory: jwtOptionsFactory, 21 deps: [Storage] 22 } 23 }) 24 ] 25})
Configuration Options
JwtHelperService: service
This service contains helper functions:
isTokenExpired (old tokenNotExpired function)
import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}
ngOnInit() {
console.log(this.jwtHelper.isTokenExpired()); // true or false
}
getTokenExpirationDate
import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}
ngOnInit() {
console.log(this.jwtHelper.getTokenExpirationDate()); // date
}
decodeToken
import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}
ngOnInit() {
console.log(this.jwtHelper.decodeToken(token)); // token
}
What is Auth0?
Auth0 helps you to:
- Add authentication with multiple authentication sources, either social like Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, among others, or enterprise identity systems like Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider.
- Add authentication through more traditional username/password databases.
- Add support for linking different user accounts with the same user.
- Support for generating signed Json Web Tokens to call your APIs and flow the user identity securely.
- Analytics of how, when and where users are logging in.
- Pull data from other sources and add it to the user profile, through JavaScript rules.
Create a free Auth0 account
- Go to Auth0 and click Sign Up.
- Use Google, GitHub or Microsoft Account to login.
Issue Reporting
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Author
License
This project is licensed under the MIT license. See the LICENSE file for more info.

No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/auth0/.github/.github/SECURITY.md:1
- Info: Found linked content: github.com/auth0/.github/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/auth0/.github/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/auth0/.github/.github/SECURITY.md:1
Reason
SAST tool detected
Details
- Info: SAST configuration detected: Snyk
- Warn: 0 commits out of 29 are checked with a SAST tool
Reason
Found 13/14 approved changesets -- score normalized to 9
Reason
dependency not pinned by hash detected -- score normalized to 7
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/angular2-jwt/build.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/angular2-jwt/build.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npm-release.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/angular2-jwt/npm-release.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/semgrep.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/angular2-jwt/semgrep.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/snyk.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/auth0/angular2-jwt/snyk.yml/main?enable=pin
- Info: 0 out of 5 GitHub-owned GitHubAction dependencies pinned
- Info: 2 out of 2 third-party GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
branch protection is not maximal on development and all release branches
Details
- Info: 'allow deletion' disabled on branch 'main'
- Info: 'force pushes' disabled on branch 'main'
- Warn: 'branch protection settings apply to administrators' is disabled on branch 'main'
- Info: 'stale review dismissal' is required to merge on branch 'main'
- Warn: required approving review count is 1 on branch 'main'
- Info: codeowner review is required on branch 'main'
- Info: 'last push approval' is required to merge on branch 'main'
- Warn: no status checks found to merge onto branch 'main'
- Info: PRs are required in order to make changes on branch 'main'
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: topLevel 'contents' permission set to 'read': .github/workflows/build.yml:14
- Warn: no topLevel permission defined: .github/workflows/npm-release.yml:1
- Warn: topLevel 'contents' permission set to 'write': .github/workflows/release.yml:10
- Warn: no topLevel permission defined: .github/workflows/semgrep.yml:1
- Info: topLevel 'contents' permission set to 'read': .github/workflows/snyk.yml:17
- Info: no jobLevel write permissions found
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
31 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-67mh-4wv8-2f99
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-76p7-773f-r4q5
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-776f-qx25-q3cc
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
5.6
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to angular-jwt-async
node-express-mongodb-jwt-rest-api-skeleton
Node.js express.js MongoDB JWT REST API - This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API)
@lambrioanpm/omnis-debitis-iste
[](https://gitter.im/json-schema-form/angular-schema-form?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [ extracted as a standalone module for the browser and node.**