Hmm, will be good if we can see some attempt codes. Especially to where you would like to redirect? and after what condition if any?
In any case, this is how I redirect unauthorized users.
*EDITED: As mentioned in the comment. I use the AngularAuthGuard to direct the unauthorized user to the auth page, in the auth component I grab the previous URL by subscribing to the router events and use RXJS filter operation to just get previous.
Well depends on how your logic is. This works fine for me. I bet there is something better. Might be more of a workaround. Cheer~
Redirect Unauthorized Users
import {
AngularFireAuthGuard,
redirectUnauthorizedTo,
} from '@angular/fire/auth-guard';
const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['loginPage']);
const routes: Routes = [
{
path: 'members',
component: InstructorComponent,
canActivate: [AngularFireAuthGuard],
data: { authGuardPipe: redirectUnauthorizedToLogin },
},
]
Auth component page
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
previousUrl: string;
currentUrl: string;
constructor(private router: Router) {}
ngOnInit(): void {
this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => {
this.previousUrl = this.currentUrl;
this.currentUrl = event.url;
console.log(this.previousUrl);
});
}
reDirectToPreviousURL() {
this.router.navigate([this.previousUrl]);
}