import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { LocalStorageService } from "ngx-webstorage";

@Injectable()
export class LoginGuard implements CanActivate {
    constructor(private router: Router,
                private localStorage: LocalStorageService,) {
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        let isLogin: boolean;
        // 判断用户是否登入
        const user = this.localStorage.retrieve('accessToken');

        if (!user) {
            isLogin = false;
            // 未登入跳转到登入界面
            this.router.navigate(['app/login']);
        } else {
            isLogin = true;
        }
        return isLogin;
    }
}
