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..04fde8c1 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: isPlatformBrowser(this.platformId) ? window.location.href : null, 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' }, @@ -369,8 +372,9 @@ 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) { (Object).assign(baseHeaders, { @@ -422,13 +426,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(); 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 +}