angular2-jwt-session is a helper library for working with JWTs in your Angular 2 applications. Also it can be used to implement "Keep me logged in feature" with the help of local storage and session storage.
- What is this Library for?
- Key Features
- How to install?
- Configurations Required
- Sending Authenticated Requests
- Configuration Options
- Checking Authentication to Hide/Show Elements and Handle Routing
- Keep me logged in
- Issue Reporting
- Author
- License
angular2-jwt-session is a small and unopinionated library that is useful for automatically attaching a JSON Web Token (JWT) as an Authorization
header when making HTTP requests from an Angular 2 app. It also has a number of helper methods that are useful for doing things like decoding JWTs.
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 session storage or in a cookie if successful.
The library comes with several helpers that are useful in your Angular 2 apps.
AuthHttp
- allows for individual and explicit authenticated HTTP requeststokenNotExpired
- allows you to check whether there is a non-expired JWT in local storage or session storage. This can be used for conditionally showing/hiding elements and stopping navigation to certain routes if the user isn't authenticated
This library not only attaches a JWT but also can be easily used to implement "Keep me logged in", "Stay signed in" or "Remember me" feature in your web app.
- Send a JWT on a per-request basis using the explicit
AuthHttp
class - Decode a JWT from your Angular 2 app
- Check the expiration date of the JWT
- Conditionally allow route navigation based on JWT status
- Implement "Keep me logged in" feature
npm install angular2-jwt-session --save
Create a new auth.module.ts
file with the following code:
import { NgModule } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig } from 'angular2-jwt-session';
export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig(), http, options);
}
@NgModule({
providers: [
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions]
}
]
})
export class AuthModule {}
We added a factory function to use as a provider for AuthHttp
. This will allow you to configure angular2-jwt-session in the AuthConfig
instance later on.
If you wish to only send a JWT on a specific HTTP request, you can use the AuthHttp
class. This class is a wrapper for Angular 2's Http
and thus supports all the same HTTP methods.
import { AuthHttp } from 'angular2-jwt-session';
// ...
class App {
someThing: string;
constructor(public authHttp: AuthHttp) {}
getSomething() {
this.authHttp.get('http://example.com/api/something')
.subscribe(
data => this.someThing = data,
err => console.log(err),
() => console.log('Request Complete')
);
}
}
AUTH_PROVIDERS
gives a default configuration setup:
- Header Name:
Authorization
- Header Prefix:
Bearer
- Token Name:
token
- Token Getter Function:
(() => localStorage.getItem(tokenName) or sessionStorage.getItem(tokenName))
- Supress error and continue with regular HTTP request if no JWT is saved:
false
- Global Headers: none
If you wish to configure the headerName
, headerPrefix
, tokenName
, tokenGetter
function, noTokenScheme
, globalHeaders
, or noJwtError
boolean, you can using provideAuth
or the factory pattern (see below).
By default, if there is no valid JWT saved, AuthHttp
will return an Observable error
with 'Invalid JWT'. If you would like to continue with an unauthenticated request instead, you can set noJwtError
to true
.
The default scheme for the Authorization
header is Bearer
, but you may either provide your own by specifying a headerPrefix
, or you may remove the prefix altogether by setting noTokenScheme
to true
.
You may set as many global headers as you like by passing an array of header-shaped objects to globalHeaders
.
You may customize any of the above options using a factory which returns an AuthHttp
instance with the options you would like to change.
import { NgModule } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig } from 'angular2-jwt-session';
export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig({
tokenName: 'token',
tokenGetter: (() => sessionStorage.getItem('token')),
globalHeaders: [{'Content-Type':'application/json'}],
}), http, options);
}
@NgModule({
providers: [
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions]
}
]
})
export class AuthModule {}
You may also send custom headers on a per-request basis with your authHttp
request by passing them in an options object.
getSomeThing() {
let myHeader = new Headers();
myHeader.append('Content-Type', 'application/json');
this.authHttp.get('http://example.com/api/something', { headers: myHeader })
.subscribe(
data => this.someThing = data,
err => console.log(error),
() => console.log('Request Complete')
);
// Pass it after the body in a POST request
this.authHttp.post('http://example.com/api/something', 'post body', { headers: myHeader })
.subscribe(
data => this.someThing = data,
err => console.log(err),
() => console.log('Request Complete')
);
}
If you wish to use the JWT as an observable stream, you can call tokenStream
from AuthHttp
.
tokenSubscription() {
this.authHttp.tokenStream.subscribe(
data => console.log(data),
err => console.log(err),
() => console.log('Complete')
);
}
This can be useful for cases where you want to make HTTP requests out of observable streams. The tokenStream
can be mapped and combined with other streams at will.
The JwtHelper
class has several useful methods that can be utilized in your components:
decodeToken
getTokenExpirationDate
isTokenExpired
You can use these methods by passing in the token to be evaluated.
jwtHelper: JwtHelper = new JwtHelper();
useJwtHelper() {
var token = localStorage.getItem('token');
console.log(
this.jwtHelper.decodeToken(token),
this.jwtHelper.getTokenExpirationDate(token),
this.jwtHelper.isTokenExpired(token)
);
}
The tokenNotExpired
function can be used to check whether a JWT exists in local storage or session storage, and if it does, whether it has expired or not. If the token is valid, tokenNotExpired
returns true
, otherwise it returns false
.
Note:
tokenNotExpired
will by default assume the token name istoken
unless a token name is passed to it, ex:tokenNotExpired('token_name')
. This will be changed in a future release to automatically use the token name that is set inAuthConfig
.
// auth.service.ts
import { tokenNotExpired } from 'angular2-jwt-session';
loggedIn() {
return tokenNotExpired();
}
The loggedIn
method can now be used in views to conditionally hide and show elements.
<button id="login" *ngIf="!authenticate.loggedIn()">Log In</button>
<button id="logout" *ngIf="authenticate.loggedIn()">Log Out</button>
To guard routes that should be limited to authenticated users, set up an AuthGuard
.
// auth-guard.service.ts
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { CanActivate } from '@angular/router';
import { Auth } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authenticate: Auth, private router: Router) {}
canActivate() {
if(this.authenticate.loggedIn()) {
return true;
} else {
this.router.navigate(['unauthorized']);
return false;
}
}
}
With the guard in place, you can use it in your route configuration.
import { AuthGuard } from './auth.guard';
export const routes: RouterConfig = [
{ path: 'admin', component: AdminComponent, canActivate: [AuthGuard] },
{ path: 'unauthorized', component: UnauthorizedComponent }
];
Once you become aware of the working of this library and how to use it, it becomes very simple to implement the "Keep me logged in" or "stay signed in" feature in your web app.
For this, I assume you have done all the above configurations and have generated JWTs. To implement this feature, you need to add a checkbox to your login page and model it with a boolean "rememberMe" which is by default false. On checking the checkbox this value changes to true and on unchecking the checkbox it becomes false. This boolean is to be stored in local storage when login button is clicked.
Now comes the logic, when the login button is pressed and rememberMe is true (i.e. checkbox is checked) the JWT is stored in the local storage using localStorage.setItem(JWT)
. And if the login button is pressed and rememberMe is false (i.e. checkbox is not checked or unchecked) the JWT is stored in the sessionStorage using sessionStorage.setItem(JWT)
.
All the other authentications, comparisons during naviagtion or during sending requests are handled by JWTHelper, AuthHttp and AuthConfig.
The "Keep me logged in" feature becomes very simple to implement using this library.
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.
Kumar Ratnam Pandey (Software Developer at Geekyants)
This project is licensed under the MIT license.