Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/angular2-token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface GlobalOptions {
export interface Angular2TokenOptions {
apiBase?: string;
apiPath?: string;
hostUrl?: string;

signInPath?: string;
signInRedirect?: string;
Expand Down Expand Up @@ -92,4 +93,4 @@ export interface Angular2TokenOptions {
oAuthWindowOptions?: { [key:string]: string; };

globalOptions?: GlobalOptions;
}
}
31 changes: 18 additions & 13 deletions src/angular2-token.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -103,6 +105,7 @@ export class Angular2TokenService implements CanActivate {
let defaultOptions: Angular2TokenOptions = {
apiPath: null,
apiBase: null,
hostUrl: null,

signInPath: 'auth/sign_in',
signInRedirect: null,
Expand All @@ -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'
},
Expand Down Expand Up @@ -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) {
(<any>Object).assign(baseHeaders, {
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node",
Expand Down Expand Up @@ -33,4 +33,4 @@
"angularCompilerOptions": {
"genDir": "../lib"
}
}
}