1

I'm having issues with my application, I used php-jwt JSON Web Token Authentication in my angular 9 apps and the token works fine when I'm not redirecting to successful page after login. when redirecting to the dashboard after login the token is set and immediately remove from localStorage. How can I allow token even after redirection to new page? Any help will be highly appreciated.

My auth.service.ts file

 // Sign-in
  signIn(user: Usermodule) {
    return this.http
      .post<any>(`${this.endpoint}/signin.php`, user)
      .subscribe((res: any) => {
        localStorage.setItem('ACCESS_TOKEN', res.jwt);
        this.getUserProfile(res.id).subscribe((res) => {
          
             this.router.navigate(['app/dashboard']);
         
         
        });

      });
  }

  // User profile
  getUserProfile(id): Observable<any> {
    let api = `${this.endpoint}/user_profile.php`;
    return this.http.get(api, { headers: this.headers }).pipe(
      map((res: Response) => {
        return res || {};
      }),
      catchError(this.handleError)
    );
  }

auth.interceptors.ts file

export class AuthInterceptor implements HttpInterceptor {
  constructor(private authService: AuthService) {}

  intercept(request: HttpRequest<any>, next: HttpHandler) {
    const access_Token = this.authService.getToken();

    request = request.clone({
        setHeaders: {
          Authorization: 'Bearer ' + access_Token,
        },
      });
  

    return next.handle(request);
  }
}

app.module.ts file

   JwtModule.forRoot({
      config: {
        tokenGetter: () => {
          return localStorage.getItem('ACCESS_TOKEN');
        }
        // whitelistedDomains: ['localhost'],
        // blacklistedRoutes: ['localhost/auth/login']
      }
    })
  ],
  providers: [
    AuthService,
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi:true}
  ],
  bootstrap: [AppComponent]

Once I comment out //this.router.navigate(['app/dashboard']); the token stays in localstorage without been killed and I can even access restricted area when I type the address manually.

enter image description here

enter image description here

Iamgisnet
  • 171
  • 3
  • 3
  • 12
  • 1
    Does this answer your question? [Response.redirect kills local storage?](https://stackoverflow.com/questions/12489999/response-redirect-kills-local-storage) - Otherwise need to see some code. – ficuscr Jul 16 '20 at 20:33
  • @ficuscr No. isn't working for me. thank for responding anyway. – Iamgisnet Jul 16 '20 at 20:52
  • Need more to go on then... https://stackoverflow.com/help/minimal-reproducible-example Any change to protocol or URI with the redirect? Is it actually aded then removed or is issue with persistence? – ficuscr Jul 16 '20 at 21:15
  • 1
    @ficuscr I just edited the question, yes it will be added then remove on page redirection. – Iamgisnet Jul 17 '20 at 04:20
  • I's be curious to see the URL you redirect to. Same port? Look at answer [here](https://stackoverflow.com/questions/1492942/is-localstorage-in-firefox-only-working-when-the-page-is-online) from Andrzej Doyle. – ficuscr Jul 17 '20 at 19:10
  • @ficuscr http://localhost:4200/app once successful logged in the page will be redirected to http://localhost:4200/app/dashboard – Iamgisnet Jul 17 '20 at 19:15

0 Answers0