0
I have an Angular 2 application that is consuming an API in Nodejs. This application has JWT authentication. My problem is this: When the Node server is restarted, the user is still able to navigate between pages, but does not see what is being requested for the API.
This is because when the user connects to the system, I store his token in the application’s localStorage and create a session in the Node. Even if the server is offline, the variable in localStorage will still be present.
Here’s my canActivate
(protecting all routes and redirecting if necessary):
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const currentUser = this.authenticationService.currentUserValue;
if (currentUser) {
// authorised so return true
if (currentUser.permissions.basic_permissions === true && currentUser.permissions.owner === false) {
this.router.navigate(["/demo"]);
}
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(["/login"], { queryParams: { returnUrl: state.url } });
return false;
}
Authentication service by logging in:
login(username: string, password: string) {
return this.http
.post<any>(
`${this.APIEndpoint}/login`,
{ username, password },
{ withCredentials: true }
)
.pipe(
map(user => {
if (user && user.userInfo.token) {
localStorage.setItem("currentUser", JSON.stringify(user.userInfo));
this.currentUserSubject.next(user.userInfo);
this.loggedIn = true;
}
return user.userInfo;
})
);
}
Do you have access to your backend ? Do you know or have any warning somewhere where you are notified that the crash occurred ? If the answer is yes, part of that principle, look where some kind of Logg is stored, now if the fall is for server reasons where you do not know whether or not the fall occurred, it will be necessary to implement a verification logic, from time to time, or at certain times, the detail is how will make this check, I would try something like using the ping on the server address, if false answer, performs appropriate procedures
– Eduardo Gonçalves
Yes, I have access to the back-end. However, I would like to do this on the Front. I believe I am more productive. I thought about leaving the front with a function from time to time checking the server, however, I believe that it would not be nice to create a timer just for this
– Matheus Bernardi
Unfortunately in this scenario, it’s the only way to get what you want
– Eduardo Gonçalves
Actually I disagree, one of the ways I was able to do it was from a server socket. Once the socket drops, most likely the server is out.
– Matheus Bernardi