I am using Angular 2.1.2.
I have an authentication token (using angular2-jwt) and if it expires my webApi call fails with a 401 error. I am looking for a solution where the user will not lose any input data.
I can catch this 401 and open a modal with the login. The user then logs in, the modal goes away, and they see their input screen. However, the failed requests are showing errors so I need to reprocess the requests. If it was a router navigate the initial data has not loaded.
I could reload the page, but if I router.navigate to the same page it does not seem to actually reload the page. I don't want to do a full page reload on the single page app. Is there a way to force router.navigate to run, even if it's the current page?
Re-navigating is still a problem because I would lose any new input data that has not saved.
Ideally, the request would just 'pause' until the user logs in from the modal. I have not found a way to implement this.
Any ideas? Is there a best practice?
Use a browser session:
https://developer.mozilla.org/de/docs/Web/API/Window/sessionStorage
Store the data as json string and then recreate form data if request fails.
After I do a login and receive a token (that in my case expires in 60 minutes), I set a interval that checks every minute to see if 59 minutes have passed. If yes, then I open up a login dialog.
The idea, however, is the login dialog is not a route, but just an overlay that opens on top of whatever screen the user happens to be in on (thus I put the login component within the html of the app root component, and use an observable to call up the login dialog from anywhere in the app).
When the user correctly re-logins in, I close the login dialog and the user goes merrily on their way with whatever screen they were using before they had to re-login.
I don't know if this is a "best practice" but it works in the situation you are describing. Let me know and I can put up code.
Well, reloading is simple:
(<any>window).location.reload(true);
It is good idea to show login/password popup and allow user to continue working if he can provide password, if user hits cancel just do redirect to login page.
There are also connection problems, timeouts, server errors. The problem is that it is difficult to reliably implement proper handling using Angular 2
Http
module andrxjs
. Personally, i gave up usingHttp
module, because its error handling has bad design and implemented my own http module, which allows configurable error handling and transparent retries.Usually I would provide a
HttpService
myself instead of usingHttp
directly. So with your requirement, I can provide my ownget()
method to chain the authentication before sending any real HTTP requests.Here is the service:
Here is the component to call the service:
By chaining observables, the
Authentication
service determines whether to interrupt the normal flow to show the modal (though currently only lives with the App component, it can certainly be implemented separately). And once a positive answer is received from the dialog, the service can resume the flow.I have a full live example here.
http://plnkr.co/edit/C129guNJvri5hbGZGsHp?open=app%2Fapp.component.ts&p=preview
Did you try to switch your button by using
tokenNotExpired
attribute like in this example : https://github.com/auth0/angular2-jwt#checking-authentication-to-hideshow-elements-and-handle-routingIt allows you to prevent the 401...