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

6
  • 1
    Does this answer your question? Response.redirect kills local storage? - Otherwise need to see some code. Commented Jul 16, 2020 at 20:33
  • @ficuscr No. isn't working for me. thank for responding anyway. Commented Jul 16, 2020 at 20:52
  • Need more to go on then... 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? Commented Jul 16, 2020 at 21:15
  • 1
    @ficuscr I just edited the question, yes it will be added then remove on page redirection. Commented Jul 17, 2020 at 4:20
  • I's be curious to see the URL you redirect to. Same port? Look at answer here from Andrzej Doyle. Commented Jul 17, 2020 at 19:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.