使用Angular 7在Firebase中合并电子邮件和电话身份验证

时间:2019-07-03 01:16:24

标签: javascript angular typescript firebase firebase-authentication

我想将uid合并为一个,以便在firebase中通过电子邮件和电话身份验证来识别用户。这两种方法都有效,并且通过firebase数据库注册时,电话将其与用户关联。我知道存在'SignInWithCredential()'方法,该方法可以通过验证ID解决此问题,但是我无法使用Angular在我的代码中提出解决方案。这是我的代码的一些屏幕截图,谢谢。

Firebase用户: firebase users

auth.service.ts上的SigIn方法

  loginUser(email: string, password: string): Promise<any> {
    return firebase.auth().signInWithEmailAndPassword(email, password);
  }

  verifySMS(phoneNumberString: string, appVerifier: firebase.auth.RecaptchaVerifier): Promise<any> {
    return firebase.auth().signInWithPhoneNumber(phoneNumberString, appVerifier);
  };

  signupUser(usuario: user): Promise<any> {
    return firebase
      .auth()
      .createUserWithEmailAndPassword(usuario.email, usuario.password)
      .then(newUser => {

        newUser.sendEmailVerification()

        if (usuario.email = 'admin@mail.com') {
          this.afDB.database.ref('userProfile/' + newUser.uid).set({
            'name' : usuario.fullName,
            'phone' : usuario.phone,
            'profile' : this.adminProfile,
            'userSettings' : this.userSettings
          });
        } else {
          this.afDB.database.ref('userProfile/' + newUser.uid).set({
            'name' : usuario.fullName,
            'phone' : usuario.phone,
            'profile' : this.defaultProfile,
            'userSettings' : this.userSettings
          });
        } 

      })
      .catch(error => {
        console.error(error);
        throw new Error(error);
      });
  }

最终登录方式的电子邮件,密码,电话

sendVerifySMS() {

    var appVerifier = this.windowRef.recaptchaVerifier;

    this._profile.getUserProfile().on('value', profile => {

      this.phone = profile.val().phone;

      firebase.auth().signInWithPhoneNumber(this.phone, appVerifier).then(res => {

        console.log('mensaje enviado');

        this.windowRef.confirmationResult = res;
        console.log(this.windowRef.confirmationResult);
        this.verificationID = res.verificationId;

    })
    .catch( error => console.log(error) );

    })

 }

  logInPhone(phoneCodeForm : FormGroup) {

    let SMScode : string = phoneCodeForm.value.code;

    //this._auth.verifyCodeSMS(SMScode);

    this.windowRef.confirmationResult.confirm(SMScode).then(result => {

      console.log(result);

      const signInCredential = firebase.auth.PhoneAuthProvider.credential(this.verificationID, SMScode)

        var prevUser = this._profile.currentUser;
        // Sign in user with another account
        firebase.auth().signInWithCredential(signInCredential).then(function(user) {
          console.log("Sign In Success", user);
          var currentUser = user;
          // Merge prevUser and currentUser data stored in Firebase.
          // Note: How you handle this is specific to your application

          // After data is migrated delete the duplicate user
          return user.delete().then(function() {
            // Link the OAuth Credential to original account
            return prevUser.linkWithCredential(signInCredential);

          }).then(function() {
            // Sign in with the newly linked credential
            return firebase.auth().signInWithCredential(signInCredential);
          });

        }).catch(function(error) {
          console.log("Sign In Error", error);

          this._snackBar.open(error.message, 'OK', {
            duration: 3000,
          });

        });

      this._auth.afDB.database.ref('userProfile/' + this._profile.currentUser.uid).update({
        'lastLogin' : new Date()
      });

      this._router.navigateByUrl('apps/dashboards/analytics');

    })
      .catch( error => this._snackBar.open(error.message, 'OK', {
          duration: 3000,
        })
      );
    }

0 个答案:

没有答案
相关问题