【问题标题】:Facebook provider is not authenticating users in my ionic app - using angularfireFacebook 提供商没有在我的 ionic 应用程序中对用户进行身份验证 - 使用 angularfire
【发布时间】:2020-04-03 08:41:09
【问题描述】:

我正在尝试在我的 ionic 应用中实现(社交和电子邮件密码)登录..

它在电子邮件传递中运行良好。当我尝试添加 google 和 facebook 时,我遇到了很多问题,然后是一个非常脏的代码。现在我的代码适用于 googleauthentication,但对于 facebook,应用程序正在重定向,但返回的结果是 {user: null},并且没有任何变化。

错误是什么?为什么 facebook 身份验证没有改变 this.afAuth.authState?

功能在我的 auth.service.ts 文件中实现。

Facebook登录功能如下:

  loginWithFacebook() {
    this.store.dispatch(new UIActions.StartLoading());
    alert('facebook login');
    try {
      let provider = new auth.FacebookAuthProvider();
      console.log(provider);
      const credential = this.afAuth.auth.signInWithRedirect(provider);
      console.log('accomplished sign in with redirect');
    } catch (error) {
        console.log(error);
        alert(error);
    }
  }

LoginWithGoogle(按预期工作):

webGoogleLogin() {
  try {
      const provider = new firebase.auth.GoogleAuthProvider();
      console.log(provider);
      const credential = this.afAuth.auth.signInWithRedirect(provider);
      console.log('accomplished sign in with redirect');
  } catch (error) {
    console.log(error);
    alert(error);
  }
}

我在 initAthListener() 中收听身份验证更改,该更改在应用初始化时触发(它按预期工作,用于电子邮件传递登录 - 注销):

initAuthListener() {
    // this.store.dispatch(new UIActions.StartLoading());

    this.afAuth.authState.subscribe(user => {
      // if( user && !user.emailVerified) {
      //   this.store.dispatch(new UIActions.StopLoading());
      //   this.alertService.presentToast(this.translateService.instant("Auth.PLEASE_VALIDATE_YOUR_EMAIL_ADDRESS"), 3000);
      // }
      console.log(user);
      alert(user);
      if (user) {
        if(user.emailVerified) {
        this.isAuthenticated = true;
        this.store.dispatch(new AuthActions.SetAuthenticated());
        this.afStore.collection('profiles').doc<FullUserProfile>(user.uid).valueChanges().pipe(take(1)).subscribe(
          (profile: FullUserProfile) => {
            if (profile != null) {
                this.store.dispatch(new AuthActions.SetUserProfile(profile));
                // this.functionsGeneralDataService.initialize(profile);
                this.generalDataService.initialize(profile);
                this.store.dispatch(new UIActions.StopLoading());
                // this.router.navigate(['/groups']);
                this.goToMainPage();
            } else {
              return;
            }
          }
        );
      }
     } else {
        this.isAuthenticated = false;
        this.generalDataService.unsubscribeFromAll();
        this.store.dispatch(new AuthActions.SetUnauthenticated());
        this.store.dispatch(new AuthActions.RemoveUserProfile());
        this.store.dispatch(new GroupsActions.ClearState());

        // this.router.navigate(['/login']);
      }
      this.store.dispatch(new UIActions.StopLoading());
    });
    // this.listenToRedirectResults();
  }

我尝试在 inmitAuthListener() 的末尾添加以下功能,但没有解决问题:

  listenToRedirectResults() {
    firebase.auth().getRedirectResult().then((result) => {
        console.log(result);
        console.log(firebase.auth().currentUser);
        alert("THIS IS RESULT");
          this.store.dispatch(new UIActions.StartLoading());
          if (result.credential) {
            alert('result.credentials is not null');
            var token = result.user.getIdToken();
            var user = result.user;
    // if(result == null || result.user == null) {
    //   alert('result or user null');
    //   this.isAuthenticated = false;
    //   this.generalDataService.unsubscribeFromAll();
    //   this.store.dispatch(new AuthActions.SetUnauthenticated());
    //   this.store.dispatch(new AuthActions.RemoveUserProfile());
    //   this.store.dispatch(new GroupsActions.ClearState());
    // }
            alert('will get data');
            this.store.dispatch(new AuthActions.SetAuthenticated());
            this.afStore.collection('profiles').doc(user.uid).valueChanges().pipe(take(1)).subscribe(
              (profile: FullUserProfile) => {
                this.store.dispatch(new UIActions.StartLoading());
                this.isAuthenticated = true;
                if (profile != null) {
                    this.store.dispatch(new AuthActions.SetUserProfile(profile));
                    // this.functionsGeneralDataService.initialize(profile);
                    this.generalDataService.initialize(profile);
                    this.store.dispatch(new UIActions.StopLoading());
                    this.goToMainPage();
                } else {
                  let profile = {} as FullUserProfile;
                  profile.id = user.uid;
                  profile.email = user.email;
                  profile.name = user.displayName;
                  profile.image = { name: '', url: user.photoURL}
                  profile.type = 0;
                  profile.numberOfGroupsAllowed = 2;
                  this.afStore.collection('profiles').doc(user.uid).set(profile);
                  this.store.dispatch(new AuthActions.SetUserProfile(profile));
                    // this.functionsGeneralDataService.initialize(profile);
                    this.generalDataService.initialize(profile);
                    this.store.dispatch(new UIActions.StopLoading());
                    this.goToMainPage();
                    // this.router.navigate(['/groups']);
                }
              }
            );
            // this.router.navigate(['groups'])
          } else {
            alert('no creadential');
          }
        })
        .catch(function(error) {
          console.log(error.message);
          this.store.dispatch(new UIActions.StopLoading());
          this.alertService.presentToast(error.message);
        });
  }

注意:如果用户从 firebase 控制台的用户中删除,并尝试使用 facebook 登录,则用户会添加到那里,但我的应用程序中没有更改。

对不起我的脏代码,在问这个问题之前我已经清理了很多.. 昨天,在添加google身份验证之前,facebook身份验证工作正常,但注销没有。

抱歉,我是 ionic 框架的新手。

【问题讨论】:

标签: ionic-framework angularfire2 facebook-authentication


【解决方案1】:

错误:我正在检查电子邮件是否为未验证电子邮件的 facebook 帐户验证。我已经重组了我的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-10
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    相关资源
    最近更新 更多