Angular 6: Get Token from localStorage After Refresh












0















EDIT: I update the code with the answer that PierreDuc gave me. He's solution works for me. I edit the code so everyone could see the working code



I'm using canActivate and canActivateChild to know if someone have token for my web and I want to block anyone who don't have token already.
I'm using localStorage for the user data and token.



When I try to login with user id:1, the guard navigate to page not found, but after I refresh the page I gain access to the page.
When I used debugger I saw that before I refreshed the localStorage was empty but had values after the refresh.



Why this is happening? how can I fix it?
This is the relevant code:



app-routing.module.ts



const appRoutes: Routes = [
{ path: 'login/:id', canActivate: [AuthGuard], children: },
{ path: '', canActivateChild: [AuthGuard], children: [
{ path: '', redirectTo: '/courses', pathMatch: 'full' },
{ path: 'courses', component: CourseListComponent, pathMatch: 'full'},
{ path: 'courses/:courseId', component: CourseDetailComponent, pathMatch: 'full' },
{ path: 'courses/:courseId/unit/:unitId', component: CoursePlayComponent,
children: [
{ path: '', component: CourseListComponent },
{ path: 'lesson/:lessonId', component: CourseLessonComponent, data:{ type: 'lesson'} },
{ path: 'quiz/:quizId', component: CourseQuizComponent, data: {type: 'quiz'} }
]}
]},
{ path: '**', component: PageNotFoundComponent, pathMatch: 'full' }];


auth.guard.ts



export class AuthGuard implements CanActivate , CanActivateChild {

constructor(private authUserService: AuthUserService, private router: Router) { }

canActivate(route: ActivatedRouteSnapshot, state:
RouterStateSnapshot): boolean |
Observable<boolean> | Promise<boolean> {
// save the id from route snapshot
const id = +route.params.id;

// if you try to logging with id
if (id) {
this.router.navigate(["/courses"]);
return this.authUserService.login(id);
}

// if you already logged and just navigate between pages
else if (this.authUserService.isLoggedIn())
return true;

else {
this.router.navigate(["/page_not_found"]);
return false;
}
}

canActivateChild(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean |
Observable<boolean> | Promise<boolean> {
return this.canActivate(route, state);
}

}


auth-user.service.ts



import { Injectable, OnDestroy } from '@angular/core';
import { Observable , BehaviorSubject } from 'rxjs';
import { LocalStorage } from '@ngx-pwa/local-storage';
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
import { catchError, groupBy } from 'rxjs/operators';

import { UserService } from './user.service';
import { IUser } from './user';

@Injectable()
export class AuthUserService implements OnDestroy {

private user: IUser;
private errorMessage: string;

constructor(private userService: UserService) { }

// store the session and call http get
login(id: number) {
return this.userService.getUser(id).pipe(
map((user) => {
this.user = user;
localStorage.setItem('user', JSON.stringify(this.user));
localStorage.setItem('token', 'JWT');
return true;
}),
catchError((error) => {
this.errorMessage = <any>error;
return of(false);
})
);
}

isLoggedIn() {
return !!localStorage.getItem('token');
}

ngOnDestroy() {
localStorage.removeItem('user');
localStorage.removeItem('token');
}
}









share|improve this question





























    0















    EDIT: I update the code with the answer that PierreDuc gave me. He's solution works for me. I edit the code so everyone could see the working code



    I'm using canActivate and canActivateChild to know if someone have token for my web and I want to block anyone who don't have token already.
    I'm using localStorage for the user data and token.



    When I try to login with user id:1, the guard navigate to page not found, but after I refresh the page I gain access to the page.
    When I used debugger I saw that before I refreshed the localStorage was empty but had values after the refresh.



    Why this is happening? how can I fix it?
    This is the relevant code:



    app-routing.module.ts



    const appRoutes: Routes = [
    { path: 'login/:id', canActivate: [AuthGuard], children: },
    { path: '', canActivateChild: [AuthGuard], children: [
    { path: '', redirectTo: '/courses', pathMatch: 'full' },
    { path: 'courses', component: CourseListComponent, pathMatch: 'full'},
    { path: 'courses/:courseId', component: CourseDetailComponent, pathMatch: 'full' },
    { path: 'courses/:courseId/unit/:unitId', component: CoursePlayComponent,
    children: [
    { path: '', component: CourseListComponent },
    { path: 'lesson/:lessonId', component: CourseLessonComponent, data:{ type: 'lesson'} },
    { path: 'quiz/:quizId', component: CourseQuizComponent, data: {type: 'quiz'} }
    ]}
    ]},
    { path: '**', component: PageNotFoundComponent, pathMatch: 'full' }];


    auth.guard.ts



    export class AuthGuard implements CanActivate , CanActivateChild {

    constructor(private authUserService: AuthUserService, private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state:
    RouterStateSnapshot): boolean |
    Observable<boolean> | Promise<boolean> {
    // save the id from route snapshot
    const id = +route.params.id;

    // if you try to logging with id
    if (id) {
    this.router.navigate(["/courses"]);
    return this.authUserService.login(id);
    }

    // if you already logged and just navigate between pages
    else if (this.authUserService.isLoggedIn())
    return true;

    else {
    this.router.navigate(["/page_not_found"]);
    return false;
    }
    }

    canActivateChild(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean |
    Observable<boolean> | Promise<boolean> {
    return this.canActivate(route, state);
    }

    }


    auth-user.service.ts



    import { Injectable, OnDestroy } from '@angular/core';
    import { Observable , BehaviorSubject } from 'rxjs';
    import { LocalStorage } from '@ngx-pwa/local-storage';
    import { of } from 'rxjs';
    import { map } from 'rxjs/operators';
    import { catchError, groupBy } from 'rxjs/operators';

    import { UserService } from './user.service';
    import { IUser } from './user';

    @Injectable()
    export class AuthUserService implements OnDestroy {

    private user: IUser;
    private errorMessage: string;

    constructor(private userService: UserService) { }

    // store the session and call http get
    login(id: number) {
    return this.userService.getUser(id).pipe(
    map((user) => {
    this.user = user;
    localStorage.setItem('user', JSON.stringify(this.user));
    localStorage.setItem('token', 'JWT');
    return true;
    }),
    catchError((error) => {
    this.errorMessage = <any>error;
    return of(false);
    })
    );
    }

    isLoggedIn() {
    return !!localStorage.getItem('token');
    }

    ngOnDestroy() {
    localStorage.removeItem('user');
    localStorage.removeItem('token');
    }
    }









    share|improve this question



























      0












      0








      0








      EDIT: I update the code with the answer that PierreDuc gave me. He's solution works for me. I edit the code so everyone could see the working code



      I'm using canActivate and canActivateChild to know if someone have token for my web and I want to block anyone who don't have token already.
      I'm using localStorage for the user data and token.



      When I try to login with user id:1, the guard navigate to page not found, but after I refresh the page I gain access to the page.
      When I used debugger I saw that before I refreshed the localStorage was empty but had values after the refresh.



      Why this is happening? how can I fix it?
      This is the relevant code:



      app-routing.module.ts



      const appRoutes: Routes = [
      { path: 'login/:id', canActivate: [AuthGuard], children: },
      { path: '', canActivateChild: [AuthGuard], children: [
      { path: '', redirectTo: '/courses', pathMatch: 'full' },
      { path: 'courses', component: CourseListComponent, pathMatch: 'full'},
      { path: 'courses/:courseId', component: CourseDetailComponent, pathMatch: 'full' },
      { path: 'courses/:courseId/unit/:unitId', component: CoursePlayComponent,
      children: [
      { path: '', component: CourseListComponent },
      { path: 'lesson/:lessonId', component: CourseLessonComponent, data:{ type: 'lesson'} },
      { path: 'quiz/:quizId', component: CourseQuizComponent, data: {type: 'quiz'} }
      ]}
      ]},
      { path: '**', component: PageNotFoundComponent, pathMatch: 'full' }];


      auth.guard.ts



      export class AuthGuard implements CanActivate , CanActivateChild {

      constructor(private authUserService: AuthUserService, private router: Router) { }

      canActivate(route: ActivatedRouteSnapshot, state:
      RouterStateSnapshot): boolean |
      Observable<boolean> | Promise<boolean> {
      // save the id from route snapshot
      const id = +route.params.id;

      // if you try to logging with id
      if (id) {
      this.router.navigate(["/courses"]);
      return this.authUserService.login(id);
      }

      // if you already logged and just navigate between pages
      else if (this.authUserService.isLoggedIn())
      return true;

      else {
      this.router.navigate(["/page_not_found"]);
      return false;
      }
      }

      canActivateChild(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean |
      Observable<boolean> | Promise<boolean> {
      return this.canActivate(route, state);
      }

      }


      auth-user.service.ts



      import { Injectable, OnDestroy } from '@angular/core';
      import { Observable , BehaviorSubject } from 'rxjs';
      import { LocalStorage } from '@ngx-pwa/local-storage';
      import { of } from 'rxjs';
      import { map } from 'rxjs/operators';
      import { catchError, groupBy } from 'rxjs/operators';

      import { UserService } from './user.service';
      import { IUser } from './user';

      @Injectable()
      export class AuthUserService implements OnDestroy {

      private user: IUser;
      private errorMessage: string;

      constructor(private userService: UserService) { }

      // store the session and call http get
      login(id: number) {
      return this.userService.getUser(id).pipe(
      map((user) => {
      this.user = user;
      localStorage.setItem('user', JSON.stringify(this.user));
      localStorage.setItem('token', 'JWT');
      return true;
      }),
      catchError((error) => {
      this.errorMessage = <any>error;
      return of(false);
      })
      );
      }

      isLoggedIn() {
      return !!localStorage.getItem('token');
      }

      ngOnDestroy() {
      localStorage.removeItem('user');
      localStorage.removeItem('token');
      }
      }









      share|improve this question
















      EDIT: I update the code with the answer that PierreDuc gave me. He's solution works for me. I edit the code so everyone could see the working code



      I'm using canActivate and canActivateChild to know if someone have token for my web and I want to block anyone who don't have token already.
      I'm using localStorage for the user data and token.



      When I try to login with user id:1, the guard navigate to page not found, but after I refresh the page I gain access to the page.
      When I used debugger I saw that before I refreshed the localStorage was empty but had values after the refresh.



      Why this is happening? how can I fix it?
      This is the relevant code:



      app-routing.module.ts



      const appRoutes: Routes = [
      { path: 'login/:id', canActivate: [AuthGuard], children: },
      { path: '', canActivateChild: [AuthGuard], children: [
      { path: '', redirectTo: '/courses', pathMatch: 'full' },
      { path: 'courses', component: CourseListComponent, pathMatch: 'full'},
      { path: 'courses/:courseId', component: CourseDetailComponent, pathMatch: 'full' },
      { path: 'courses/:courseId/unit/:unitId', component: CoursePlayComponent,
      children: [
      { path: '', component: CourseListComponent },
      { path: 'lesson/:lessonId', component: CourseLessonComponent, data:{ type: 'lesson'} },
      { path: 'quiz/:quizId', component: CourseQuizComponent, data: {type: 'quiz'} }
      ]}
      ]},
      { path: '**', component: PageNotFoundComponent, pathMatch: 'full' }];


      auth.guard.ts



      export class AuthGuard implements CanActivate , CanActivateChild {

      constructor(private authUserService: AuthUserService, private router: Router) { }

      canActivate(route: ActivatedRouteSnapshot, state:
      RouterStateSnapshot): boolean |
      Observable<boolean> | Promise<boolean> {
      // save the id from route snapshot
      const id = +route.params.id;

      // if you try to logging with id
      if (id) {
      this.router.navigate(["/courses"]);
      return this.authUserService.login(id);
      }

      // if you already logged and just navigate between pages
      else if (this.authUserService.isLoggedIn())
      return true;

      else {
      this.router.navigate(["/page_not_found"]);
      return false;
      }
      }

      canActivateChild(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean |
      Observable<boolean> | Promise<boolean> {
      return this.canActivate(route, state);
      }

      }


      auth-user.service.ts



      import { Injectable, OnDestroy } from '@angular/core';
      import { Observable , BehaviorSubject } from 'rxjs';
      import { LocalStorage } from '@ngx-pwa/local-storage';
      import { of } from 'rxjs';
      import { map } from 'rxjs/operators';
      import { catchError, groupBy } from 'rxjs/operators';

      import { UserService } from './user.service';
      import { IUser } from './user';

      @Injectable()
      export class AuthUserService implements OnDestroy {

      private user: IUser;
      private errorMessage: string;

      constructor(private userService: UserService) { }

      // store the session and call http get
      login(id: number) {
      return this.userService.getUser(id).pipe(
      map((user) => {
      this.user = user;
      localStorage.setItem('user', JSON.stringify(this.user));
      localStorage.setItem('token', 'JWT');
      return true;
      }),
      catchError((error) => {
      this.errorMessage = <any>error;
      return of(false);
      })
      );
      }

      isLoggedIn() {
      return !!localStorage.getItem('token');
      }

      ngOnDestroy() {
      localStorage.removeItem('user');
      localStorage.removeItem('token');
      }
      }






      angular typescript authentication jwt local-storage






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:32







      Ofir Sasson

















      asked Nov 13 '18 at 13:44









      Ofir SassonOfir Sasson

      107115




      107115
























          1 Answer
          1






          active

          oldest

          votes


















          3














          The userService.getUser is async, it won't be finished and will set your localStorage after you already checked it.



          Change your authService to this:



          login(id: number) {
          return this.userService.getUser(id).pipe(
          map((user) => {
          this.user = user;
          localStorage.setItem('user', JSON.stringify(this.user));
          localStorage.setItem('token', 'JWT');
          return true;
          }),
          catchError((error) => {
          this.errorMessage = <any>error;
          return of(false);
          })
          );
          }


          And update your guard to this:



          if (id) {
          this.router.navigate(["/courses"]);
          return this.authUserService.login(id);
          }





          share|improve this answer


























          • I did as you said and now I get this error when I try to go for localhost:4200/login/1 => ERROR Error: Uncaught (in promise): ReferenceError: of is not defined

            – Ofir Sasson
            Nov 13 '18 at 13:55






          • 1





            of is a operator from rxjs, you have to import it using import { of } from 'rxjs'; Same for the catchError and the map. CatchError has to return an observable

            – PierreDuc
            Nov 13 '18 at 14:00






          • 1





            map updates the value emitted by the observable. Subscribe triggers the cold observable.. Which is not what you want there, but only when angular checks the guard

            – PierreDuc
            Nov 13 '18 at 14:11






          • 1





            Yes.. you have to import it from rxjs/operators

            – PierreDuc
            Nov 13 '18 at 14:15






          • 1





            ah yes, I misread your routes. You use the same guard for your login/:id path as well. You have to add the redirect then indeed. Sorry about that. I'll update my answer

            – PierreDuc
            Nov 13 '18 at 14:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282395%2fangular-6-get-token-from-localstorage-after-refresh%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          The userService.getUser is async, it won't be finished and will set your localStorage after you already checked it.



          Change your authService to this:



          login(id: number) {
          return this.userService.getUser(id).pipe(
          map((user) => {
          this.user = user;
          localStorage.setItem('user', JSON.stringify(this.user));
          localStorage.setItem('token', 'JWT');
          return true;
          }),
          catchError((error) => {
          this.errorMessage = <any>error;
          return of(false);
          })
          );
          }


          And update your guard to this:



          if (id) {
          this.router.navigate(["/courses"]);
          return this.authUserService.login(id);
          }





          share|improve this answer


























          • I did as you said and now I get this error when I try to go for localhost:4200/login/1 => ERROR Error: Uncaught (in promise): ReferenceError: of is not defined

            – Ofir Sasson
            Nov 13 '18 at 13:55






          • 1





            of is a operator from rxjs, you have to import it using import { of } from 'rxjs'; Same for the catchError and the map. CatchError has to return an observable

            – PierreDuc
            Nov 13 '18 at 14:00






          • 1





            map updates the value emitted by the observable. Subscribe triggers the cold observable.. Which is not what you want there, but only when angular checks the guard

            – PierreDuc
            Nov 13 '18 at 14:11






          • 1





            Yes.. you have to import it from rxjs/operators

            – PierreDuc
            Nov 13 '18 at 14:15






          • 1





            ah yes, I misread your routes. You use the same guard for your login/:id path as well. You have to add the redirect then indeed. Sorry about that. I'll update my answer

            – PierreDuc
            Nov 13 '18 at 14:26
















          3














          The userService.getUser is async, it won't be finished and will set your localStorage after you already checked it.



          Change your authService to this:



          login(id: number) {
          return this.userService.getUser(id).pipe(
          map((user) => {
          this.user = user;
          localStorage.setItem('user', JSON.stringify(this.user));
          localStorage.setItem('token', 'JWT');
          return true;
          }),
          catchError((error) => {
          this.errorMessage = <any>error;
          return of(false);
          })
          );
          }


          And update your guard to this:



          if (id) {
          this.router.navigate(["/courses"]);
          return this.authUserService.login(id);
          }





          share|improve this answer


























          • I did as you said and now I get this error when I try to go for localhost:4200/login/1 => ERROR Error: Uncaught (in promise): ReferenceError: of is not defined

            – Ofir Sasson
            Nov 13 '18 at 13:55






          • 1





            of is a operator from rxjs, you have to import it using import { of } from 'rxjs'; Same for the catchError and the map. CatchError has to return an observable

            – PierreDuc
            Nov 13 '18 at 14:00






          • 1





            map updates the value emitted by the observable. Subscribe triggers the cold observable.. Which is not what you want there, but only when angular checks the guard

            – PierreDuc
            Nov 13 '18 at 14:11






          • 1





            Yes.. you have to import it from rxjs/operators

            – PierreDuc
            Nov 13 '18 at 14:15






          • 1





            ah yes, I misread your routes. You use the same guard for your login/:id path as well. You have to add the redirect then indeed. Sorry about that. I'll update my answer

            – PierreDuc
            Nov 13 '18 at 14:26














          3












          3








          3







          The userService.getUser is async, it won't be finished and will set your localStorage after you already checked it.



          Change your authService to this:



          login(id: number) {
          return this.userService.getUser(id).pipe(
          map((user) => {
          this.user = user;
          localStorage.setItem('user', JSON.stringify(this.user));
          localStorage.setItem('token', 'JWT');
          return true;
          }),
          catchError((error) => {
          this.errorMessage = <any>error;
          return of(false);
          })
          );
          }


          And update your guard to this:



          if (id) {
          this.router.navigate(["/courses"]);
          return this.authUserService.login(id);
          }





          share|improve this answer















          The userService.getUser is async, it won't be finished and will set your localStorage after you already checked it.



          Change your authService to this:



          login(id: number) {
          return this.userService.getUser(id).pipe(
          map((user) => {
          this.user = user;
          localStorage.setItem('user', JSON.stringify(this.user));
          localStorage.setItem('token', 'JWT');
          return true;
          }),
          catchError((error) => {
          this.errorMessage = <any>error;
          return of(false);
          })
          );
          }


          And update your guard to this:



          if (id) {
          this.router.navigate(["/courses"]);
          return this.authUserService.login(id);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 14:27

























          answered Nov 13 '18 at 13:51









          PierreDucPierreDuc

          29.2k45376




          29.2k45376













          • I did as you said and now I get this error when I try to go for localhost:4200/login/1 => ERROR Error: Uncaught (in promise): ReferenceError: of is not defined

            – Ofir Sasson
            Nov 13 '18 at 13:55






          • 1





            of is a operator from rxjs, you have to import it using import { of } from 'rxjs'; Same for the catchError and the map. CatchError has to return an observable

            – PierreDuc
            Nov 13 '18 at 14:00






          • 1





            map updates the value emitted by the observable. Subscribe triggers the cold observable.. Which is not what you want there, but only when angular checks the guard

            – PierreDuc
            Nov 13 '18 at 14:11






          • 1





            Yes.. you have to import it from rxjs/operators

            – PierreDuc
            Nov 13 '18 at 14:15






          • 1





            ah yes, I misread your routes. You use the same guard for your login/:id path as well. You have to add the redirect then indeed. Sorry about that. I'll update my answer

            – PierreDuc
            Nov 13 '18 at 14:26



















          • I did as you said and now I get this error when I try to go for localhost:4200/login/1 => ERROR Error: Uncaught (in promise): ReferenceError: of is not defined

            – Ofir Sasson
            Nov 13 '18 at 13:55






          • 1





            of is a operator from rxjs, you have to import it using import { of } from 'rxjs'; Same for the catchError and the map. CatchError has to return an observable

            – PierreDuc
            Nov 13 '18 at 14:00






          • 1





            map updates the value emitted by the observable. Subscribe triggers the cold observable.. Which is not what you want there, but only when angular checks the guard

            – PierreDuc
            Nov 13 '18 at 14:11






          • 1





            Yes.. you have to import it from rxjs/operators

            – PierreDuc
            Nov 13 '18 at 14:15






          • 1





            ah yes, I misread your routes. You use the same guard for your login/:id path as well. You have to add the redirect then indeed. Sorry about that. I'll update my answer

            – PierreDuc
            Nov 13 '18 at 14:26

















          I did as you said and now I get this error when I try to go for localhost:4200/login/1 => ERROR Error: Uncaught (in promise): ReferenceError: of is not defined

          – Ofir Sasson
          Nov 13 '18 at 13:55





          I did as you said and now I get this error when I try to go for localhost:4200/login/1 => ERROR Error: Uncaught (in promise): ReferenceError: of is not defined

          – Ofir Sasson
          Nov 13 '18 at 13:55




          1




          1





          of is a operator from rxjs, you have to import it using import { of } from 'rxjs'; Same for the catchError and the map. CatchError has to return an observable

          – PierreDuc
          Nov 13 '18 at 14:00





          of is a operator from rxjs, you have to import it using import { of } from 'rxjs'; Same for the catchError and the map. CatchError has to return an observable

          – PierreDuc
          Nov 13 '18 at 14:00




          1




          1





          map updates the value emitted by the observable. Subscribe triggers the cold observable.. Which is not what you want there, but only when angular checks the guard

          – PierreDuc
          Nov 13 '18 at 14:11





          map updates the value emitted by the observable. Subscribe triggers the cold observable.. Which is not what you want there, but only when angular checks the guard

          – PierreDuc
          Nov 13 '18 at 14:11




          1




          1





          Yes.. you have to import it from rxjs/operators

          – PierreDuc
          Nov 13 '18 at 14:15





          Yes.. you have to import it from rxjs/operators

          – PierreDuc
          Nov 13 '18 at 14:15




          1




          1





          ah yes, I misread your routes. You use the same guard for your login/:id path as well. You have to add the redirect then indeed. Sorry about that. I'll update my answer

          – PierreDuc
          Nov 13 '18 at 14:26





          ah yes, I misread your routes. You use the same guard for your login/:id path as well. You have to add the redirect then indeed. Sorry about that. I'll update my answer

          – PierreDuc
          Nov 13 '18 at 14:26


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282395%2fangular-6-get-token-from-localstorage-after-refresh%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python