i'm trying to make an auth with facebook on firebase. I've already set up facebook SDK and I can sign in with facebook perfectly.
But I can't get firebase auth work. The app has a TOAST on the method onComplete(Task <AuthResult> task) and if something went wrong during the process it will show up. I'm getting this TOAST error, but can't find where is the problem. I've set up everything like in firebase docs.
public class LoginActivity extends AppCompatActivity {
private LoginButton loginButton;
private CallbackManager callbackManager;
private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
firebaseAuth = FirebaseAuth.getInstance();
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.loginButton);
loginButton.setReadPermissions("email","public_profile");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"Error al iniciar sesión",Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(),"Error al iniciar sesión",Toast.LENGTH_LONG).show();
}
});
firebaseAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
goMainScreen();
}
}
};
}
private void handleFacebookAccessToken(AccessToken accessToken) {
Log.d("","handle"+accessToken.getCurrentAccessToken().getToken());
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getCurrentAccessToken().getToken());
firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show(); // here is the ERROR.
}
}
});
}
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);
}
@Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
}
@Override
protected void onStop() {
super.onStop();
firebaseAuth.removeAuthStateListener(firebaseAuthListener);
}
}
I've already enabled Facebook auth on firebase and already copied the id and secret. Also I've already copied the Firebase URL in Facebook developers.
"error"in your toast, showtask. getException().toString()or better yet: throw the exception withthrow task.getException(). - Frank van Puffelen