6

我想在成功联合 Google 登录或用户登录状态后将 isAuthenticated:boolean 设置为 True。

import {Injectable} from '@angular/core';
import {AngularFire} from 'angularfire2';
import {Observable} from 'rxjs/Observable';


//this current code is not making sense. Any suggestion 
//to make it an Observable for Signin and Logout?
@Injectable()
export class AuthService {
  isAuthenticated:boolean = false;
  authObj: Observable<any>;

  constructor(public af:AngularFire) {
    this.authObj = af.auth;
  }

  signin() {
    //I want to make AngularFire2 Authentication token/obj an Observable. So this will keep emitting an Observable
    //that return True.... after successful Federated Google Signin. I want my other component to subscribe to this Observable.
    return this.authObj.do(val => this.af.auth.login());
    this.isAuthenticated = true;
  }

}
4

3 回答 3

5

我写了一篇文章,将零碎的片段连接在一起。您可能必须像这样创建身份验证服务:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/first';
import { Observable } from 'rxjs/Observable';

import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AngularFire } from 'angularfire2';

@Injectable()
export class AuthGuard implements CanActivate{
  public allowed: boolean;

  constructor(private af: AngularFire, private router: Router) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    return this.af.auth.map((auth) =>  {
      if(auth == null) {
        this.router.navigate(['/login']);
        return false;
      } else {
        return true;
      }
    }).first()
  }
}

查看AngularFire2 身份验证文章了解更多信息。

于 2016-09-27T16:54:45.753 回答
3

只需订阅 auth 对象即可在有用户对象时收到通知...

  var auth = this.af.auth.subscribe( (user) => {
    debugger;
    if (user) {
      // User signed in!
      var uid = user.uid;
      console.log(uid)
    } else {
      // User logged out
      console.log("no user")
    }
  });
于 2016-07-10T06:13:07.327 回答
0

使用Angular v6,你可以做这样的事情Firebase v5.0.*angularfire2 5 rc 10

...
import { AngularFireAuth } from 'angularfire2/auth';
import { map } from 'rxjs/operators';

...
export class DashboardGuard implements CanActivate, CanActivateChild {

  constructor(private router: Router, private auth: AngularFireAuth) { }
  // #2
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    // #1
    return this.auth.authState.pipe(
      // #3
      map((user: any) => user !== null) // <--- [1]
    );

  }

  canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    return this.canActivate(route, state);
  }

}

直接使用AngularFireAuth'authState将其向下传递并检查 user 是否不等于 null [1]

  1. authState返回Observable<firebase.User>
  2. canActivate()返回Observable<boolean> | Promise<boolean> | boolean
  3. 如果未通过身份验证,订阅authState将发出null,否则发出firebase.User(见下面的屏幕截图)。

我刚刚发现我现在可以使用最新版本执行此操作-> return an Observable<boolean>in canActivate... 因为在最新版本中,尽管在文档中它说canActivate返回Observable<boolean>它会给我带来大量错误。但现在是的!

顺便说一句,我只想分享我在 Angular v6 之前是如何编写路由保护的。使用承诺

constructor(private router: Router, private afAuth: AngularFireAuth) {}

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  const promise = new Promise(
    (resolve, reject) => {
      this.afAuth.authState.subscribe((state: any) => {
        resolve(state !== null);
      });
    }
  );

  return promise.then((data: boolean) => {
    if (data) {
      return data;
    } else {
      this.router.navigate(['/']);
    }
  });
}

在此处输入图像描述

于 2018-06-15T15:35:57.697 回答