import {Injectable, OnInit} from '@angular/core';
import {CanActivateChild, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Transfer} from './transfer';
import {Load} from './load';

@Injectable()
export class Auth implements CanActivateChild, OnInit {
    public login = false;

    constructor(private transfer: Transfer, private load: Load) {
    }

    ngOnInit() {
    }

    canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        if (window.localStorage.getItem('token')) {
            return true;
        } else {
            this.load.offLoad();
            this.transfer.transfer({
                url: '/login',
                query: '',
                hash: ''
            });
            return false;
        }
    }
}
