How to prevent multiple account for same user in Firebase?

前端 未结 2 1707
挽巷
挽巷 2020-12-20 10:19

We are developing an app using Flutter and exploring Firebase for Authentication, We have plan to provide below authentication mechanism to the end-user.

  1. Login
相关标签:
2条回答
  • 2020-12-20 10:54

    You'll need to find a way to identify that this is the same user, and then link their Phone and Facebook accounts together.

    The idiomatic way to identify the accounts as being from the same user is by their email address. That means you'll have to ask the Phone auth user to provide an email address to, and then set that email address on the account.

    0 讨论(0)
  • 2020-12-20 11:02

    I was able to solve this.

    1. Login Social account (Account_2)
    2. Try to link Mobile No
        If(Success)
           Account (Step1) updated with Mobile No
        Else
           Login with Mobile No (Account_1)
           Link Social account (Account_2) created in step1 with Account_1
    

    Sample java script code:

        <script>
      var firebaseConfig = {
        apiKey: "XXXX",
        authDomain: "XXXXXX",
        databaseURL: "XXXXXX",
        projectId: "XXXXXX",
        storageBucket: "XXXXXXXXX",
        messagingSenderId: "XXXXXXX",
        appId: "XXXXXXXX",
        measurementId: "XXXXXXXXXXXX"
      };
    
      firebase.initializeApp(firebaseConfig);
      firebase.analytics();
    
    
    var provider = new firebase.auth.FacebookAuthProvider;
    
    var phoneAuthProvider = new firebase.auth.PhoneAuthProvider;
    
    firebase.auth().useDeviceLanguage();
    
    provider.setCustomParameters({
      'display': 'popup'
    });
    
    var globalCred ;
    
    
    firebase.auth().signInWithPopup(provider).then(function(result) {
    
      var token = result.credential.accessToken;
    
      globalCred = result.credential;
    
      console.log("GLOBAL :" + globalCred);
    
      var user = result.user;
    
      var prevUser = firebase.auth().currentUser; //Logged in via FB
      
      console.log(prevUser);
    
      
      if (!user.phoneNumber) {
          // Ask user for phone number.
          var phoneNumber = window.prompt('Provide your phone number');
          // You also need to provide a button element signInButtonElement
          // which the user would click to complete sign-in.
          // Get recaptcha token. Let's use invisible recaptcha and hook to the button.
          var appVerifier = new firebase.auth.RecaptchaVerifier(
              signInButtonElement, {size: 'invisible'});
          // This will wait for the button to be clicked the reCAPTCHA resolved.
          return result.user.linkWithPhoneNumber(phoneNumber, appVerifier)
            .then(function(confirmationResult) {
              // Ask user to provide the SMS code.
              var code = window.prompt('Provide your SMS code');
              // Complete sign-in.
              return confirmationResult.confirm(code);
            })
        }
    
    }).catch(function(error) {
    
      var errorCode = error.code;
      var errorMessage = error.message;
    
      var email = error.email;
    
      var credential = error.credential;
    
      if(errorCode == 'auth/credential-already-in-use'){
          // Merge accounts      
          console.log("Trying Linking");  
    
          var prevUser = firebase.auth().currentUser;
    
          prevUser.delete().then(function() {
            console.log("FB user deleted");
          }).catch(function(error) {
            // An error happened.
            console.log("Error in FB user deletion");
          });
    
          firebase.auth().signInWithCredential(credential).then(function(result) {
            console.log("Sign In Success using Phone", result);
            var currentUser = result.user;
    
              firebase.auth().currentUser.linkWithCredential(globalCred);
    
              });
    
          }).catch(function(error) {
            // If there are errors we want to undo the data merge/deletion
            console.log("Sign In Error", error);
            //repo.set(prevUser, prevUserData);
          });
    
    
      }
    
    }); 
    
    </script>
    
    0 讨论(0)
提交回复
热议问题