From 7f0abff6ae626f535c916e5249222691214b4061 Mon Sep 17 00:00:00 2001 From: Serra Allgood Date: Mon, 2 Oct 2017 11:24:24 -0400 Subject: [PATCH 1/5] Change module to commonjs --- src/tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tsconfig.json b/src/tsconfig.json index 5a56436d..48f2b951 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "es5", - "module": "es2015", + "module": "commonjs", "emitDecoratorMetadata": true, "experimentalDecorators": true, "moduleResolution": "node", @@ -33,4 +33,4 @@ "angularCompilerOptions": { "genDir": "../lib" } -} \ No newline at end of file +} From 9d8f495e8bf3c587eaa24f55778d5cafd1299098 Mon Sep 17 00:00:00 2001 From: Serra Allgood Date: Mon, 2 Oct 2017 20:19:09 -0400 Subject: [PATCH 2/5] Check isPlatformBrowser before using browser apis --- src/angular2-token.model.ts | 3 ++- src/angular2-token.service.ts | 17 ++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/angular2-token.model.ts b/src/angular2-token.model.ts index 16fc2438..f06597b0 100644 --- a/src/angular2-token.model.ts +++ b/src/angular2-token.model.ts @@ -65,6 +65,7 @@ export interface GlobalOptions { export interface Angular2TokenOptions { apiBase?: string; apiPath?: string; + hostUrl?: string; signInPath?: string; signInRedirect?: string; @@ -92,4 +93,4 @@ export interface Angular2TokenOptions { oAuthWindowOptions?: { [key:string]: string; }; globalOptions?: GlobalOptions; -} \ No newline at end of file +} diff --git a/src/angular2-token.service.ts b/src/angular2-token.service.ts index ecd65a14..ba2cf241 100644 --- a/src/angular2-token.service.ts +++ b/src/angular2-token.service.ts @@ -1,5 +1,6 @@ -import { Injectable, Optional } from '@angular/core'; +import { Inject, Injectable, Optional, PLATFORM_ID } from '@angular/core'; import { ActivatedRoute, Router, CanActivate } from '@angular/router'; +import { isPlatformBrowser } from '@angular/common'; import { Http, Response, @@ -70,7 +71,8 @@ export class Angular2TokenService implements CanActivate { constructor( private http: Http, @Optional() private activatedRoute: ActivatedRoute, - @Optional() private router: Router + @Optional() private router: Router, + @Inject(PLATFORM_ID) protected platformId: Object ) { } userSignedIn(): boolean { @@ -82,7 +84,7 @@ export class Angular2TokenService implements CanActivate { return true; else { // Store current location in storage (usefull for redirection after signing in) - if (this.atOptions.signInStoredUrlStorageKey) { + if (this.atOptions.signInStoredUrlStorageKey && isPlatformBrowser(this.platformId)) { localStorage.setItem( this.atOptions.signInStoredUrlStorageKey, window.location.pathname + window.location.search @@ -103,6 +105,7 @@ export class Angular2TokenService implements CanActivate { let defaultOptions: Angular2TokenOptions = { apiPath: null, apiBase: null, + hostUrl: null, signInPath: 'auth/sign_in', signInRedirect: null, @@ -114,16 +117,16 @@ export class Angular2TokenService implements CanActivate { registerAccountPath: 'auth', deleteAccountPath: 'auth', - registerAccountCallback: window.location.href, + registerAccountCallback: , updatePasswordPath: 'auth', resetPasswordPath: 'auth/password', - resetPasswordCallback: window.location.href, + resetPasswordCallback: isPlatformBrowser(this.platformId) ? window.location.href : null, userTypes: null, - oAuthBase: window.location.origin, + oAuthBase: isPlatformBrowser(this.platformId) ? window.location.origin : null, oAuthPaths: { github: 'auth/github' }, @@ -370,7 +373,7 @@ export class Angular2TokenService implements CanActivate { // Get auth data from local storage this.getAuthDataFromStorage(); - + // Merge auth headers to request if set if (this.atCurrentAuthData != null) { (Object).assign(baseHeaders, { From 67517db05557f2238014d43bc34a17deb39854f9 Mon Sep 17 00:00:00 2001 From: Serra Allgood Date: Mon, 2 Oct 2017 20:21:33 -0400 Subject: [PATCH 3/5] Forgot a line --- src/angular2-token.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/angular2-token.service.ts b/src/angular2-token.service.ts index ba2cf241..b31abdcb 100644 --- a/src/angular2-token.service.ts +++ b/src/angular2-token.service.ts @@ -117,7 +117,7 @@ export class Angular2TokenService implements CanActivate { registerAccountPath: 'auth', deleteAccountPath: 'auth', - registerAccountCallback: , + registerAccountCallback: isPlatformBrowser(this.platformId) ? window.location.href : null, updatePasswordPath: 'auth', From 4538f0131b075deedc42dee0681c3716d14da61c Mon Sep 17 00:00:00 2001 From: Serra Allgood Date: Mon, 2 Oct 2017 20:27:12 -0400 Subject: [PATCH 4/5] Add another conditional for isPlatformBrowser --- src/angular2-token.service.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/angular2-token.service.ts b/src/angular2-token.service.ts index b31abdcb..6cfc21fb 100644 --- a/src/angular2-token.service.ts +++ b/src/angular2-token.service.ts @@ -425,13 +425,14 @@ export class Angular2TokenService implements CanActivate { // Try to load auth data private tryLoadAuthData(): void { + if (isPlatformBrowser(this.platformId)) { + let userType = this.getUserTypeByName(localStorage.getItem('userType')); - let userType = this.getUserTypeByName(localStorage.getItem('userType')); + if (userType) + this.atCurrentUserType = userType; - if (userType) - this.atCurrentUserType = userType; - - this.getAuthDataFromStorage(); + this.getAuthDataFromStorage(); + } if(this.activatedRoute) this.getAuthDataFromParams(); From 4b64eeba9c3e73dd014d53548aa0e6ac6d4bc948 Mon Sep 17 00:00:00 2001 From: Serra Allgood Date: Mon, 2 Oct 2017 20:36:59 -0400 Subject: [PATCH 5/5] Check another function call for isPlatformBrowser --- src/angular2-token.service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/angular2-token.service.ts b/src/angular2-token.service.ts index 6cfc21fb..04fde8c1 100644 --- a/src/angular2-token.service.ts +++ b/src/angular2-token.service.ts @@ -372,7 +372,8 @@ export class Angular2TokenService implements CanActivate { let baseHeaders: { [key:string]: string; } = this.atOptions.globalOptions.headers; // Get auth data from local storage - this.getAuthDataFromStorage(); + if (isPlatformBrowser(this.platformId)) + this.getAuthDataFromStorage(); // Merge auth headers to request if set if (this.atCurrentAuthData != null) {