avatar
Unveiling the Secrets of AI

Guest 145 26th Dec, 2024

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AngularFireAuth } from '@angular/fire/auth';
import firebase from 'firebase/app';
import { first } from 'rxjs/operators';

@Injectable()
export class AdminGuard implements CanActivate {

    constructor(private router: Router, public auth: AngularFireAuth) { }

    isLoggedIn() {
      return this.auth.authState.pipe(first()).toPromise();
    }

    async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
      if (await this.isLoggedIn()) {
        return true;
      }
      // this.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
      this.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()).then(()=>{
        this.router.navigateByUrl(state.url);
      });
      return false;
    }
}
Markup
Description

No description

To share this paste please copy this url and send to your friends
RAW Paste Data