Firebase Auth get additional user info (age, gender)

前端 未结 4 1753
眼角桃花
眼角桃花 2020-12-05 08:12

I am using Firebase Authentication for my Android app. Users have the ability to login with multiple providers (Google, Facebook, Twitter).

After a successful login,

4条回答
  •  遥遥无期
    2020-12-05 08:34

    No, you can't get these data directly. But you can use the id of the user and get these data from various providers. Please check before what are the data that are available in the public API for each of these providers for instance google just deprecated few methods from the peopleApi.

    Anyways here is what i do for facebook

    // Initialize Firebase Auth
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    
    // Create a listener
    FirebaseAuth.AuthStateListener mAuthListener = firebaseAuth -> {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
    
            if (user != null) {
                Log.d(TAG, "User details : " + user.getDisplayName() + user.getEmail() + "\n" + user.getPhotoUrl() + "\n"
                        + user.getUid() + "\n" + user.getToken(true) + "\n" + user.getProviderId());
    
                String userId = user.getUid(); 
                String displayName = user.getDisplayName();
                String photoUrl = String.valueOf(user.getPhotoUrl());
                String email = user.getEmail();
    
                Intent homeIntent = new Intent(LoginActivity.this, HomeActivity.class);
                startActivity(homeIntent);
                finish();
            }
        };
    
    //Initialize the fB callbackManager
    mCallbackManager = CallbackManager.Factory.create();
    

    And do the following inside the onClick of the FB login button

    LoginManager.getInstance().registerCallback(mCallbackManager,
                new FacebookCallback() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        Log.d(TAG, "facebook:onSuccess:" + loginResult);
                        handleFacebookAccessToken(loginResult.getAccessToken());
                    }
    
                    @Override
                    public void onCancel() {
                        Log.d(TAG, "facebook:onCancel");
                    }
    
                    @Override
                    public void onError(FacebookException error) {
                        Log.d(TAG, "facebook:onError", error);
                    }
                });
    
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
    

提交回复
热议问题