I've got a structure for redirection after log in in which we are sent to a page which tells the user to log in, after log in a service tries to redirect the user to the page they were trying to visit before the redirection. I keep the page visited in an observable. The redirection happens after the assignment of new tokens and refresh tokens
It looks like this
Authguard Service:
async canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
console.log("ENTERING THE GUARD")
if (!this.storage.getToken() || !this.storage.isLoggedInfunc()) {
console.log("doesn't have token")
if (this.storage.getToken()) {
const refreshing = await this.auth.tryRefreshingTokens();
if (refreshing && this.storage.isLoggedInfunc()) {
return true;
}
else {
this.storage.setValueLastPage(this.router.url);
this.router.navigate(['needlogin']);
console.log("Returning from inside 1");
return false;
}
}
else {
this.storage.setValueLastPage(this.router.url);
this.router.navigate(['needlogin']);
console.log("Returning from inside 2");
return false;
}
}
console.log(this.router.url)
debugger
console.log("Gets into the true part")
console.log(state.url);
return true;
} }
Storage Service:
public saveUser(user: any): void {
this.getValueLastPage().subscribe(value => {
this.LastPageThis = value;
});
this.cookieService.delete(JWT_TOKEN)
this.cookieService.set(JWT_TOKEN, user.token);
delete user.token;
window.localStorage.removeItem(REFRESH_TOKEN);
window.localStorage.setItem(REFRESH_TOKEN, user.refreshToken);
delete user.refreshToken;
window.localStorage.setItem(USER_KEY, JSON.stringify(user));
this.setValueLoggedIn(true);
console.log(this.router.url)
if (this.router.url.includes("/needlogin") && this.LastPageThis !== null) {
console.log(this.LastPageThis);
this.navigateback(this.LastPageThis);
} else {
this.router.navigate(['/'])
}
}
The problem is it sends me to the needlogin page instead of say '', but when I check the url on the state in the authguard '' appears.
Thank you very much for your help.