【发布时间】:2018-12-28 19:47:32
【问题描述】:
我的应用中有几种注册方法,如下所示:
让我们围绕 Google 登录方法来解决问题:
我正在做的是,当用户登录时,enteringDataIntoUserNode() 方法在 signInWithCredential() 方法中被调用,例如:
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential =
GoogleAuthProvider.getCredential(acct.getIdToken(), null);
// signing in
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// entering default values in the database
enteringDataIntoUserNode();
Intent intent = new Intent(LoginAndSignupActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
else {
Log.w(TAG, "signInWithCredential:failure", task.getException());
}
}
});
}
进入DataIntoUserNode()
private void enteringDataIntoUserNode() {
final String currentUid = mAuth.getCurrentUser().getUid();
//
String deviceToken = FirebaseInstanceId.getInstance().getToken();
final String userName = mAuth.getCurrentUser().getDisplayName();
String imageUrl = mAuth.getCurrentUser().getPhotoUrl().toString();
String userStatus = "Hi there! How is it going?";
// inserting data in key-value pair
usersReference.child(currentUid).child("user_name").setValue(userName);
usersReference.child(currentUid).child("user_status").setValue(userStatus);
usersReference.child(currentUid).child("user_image").setValue(imageUrl);
usersReference.child(currentUid).child("device_token").setValue(deviceToken);
// when this last value will be inserted then, making an intent to MainActivity
usersReference.child(currentUid).child("user_thumb_image").setValue(imageUrl)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.d(TAG, "onComplete: Data insertion for the new user is successful ");
}
}
});
}
enteringDataIntoUserNode() 方法只是在每次用户登录时将一些默认数据传递给数据库。但是如果用户更改其图像或用户名等详细信息并退出并再次登录通过 Google 登录方法 然后再次调用 enteringDataIntoUserNode() 方法,用户详细信息将被默认值覆盖。
我的问题是,有没有办法在 Google 登录期间检查用户是否是第一次登录,以便在后一种情况下我可以跳过调用 进入DataIntoUserNode() 方法并防止数据被覆盖。 我希望在 Facebook 和 电话号码 登录期间实现同样的目标。
【问题讨论】:
标签: android firebase firebase-authentication google-signin