Gathering detailed insights and metrics for angular-jwt-async
Gathering detailed insights and metrics for angular-jwt-async
Gathering detailed insights and metrics for angular-jwt-async
Gathering detailed insights and metrics for 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.**
npm install angular-jwt-async
Typescript
Module System
Node Version
NPM Version
66
Supply Chain
95.4
Quality
75
Maintenance
100
Vulnerability
98.9
License
TypeScript (93.24%)
JavaScript (5.92%)
HTML (0.66%)
SCSS (0.18%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
612
Last Day
1
Last Week
2
Last Month
13
Last Year
61
MIT License
2,632 Stars
529 Commits
485 Forks
87 Watchers
11 Branches
103 Contributors
Updated on Feb 13, 2025
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
1
1
31
@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.
1# installation with npm 2npm install @auth0/angular-jwt 3 4# installation with yarn 5yarn add @auth0/angular-jwt
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);
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}
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});
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.
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})
JwtHelperService: service
This service contains helper functions:
import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}
ngOnInit() {
console.log(this.jwtHelper.isTokenExpired()); // true or false
}
import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}
ngOnInit() {
console.log(this.jwtHelper.getTokenExpirationDate()); // date
}
import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}
ngOnInit() {
console.log(this.jwtHelper.decodeToken(token)); // token
}
Auth0 helps you to:
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.
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
Reason
security policy file detected
Details
Reason
SAST tool detected
Details
Reason
Found 13/14 approved changesets -- score normalized to 9
Reason
dependency not pinned by hash detected -- score normalized to 7
Details
Reason
branch protection is not maximal on development and all release branches
Details
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
Reason
project is not fuzzed
Details
Reason
31 existing vulnerabilities detected
Details
Score
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 More