UNPKG

innotec-auth-plugin

Version:

The Innotec-Auth-Plugin is designed to handle all authentication processes for applications where's conntected to the innotec v2 infrastructure. Theses plugin provides different authentication processes:

120 lines (90 loc) 3.61 kB
import { Injectable, Inject, ReflectiveInjector, EventEmitter, NgZone } from '@angular/core'; import { Location } from '@angular/common'; import { Router, ActivatedRoute, Params } from '@angular/router'; import { Http, Response, URLSearchParams, Headers } from '@angular/http'; import { ModuleOptions } from '../moduleoptions/moduleoptions.service'; import { CookieService } from '../cookieService/cookie.service'; import { I18nService } from '../i18n/i18n.service'; import { SocialConnector } from '../socialConnector/socialConnector.service'; import { JwtHelper } from 'angular2-jwt'; import { BehaviorSubject, Observable } from 'rxjs'; @Injectable() export class CheckAuthStatus { authProvider: any; jwtHelper: any; paramsSub: any; code: any; return: any; loginState: boolean; isLoginSubject = new BehaviorSubject<boolean>(this.loginState); constructor(@Inject(Router) private router: Router, @Inject(ModuleOptions) private options: ModuleOptions, @Inject(CookieService) private cookie: CookieService, @Inject(SocialConnector) private social: SocialConnector, @Inject(I18nService) private i18n: I18nService, private activatedRoute: ActivatedRoute, private http: Http, private location: Location, private zone: NgZone) { let injector = ReflectiveInjector.resolveAndCreate([ JwtHelper ]); this.jwtHelper = injector.get(JwtHelper); let params = new URLSearchParams(this.location.path(false).split('?')[1]); this.code = params.get('code'); if (this.code) { let url: string = this.router.url.substring(0, this.router.url.indexOf('?')); this.router.navigateByUrl(url); } } public setLoginState(state) { this.isLoginSubject.next(state); } public setLoginCookie(token, social) { let value = JSON.stringify({ isLoggedIn: true, token, social }); this.zone.run(() => this.isLoginSubject.next(true)); this.cookie.set('InnotecApp', value, 30, this.options.get().hostname); return true; } private checkSocial(code, social): Promise<any> { let authorization = this.options.get().clientid + ':' + this.options.get().clientsecret; let headers = new Headers(); headers.set('Authorization', authorization); return this.http.post(this.options.get().endpoint + '/' + social, { languagecode: this.i18n.getLanguage(), code }, { headers }) .toPromise() .then(res => this.setLoginCookie(res.json(), social), err => console.log(err)); } public check() { let logininfo; if (this.cookie.get('InnotecApp')) { logininfo = JSON.parse(this.cookie.get('InnotecApp')); if (logininfo.isLoggedIn) { let expired = this.jwtHelper.isTokenExpired(logininfo.token.token); if (expired) { return this.social.reLogin(logininfo); } return true; } return false; } if (this.code && localStorage.getItem('return')) { let social = localStorage.getItem('return'); localStorage.removeItem('return'); this.checkSocial(this.code, social); } return false; } public logout() { this.cookie.delete('InnotecApp'); this.isLoginSubject.next(false); } public getTokenInfo() { let logininfo; if (this.cookie.get('InnotecApp')) { logininfo = JSON.parse(this.cookie.get('InnotecApp')); return this.jwtHelper.decodeToken(logininfo.token.token); } return false; } public isLoggedIn(): Observable<boolean> { return this.isLoginSubject.asObservable(); } }