angular - angular2-jwt token alwas not valid -


so have simple angular 2/laravel app wit jwt authentification support. have service verifies everytime route called if jwt token valid or not using angular2-jwt tokennotexpired() function, function return false reason, user redirected login page.

so goes user logs in, token generated backend , saved on local storage, service check if token valid before initiating route using canactivate lifecycle hook.

here did far:

login component:

   ...             this.http.post(server_url + 'auth', body, {                 headers: headers             }             ).subscribe(                 data => {                      localstorage.setitem('auth_token', data.json().token);                     this.authhttp.get(server_url + 'auth/user', headers)                         .subscribe(                         data => {                             this.store.dispatch({ type: set_current_user_profile, payload: data.json().user });                             localstorage.setitem('user', data.json().user);                             this.router.navigate(['/home']);                          },                         err => console.log('fehlermeldung: ' + err)                         );                  }, ... 

app.module :

...  { provide: authconfig, usevalue: new authconfig({               headername: 'authorization',               headerprefix: 'bearer ',               tokenname: 'auth_token',               tokengetter: (() => localstorage.getitem('auth_token')),               globalheaders: [{ 'content-type': 'application/json' }],               nojwterror: true,               notokenscheme: true   })},     authhttp ... 

auth.service : // check jwt token service

import { tokennotexpired } 'angular2-jwt'; import { injectable } '@angular/core';  @injectable() export class authservice  {      loggedin() {     return tokennotexpired();     }  } 

auth.guard.service :

// check if token of user still valid import { injectable } '@angular/core'; import { router } '@angular/router'; import { canactivate } '@angular/router'; import { authservice } './auth.service'; import { appstate } '../shared/interfaces'; import { set_current_user_profile } '../shared/state.actions';    import { store } '@ngrx/store'  @injectable() export class authguardservice implements canactivate {    constructor(private auth: authservice, private router: router, private store: store<appstate>) {}    canactivate() {     if(this.auth.loggedin()) {       return true;     } else {         console.log ('token expired or not valid')         localstorage.setitem('auth_token', '');         localstorage.setitem('user', '');         this.store.dispatch({ type: set_current_user_profile, payload: null });         this.router.navigate(['/']);       return false;     }   } } 

app.routing :

const routes: routes = [   { path: 'home', component: homecomponent},   { path: 'about', component: aboutcomponent, canactivate: [authguardservice]},   { path: 'profile/:id', component: profilecomponent, canactivate: [authguardservice]},   { path: '', component: logincomponent} ]; 

edit: backend side ok since token generated , stored in localstorage after user logs in.

solution: how fixed may have same problem, used jwthelper function istokenexpired() instead of tokennotexpired , inversed logic in service , worked not sure why though

auth.service looks :

@injectable() export class authservice  {      private jwthelper: jwthelper = new jwthelper();     private token = localstorage.getitem('auth_token');      isexpired() {         return this.jwthelper.istokenexpired(this.token);     }  } 

and auth.guard.service canactivate():

  canactivate() {     if (this.auth.isexpired()) {         console.log (this.auth.isexpired());         localstorage.setitem('auth_token', '');         localstorage.setitem('user', '');         this.store.dispatch({ type: set_current_user_profile, payload: null });         this.router.navigate(['/']);       return false;     } else {       return true;     }   } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -