3

I am using spring boot micro service with angular application. And I am using UAA as authorization server. If session expires means the application should show the message in pop up and should redirect to login page. How to achieve this in angular?

Can any one provide solution in this?

Thanks and Regards

Shilpa Kulkarni

user8030367
  • 81
  • 4
  • 18

2 Answers2

2

You can use an angular http-interceptor to intercept all your requests. When your token or session expires http responses will be 401(unauthorized). Based on that you can redirect user to the login route. See the documentation for HttpInterceptor.

Something like this.

export class YourInterceptor implements HttpInterceptor {
  constructor() {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(request).do((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {
        // do stuff with response if you want
      }
    }, (err: any) => {
      if (err instanceof HttpErrorResponse) {
        if (err.status === 401) {
          // redirect to the login route
          // or show a modal
        }
      }
    });
  }
}

Hope this helps.

Anuradha Gunasekara
  • 6,553
  • 2
  • 27
  • 37
  • Thank you for the reply. If session expired means in the console it is trying to redirect to login page but the cross-origin request is blocking . I am getting the following error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8081/uaa/oauth/authorize?client_id=client&redirect_uri=http://localhost:8088/login&response_type=code&state=4klB9W. (Reason: CORS preflight channel did not succeed). How to resolve this? – user8030367 May 09 '18 at 06:05
  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8081/uaa/oauth/authorize?client_id=client&redirect_uri=http://localhost:8088/login&response_type=code&state=4klB9W. (Reason: CORS preflight channel did not succeed). – user8030367 May 09 '18 at 06:09
  • You should enable CORS from your back end. For now add [this](https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en) plugin to chrome to enable CORS and test your frontend code. – Anuradha Gunasekara May 09 '18 at 06:15
  • @shilpa can you share your code here that how you are checking session expiration, i am bit confused with above code – Soumya B. Athani May 09 '18 at 09:42
  • How can we show modal if there is 'n' number of requests intercept ? – Sharath Nayak Oct 01 '21 at 06:17
-2

You need to make a check on HTTP requests, for example in case of the session(token) expire and the user is trying to hit some HTTP calls from client side then the server must return some relevant status code let's say it's 401.

So, in that case, you will check if the server is responding with status code 401 then show popup and redirect to log in screen.

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215