How to determine if the user signed in to Firebase with email and password or with google sign in?

前端 未结 4 2487
遥遥无期
遥遥无期 2021-02-10 04:30

I am working on a log in for a web application, and I have made it so the user can sign in manually with their email and password or by using a google sign in. Is there a way to

相关标签:
4条回答
  • 2021-02-10 05:04

    You can determine by using currentUser https://firebase.google.com/docs/reference/js/firebase.UserInfo

    Like this:

    firebase.auth().currentUser.providerData[0].providerId

    Hope this helps.

    0 讨论(0)
  • 2021-02-10 05:12

    @mjrdnk's answer is correct, but there are cases that are not covered by the answer so I find that this solution works best in all cases (that I have tested).

    val user = firebaseAuth.currentUser
    
            user?.let {
    
            authProvider = when (it.providerData[it.providerData.size-1].providerId) {
                "phone" -> {
                    ConnectedUser.LOGIN_PROVIDERS.PHONE
                }
                "password" -> {
                    // Email and password
                    ConnectedUser.LOGIN_PROVIDERS.EMAIL
                }
                else -> {
                    ConnectedUser.LOGIN_PROVIDERS.UNKNOWN
                }
            }
    .
    .
    .
    

    Wonderful source of information: FirebaseUI for Auth

    Some provider names follow (see above source code for more):

    ANONYMOUS_PROVIDER = "anonymous"
    EMAIL_LINK_SIGN_IN_METHOD = "emailLink"
    EMAIL_PASSWORD_SIGN_IN_METHOD = "password"
    MICROSOFT_PROVIDER = "microsoft.com"
    YAHOO_PROVIDER = "yahoo.com"
    APPLE_PROVIDER = "apple.com"
    PHONE = "phone"
    
    0 讨论(0)
  • 2021-02-10 05:17

    @mjrdnk's answer is correct, but there is a caveat, a user could have multiple providers linked. So using firebase.auth().currentUser.providerData[0].providerId will always yield the same provider even when another linked provider was used to sign in. The most accurate way to determine the current provider used to sign is by inspecting the ID token's field: firebase.sign_in_provider.

    0 讨论(0)
  • 2021-02-10 05:19

    Currently @mjdnk asnwer depracated, because it will gives always first provider not the last logged in.

    So most recent solution is:

    As noted here

    var uiConfig = {
            callbacks: {
              signInSuccessWithAuthResult: function(authResult, redirectUrl) {
                var providerId = authResult.additionalUserInfo.providerId;
                localStorage.setItem("firebaseProviderId", providerId)
                //...
              },
              //..
           }
    

    and for display in page

    firebase.auth().onAuthStateChanged(function (user) {
          if (user) {
            user.getIdToken().then(function (idToken) {
    
              $('#user').text(welcomeName + "(" + localStorage.getItem("firebaseProviderId")+ ")");
              $('#logged-in').show();
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题