import { Injectable } from "@angular/core"; import { Router, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router"; import { AuthenticationService } from "./authentication.service"; @Injectable({ providedIn: "root" }) export class AuthGuardService { constructor(private router: Router, private authenticationService: AuthenticationService) {} public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { const currentUser = this.authenticationService.currentUserValue; if (currentUser) { // logged in so return true return true; } // not logged in so redirect to login page with the return url this.router.navigate(["/login"], {skipLocationChange: true, queryParams: { returnUrl: state.url }}); return false; } }