当用户从注册页面登录时,它表示登录成功,但是当我转到下一个活动并检查是否已登录时,它表明没有用户登录,
Im用于登录的代码为:
${url}
即使再次打开活动,它仍然显示用户已注销
我用来检查用户是否登录的代码如下:-
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
progressDialog.dismiss();
} catch (ApiException e) {
Log.w(TAG, "Google sign in failed", e);
String s = task.getException().getMessage();
Toast.makeText(getApplicationContext(),"" + s,Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
}
private void updateUI(FirebaseUser user) {
if (user != null) {
/* Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish(); */
} else {
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
Auth.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 = Auth.getCurrentUser();
// updateUI(user);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
progressDialog.dismiss();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Snackbar.make(findViewById(R.id.snake), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
}
我参考了developer.google.com的这段代码。 任何帮助将不胜感激。