My question is more theoretical than practical. I am curious to know how experts handle situations like the one I'm about to describe.
I have developed a Single Page Application (SPA) using Angular and Breeze with token-based authentication. I have set up an interceptor in Angular to ensure that requests include the token, and if the server returns a 401 error, the user is redirected to the login page. Breeze uses the same $http instance as Angular.
When the app initializes, it sends 1-5 requests to the server without authentication tokens, resulting in 401 errors and redirection to the login page. I plan to authenticate every user as a Visitor with limited access or a Registered User with specific permissions.
My issue arises when handling multiple 401 responses - the redirection happens unpredictably, leading to errors in the console for subsequent responses. This lack of control bothers me, and I believe there must be a better way to manage these exceptions.
It seems necessary to send an initial request to the server to verify user authentication status before proceeding with other requests. However, since Angular's $http does not support synchronous operations, I am considering implementing a service with a flag that restricts calls until authentication is confirmed. This solution feels cumbersome, though.
How do experienced developers typically address this type of situation? Are there any resources or blogs discussing similar challenges?
Any help or insights would be greatly appreciated. Thank you!