Skip to content

Instantly share code, notes, and snippets.

@nartc
Created August 9, 2018 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nartc/9ebcc2fa55dfa5a2e52bbc3c346ebdc3 to your computer and use it in GitHub Desktop.
Save nartc/9ebcc2fa55dfa5a2e52bbc3c346ebdc3 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { JwtHelperService } from '@auth0/angular-jwt';
import { interval } from 'rxjs/internal/observable/interval';
import { of } from 'rxjs/internal/observable/of';
import { map, startWith, switchMap } from 'rxjs/operators';
import { Observable } from 'rxjs/Rx';
import { LoginResponseVm, LoginVm, SecurityClient } from '../app.api';
import { LocalStorageService } from './local-storage.service';
@Injectable({
providedIn: 'root',
})
export class SecurityService {
jwtHelper: JwtHelperService;
token: string;
loginResult: LoginResponseVm;
private bufferMinuteMilli = 1000 * 60;
private bufferMinute = 5;
constructor(
private _securityApiClient: SecurityClient,
private _localStorageService: LocalStorageService,
) {
this.jwtHelper = new JwtHelperService();
}
login(loginVm: LoginVm): Observable<LoginResponseVm> {
return this._securityApiClient.login(loginVm);
}
isTokenExpired(token: string): boolean {
return this.jwtHelper.isTokenExpired(token);
}
saveLocalLogin(data: LoginResponseVm) {
this._localStorageService.setObject('loginResult', data);
this._localStorageService.set('token', data.token);
}
checkLocalLogin(): boolean {
const result: LoginResponseVm = this._localStorageService.getObject('loginResult');
if (result && result.user) {
this.token = result.token;
if (this.isTokenExpired(this.token)) {
this.clearLocalLogin();
return false;
}
this.loginResult = result;
return true;
}
return false;
}
startRefreshCheck(): Observable<LoginResponseVm> {
const result: LoginResponseVm = this._localStorageService.getObject('loginResult');
this.token = this._localStorageService.get('token');
if (!result || !result.user || !this.token || this.isTokenExpired(this.token)) {
return of();
}
let refreshInterval = interval(this.bufferMinuteMilli * this.bufferMinute);
if (this.tokenShouldRefresh(this.token)) {
refreshInterval = refreshInterval.pipe(startWith(0));
}
return refreshInterval.pipe(
switchMap((i: number) => {
if (!this.tokenShouldRefresh(this.token)) {
return of();
}
return this._securityApiClient.refreshToken().pipe(
map((data: LoginResponseVm) => {
this.saveLocalLogin(data);
return data;
}),
);
}),
);
}
private tokenShouldRefresh(token: string): boolean {
return this.jwtHelper.isTokenExpired(token, 300);
}
clearLocalLogin() {
this._localStorageService.remove('loginResult');
this._localStorageService.remove('token');
this._localStorageService.setObject('loginResult', null);
this._localStorageService.set('token', null);
this.loginResult = null;
this.token = null;
this._localStorageService.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment