i'm a beginner who is learning how to use Android studio and this is my first time posting on Stack Overflow. I wish to implement Facebook login on my android app. I mostly followed this tutorial (https://www.youtube.com/watch?v=ImbuK35vmzs&t=1540s) to create the sign in using facebook.
While I'm able to run the app, ,whenever i click on the sign in using facebook button, the chrome browser opens up and the screen is stuck loading the facebook sign in page (refer to the screenshot). No error pops up on logcat, i've already enabled internet permission in the android manifest file as well as check to see the facebook access id + secret key were correctly copied. Below is my MainActivity.java code. Any idea why this issue might be happening and how do i go about troubleshooting it? Thank you.
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mFirebaseAuth;
private CallbackManager mCallbackManager;
private TextView textViewUser;
private LoginButton loginButton;
private ImageView mLogo;
private FirebaseAuth.AuthStateListener authStateListener;
private AccessTokenTracker accessTokenTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
Button registerButton = findViewById(R.id.signupButton);
textViewUser = findViewById(R.id.textView_user);
mFirebaseAuth = FirebaseAuth.getInstance();
//Initialise facebook SDK
FacebookSdk.sdkInitialize(getApplicationContext());
//Assign textviewuser to the facebook button
mLogo = findViewById(R.id.image_logo);
loginButton = findViewById(R.id.facebook_button);
// Requesting read permission for the email and profile for facebook login
loginButton.setReadPermissions("email","public_profile");
mCallbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "onSuccess" + loginResult);
handleFacebookToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.d(TAG, "onCancel");
}
@Override
public void onError(@NonNull FacebookException error) {
Log.d(TAG, "onError" + error);
}
});
authStateListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
updateUI(user);
} else {
updateUI(null);
}
}
};
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
if(currentAccessToken == null){
mFirebaseAuth.signOut();
}
}
};
private void handleFacebookToken(AccessToken token) {
Log.d(TAG, "handleFacebookToken" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mFirebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Log.d(TAG, "sign in with credential: successful");
FirebaseUser user = mFirebaseAuth.getCurrentUser();
updateUI(user);
}else{
Log.d(TAG, "sign in with credential: failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication Failed", Toast.LENGTH_SHORT).show();
updateUI(null);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
mCallbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
private void updateUI(FirebaseUser user) {
if (user != null) {
textViewUser.setText(user.getDisplayName());
if(user.getPhotoUrl()!=null){
String photoUrl= user.getPhotoUrl().toString();
photoUrl = photoUrl + "?type=large";
Picasso.get().load(photoUrl).into(mLogo);
}
}
else{
textViewUser.setText("");
mLogo.setImageResource((R.drawable.logo));
}
}
public void toastMsg(String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart(){
super.onStart();
mFirebaseAuth.addAuthStateListener((authStateListener));
}
@Override
protected void onStop(){
super.onStop();
if(authStateListener!=null){
mFirebaseAuth.removeAuthStateListener(authStateListener);
}
}
}