我想知道如果用户是新的或存在。我想显示额外的查看如果用户是新的。我已经实现了谷歌登录使用此链接https://developers.google.com/identity/sign-in/android/sign-in。一切正常,但除了我想知道如果用户是新的或不。如果登录用户是新的,我想做一些事情。
这里是我的代码登录使用谷歌认证.
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Objects;
public class Login extends AppCompatActivity {
private static final int RC_SIGN_IN = 100;
LinearLayout button;
GoogleSignInClient mGoogleSignInClient;
GoogleSignInAccount account;
DatabaseReference databaseReference;
ProgressDialog progressDialog;
long childTotal = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
button = findViewById(R.id.loginBtn);
getWindow().setStatusBarColor(getResources().getColor(R.color.white));
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(Login.this, gso);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
childTotal = snapshot.getChildrenCount();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressDialog = new ProgressDialog(Login.this);
progressDialog.setMessage("Please wait . . . ");
progressDialog.setCancelable(false);
progressDialog.show();
signIn();
}
});
}
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 GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
if (account!=null){
addUserDataToDatabase();
startActivity(new Intent(Login.this, MainActivity.class));
progressDialog.dismiss();
finish();
}
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
progressDialog.dismiss();
// Toast.makeText(this, "Request cancelled by user.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStart() {
super.onStart();
account = GoogleSignIn.getLastSignedInAccount(this);
if (account!=null) {
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
private void addUserDataToDatabase(){
UserModel userModel = new UserModel();
userModel.setName(account.getDisplayName());
userModel.setEmail(account.getEmail());
if (account.getPhotoUrl()==null){
userModel.setPicture("No image available");
}else {
userModel.setPicture(String.valueOf(account.getPhotoUrl()));
}
databaseReference.child(Objects.requireNonNull(account.getDisplayName()))
.setValue(userModel);
}
}
1条答案
按热度按时间prdp8dxp1#
如果你想检查用户是否是Firebase的新手,那么你可以使用我在下面帖子中的答案:
如果您不想使用Firebase身份验证,但我强烈建议您实现它,那么您可以在Firestore或Realtime Database中添加用户对象。
由于您使用的是Java,要检查用户是否存在于Firestore中,请检查以下答案:
要检查用户是否存在于实时数据库中,请检查以下答案:
因此,当用户创建一个新帐户时,首先检查UID是否已经存在于数据库中,如果不存在,则该用户是新用户,否则是现有用户。