From 39010370636fbcfb9d9662eb7a280bec3a7e9ed0 Mon Sep 17 00:00:00 2001 From: Dana Gutride Date: Fri, 3 Mar 2017 15:14:21 -0500 Subject: [PATCH] feat(refactorauth): remove routing from auth service and dispatch necessary events instead --- login.module.ts | 7 ++-- src/app/auth/authentication.service.ts | 56 ++++++++------------------ src/app/user/user.service.ts | 2 +- 3 files changed, 21 insertions(+), 44 deletions(-) diff --git a/login.module.ts b/login.module.ts index 250bf56..463fd69 100644 --- a/login.module.ts +++ b/login.module.ts @@ -1,12 +1,11 @@ import { NgModule } from '@angular/core'; -import { Http, HttpModule } from "@angular/http"; +import { HttpModule } from '@angular/http'; import { AlmUserName } from './src/app/user/alm-user-name.pipe'; @NgModule({ imports: [ - Http, - HttpModule, + HttpModule ], declarations: [ AlmUserName @@ -30,4 +29,4 @@ export class LoginModule { // ] // }; // } -} \ No newline at end of file +} diff --git a/src/app/auth/authentication.service.ts b/src/app/auth/authentication.service.ts index b73d6cf..cf31102 100644 --- a/src/app/auth/authentication.service.ts +++ b/src/app/auth/authentication.service.ts @@ -1,4 +1,3 @@ -import { Router } from '@angular/router'; import { Injectable, Inject } from '@angular/core'; import { Http, Response, Headers } from '@angular/http'; @@ -8,52 +7,42 @@ import { AUTH_API_URL } from '../shared/auth-api'; @Injectable() export class AuthenticationService { - private authToken: string = ''; private refreshInterval: number; private apiUrl: string; private clearTimeoutId: any; - constructor(private router: Router, - private broadcaster: Broadcaster, + constructor(private broadcaster: Broadcaster, @Inject(AUTH_API_URL) apiUrl: string, private http: Http) { this.apiUrl = apiUrl; } - isLoggedIn(): boolean { - let token = localStorage.getItem('auth_token'); - if (token) { - this.authToken = token; - // refresh the token in five seconds to make sure we have expiry and a running timer - only do this first time in - if(!this.refreshInterval) { - this.setupRefreshTimer(15); - } - return true; - } - let params: any = this.getUrlParams(); - if ('token_json' in params) { - let tokenJson = decodeURIComponent(params['token_json']); - let token = this.processTokenResponse(JSON.parse(tokenJson)); - this.setupRefreshTimer(token.expires_in); - return true; - } - return false; + logIn(tokenParameter: string): boolean { + let tokenJson = decodeURIComponent(tokenParameter); + let token = this.processTokenResponse(JSON.parse(tokenJson)); + this.setupRefreshTimer(token.expires_in); + this.broadcaster.broadcast('loggedin', 1); + return true; } - logout(redirect: boolean = false) { - this.authToken = ''; + logout() { localStorage.removeItem('auth_token'); localStorage.removeItem('refresh_token'); clearTimeout(this.clearTimeoutId); this.refreshInterval = null; this.broadcaster.broadcast('logout', 1); - if (redirect) { - this.router.navigate(['login']); + } + + isLoggedIn(): boolean { + let token = localStorage.getItem('auth_token'); + if (token) { + return true; } + return false; } getToken() { - if (this.isLoggedIn()) return this.authToken; + if (this.isLoggedIn()) return localStorage.getItem('auth_token'); } setupRefreshTimer(refreshInSeconds: number) { @@ -86,20 +75,9 @@ export class AuthenticationService { } } - getUrlParams(): Object { - let query = window.location.search.substr(1); - let result: any = {}; - query.split('&').forEach(function (part) { - let item: any = part.split('='); - result[item[0]] = decodeURIComponent(item[1]); - }); - return result; - } - processTokenResponse(response: any): Token { let token = response as Token; - this.authToken = token.access_token; - localStorage.setItem('auth_token', this.authToken); + localStorage.setItem('auth_token', token.access_token); localStorage.setItem('refresh_token', token.refresh_token); return token; } diff --git a/src/app/user/user.service.ts b/src/app/user/user.service.ts index 679dfae..637e3b7 100644 --- a/src/app/user/user.service.ts +++ b/src/app/user/user.service.ts @@ -77,7 +77,7 @@ export class UserService { }) .catch ((e) => { if (e.status === 401) { - this.auth.logout(true); + this.auth.logout(); } else { this.handleError(e); }