import { Injectable } from '@angular/core';
import {
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
} from '@angular/router';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import Parse from 'parse';

@Injectable({
  providedIn: 'root',
})
export class LoginAuthGuard implements CanActivate {
  LoginPage = 'tabs';

  constructor(public router: Router) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    // 当前路由url
    // let url: string = state.url;
    return this.checkLogin();
  }
  checkLogin(): boolean {
    // 如果已登录,直接跳转首页
    let currentUser = Parse.User.current();
    if (currentUser?.id) {
      // 否则重定向到login页面
      this.router.navigate([this.LoginPage]);
      return false;
    }
    return true;
  }
}
export { CanActivate };
