Last active
August 7, 2025 21:49
-
-
Save alvareztech/8c38122832535b20f4afc42c5b0b9366 to your computer and use it in GitHub Desktop.
Firebase Auth with Facebook a Android application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tech.alvarez.today; | |
| import android.content.Intent; | |
| import android.os.Bundle; | |
| import android.support.annotation.NonNull; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.util.Log; | |
| import android.widget.Toast; | |
| import com.facebook.AccessToken; | |
| import com.facebook.CallbackManager; | |
| import com.facebook.FacebookCallback; | |
| import com.facebook.FacebookException; | |
| import com.facebook.login.LoginResult; | |
| import com.facebook.login.widget.LoginButton; | |
| import com.google.android.gms.tasks.OnCompleteListener; | |
| import com.google.android.gms.tasks.Task; | |
| import com.google.firebase.auth.AuthCredential; | |
| import com.google.firebase.auth.AuthResult; | |
| import com.google.firebase.auth.FacebookAuthProvider; | |
| import com.google.firebase.auth.FirebaseAuth; | |
| import com.google.firebase.auth.FirebaseUser; | |
| import java.util.Arrays; | |
| public class LoginActivity extends AppCompatActivity { | |
| private LoginButton loginButton; | |
| private CallbackManager callbackManager; | |
| private FirebaseAuth firebaseAuth; | |
| private FirebaseAuth.AuthStateListener firebaseAuthListener; | |
| public static final String TAG = "TODAY"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_login); | |
| callbackManager = CallbackManager.Factory.create(); | |
| loginButton = (LoginButton) findViewById(R.id.loginButton); | |
| firebaseAuth = FirebaseAuth.getInstance(); | |
| loginButton.setReadPermissions("email", "public_profile"); | |
| loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { | |
| @Override | |
| public void onSuccess(LoginResult loginResult) { | |
| // goMainScreen(); | |
| Log.d("TODAY", "onSuccess"); | |
| handleFacebookAccessToken(loginResult.getAccessToken()); | |
| } | |
| @Override | |
| public void onCancel() { | |
| Toast.makeText(getApplicationContext(), R.string.cancel_login, Toast.LENGTH_SHORT).show(); | |
| } | |
| @Override | |
| public void onError(FacebookException error) { | |
| Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| firebaseAuthListener = new FirebaseAuth.AuthStateListener() { | |
| @Override | |
| public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
| FirebaseUser user = firebaseAuth.getCurrentUser(); | |
| if (user != null) { | |
| Log.d("TODAY", "OK FIREBASE" + user.getUid()); | |
| } else { | |
| Log.d("TODAY", "NO LOGIN"); | |
| } | |
| } | |
| }; | |
| } | |
| private void handleFacebookAccessToken(AccessToken accessToken) { | |
| Log.d("TODAY", "handleFacebookAccessToken"); | |
| AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken()); | |
| firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
| @Override | |
| public void onComplete(@NonNull Task<AuthResult> task) { | |
| Log.d("TODAY", "signInWithCredential:onComplete:" + task.isSuccessful()); | |
| if (!task.isSuccessful()) { | |
| Log.d("TODAY", "fallo:" + task.isSuccessful()); | |
| } | |
| } | |
| }); | |
| } | |
| @Override | |
| protected void onStart() { | |
| super.onStart(); | |
| firebaseAuth.addAuthStateListener(firebaseAuthListener); | |
| } | |
| @Override | |
| protected void onStop() { | |
| super.onStop(); | |
| if (firebaseAuthListener != null) { | |
| firebaseAuth.removeAuthStateListener(firebaseAuthListener); | |
| } | |
| } | |
| private void goMainScreen() { | |
| Intent intent = new Intent(this, MainActivity.class); | |
| intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); | |
| startActivity(intent); | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| super.onActivityResult(requestCode, resultCode, data); | |
| callbackManager.onActivityResult(requestCode, resultCode, data); | |
| } | |
| } |
thanks!
Gracias por tu tutorial! 💯
Thank's a lot 👍
el todayapp.java es otra activity?
que funcion cumple?
thanks!
great! some deprecation happens
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kindly upload the manifest file too