Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
feat(refactorauth): remove routing from auth service and dispatch nec…
Browse files Browse the repository at this point in the history
…essary events instead
  • Loading branch information
dgutride authored and joshuawilson committed Mar 4, 2017
1 parent 1e5c3f9 commit 3901037
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 44 deletions.
7 changes: 3 additions & 4 deletions login.module.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -30,4 +29,4 @@ export class LoginModule {
// ]
// };
// }
}
}
56 changes: 17 additions & 39 deletions src/app/auth/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Router } from '@angular/router';
import { Injectable, Inject } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class UserService {
})
.catch ((e) => {
if (e.status === 401) {
this.auth.logout(true);
this.auth.logout();
} else {
this.handleError(e);
}
Expand Down

0 comments on commit 3901037

Please sign in to comment.