i want to integrate angular2-jwt in my project : https://github.com/auth0/angular2-jwt
when i try to call the function tokenNotExpired i get this exception :
Exception: Call to Node module failed with error: ReferenceError: localStorage is not defined at Object.tokenNotExpired
this is my code :
auth.service.ts
import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
@Injectable()
export class Auth {
loggedIn() {
return tokenNotExpired();
}
}
app.component.ts
import { Component } from '@angular/core';
import { Auth } from '../.././services/auth.service';
@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private auth: Auth) { }
}
app.component.html
<div class='container-fluid'>
<div class='row'>
<div *ngIf="auth.loggedIn()" class='col-sm-3'>
<nav-menu></nav-menu>
</div>
<div class='col-sm-9 body-content'>
<router-outlet></router-outlet>
</div>
</div>
</div>
Thanks
I found a solution. the problem was that angular-universal execute the code in client and server side. and in server side 'window' object doesn't exist.
to prevents the code from running on the server-side :
loggedIn() {
if (typeof window !== 'undefined') {
return tokenNotExpired();
}
}