当我按下按钮后,我在我的应用程序中使用Facebook登录在我按“打开”后,它仍然正常运行继续,因为(UserName)该应用程序连接到Firebase时什么都没发生,我添加了两个Toast以检查是否登录进程是否失败,但是它们什么都没有出现,失败时应该出现的吐司或者正常工作时应该出现的其他吐司
这是我的活动代码:
public class GeneralSignActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
SignInButton button;
private final static int RC_SIGN_IN = 2;
private final String TAG = "Logging";
CallbackManager mCallbackManager;
GoogleApiClient mGoogleApiClient;
GoogleSignInOptions gso;
private GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general_sign);
ButterKnife.bind(this);
mAuth = FirebaseAuth.getInstance();
button = findViewById(R.id.googleBtn);
FacebookSdk.sdkInitialize(getApplicationContext());
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(GeneralSignActivity.this, "حدث خطأ برجاء اعادة المحاولة", Toast.LENGTH_SHORT).show();
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API)
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = findViewById(R.id.facebookBtn);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
Toast.makeText(GeneralSignActivity.this, "نجح تسجيل الدخول", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
Intent i = new Intent(GeneralSignActivity.this, MainActivity.class);
startActivity(i);
Toast.makeText(GeneralSignActivity.this, "handle facebook access token.",
Toast.LENGTH_SHORT).show();
//updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(GeneralSignActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
// updateUI(null);
}
// ...
}
});
}
@OnClick(R.id.btnSignUp)
void click(View view) {
Intent i = new Intent(this, SignUpActivity.class);
startActivity(i);
}
@OnClick(R.id.btnSignIn)
void click2(View view) {
Intent i = new Intent(this, SignInActivity.class);
startActivity(i);
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
Toast.makeText(this, "نجح تسجيل الدخول",Toast.LENGTH_SHORT).show();
startActivity(new Intent(GeneralSignActivity.this,MainActivity.class));
} catch (ApiException e) {
Toast.makeText(GeneralSignActivity.this, "حدث خطأ أثناء محاولة التسجيل برجاء اعادة المحاولة", Toast.LENGTH_SHORT).show();
}
}//TODO
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
} else {
// If sign in fails, display a message to the user.
Toast.makeText(GeneralSignActivity.this, "Authentication Failed.", Toast.LENGTH_SHORT).show();
//updateUI(null);
}
// ...
}
});
}
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
startActivity(new Intent(GeneralSignActivity.this, MainActivity.class));
}
}
}