I'm working on an application with reactive forms where user fills up data and goes to next step by clicking Next button.
I've got a following problem to solve in my application: user can't navigate through website by manually changing url. If he/she tries, the expected outcome should be:
Pop up informing that all data will be lost and user will be turned back to the first page.
After user changes url and accepts the lost of data by clicking "Yes" user should start on the first page.
- The only time when the user should stay on the same page should be after refreshing the page.
I tried firstly do it in with guards, but then I couldn't find a way to tell Angular that the guard should only work if user changes url manually. The guard also worked while navigating using button Next - in other words the Next button didn't allow me to go to next step anymore.
Then I implemented something like this:
export class DirectAccessGuard implements CanActivate {
constructor(private router: Router ) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this.router.url === '/') {
this.router.navigate(['']);
return false;
}
return true;
}
}
and added this guard to each of my paths and it worked quite well until I realize this solution isn't perfect either because it redirects me to the first page after refreshing the page. Also tried CanDeactivate but I guess it isn't what I need here. Any ideas?