All files / src/app/shared/login/auth auth.guard.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 231x 1x 1x         1x 2x 2x       2x 1x   1x 1x 1x        
import { Injectable } from "@angular/core";
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from "@angular/router";
import { LoginService } from "../login.service";
 
@Injectable({
    providedIn: "root",
})
export class AuthGuard implements CanActivate {
    constructor (private loginService: LoginService, private router: Router) {
        this.loginService.init();
    }
 
    canActivate (_next: ActivatedRouteSnapshot, snap: RouterStateSnapshot): boolean {
        if (this.loginService.isLoggedIn()) {
            return true;
        } else {
            sessionStorage.setItem("afterlogin-redirect", snap.url);
            this.router.navigate(["/login"]);
            return false;
        }
    }
}