【发布时间】:2017-11-10 00:56:47
【问题描述】:
我正在使用 FireBase 和 FireBaseAuth 为 Google 登录制作一个 Android 应用程序。我的数据库中没有用户表,因此 FireBaseAuth 除了对用户进行身份验证之外并没有做太多事情。
我在 MainScreen (HomeScreen) 中有一个 NavigationDrawer,我希望在其中显示用户的个人资料图片、姓名和电子邮件。像这个例子(但只有 1 个帐户/图片):
用户无需登录即可使用该应用,但某些活动(如撰写评论)需要用户登录。如果用户未登录,则无需显示有关用户的任何内容导航抽屉。但是,如果有登录用户,则应显示其信息,如示例所示。
这是我在 ReviewActivity 中的 FireBaseAuth 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviews);
// Setting up the toolbar:
Toolbar toolbar = findViewById(R.id.Activity_Review_Toolbar);
toolbar.setTitle("Write a review");
// Setting up the database:
firebaseDatabase = FirebaseDatabase.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
reviewDatabaseReference = firebaseDatabase.getReference().child("Reviews");
firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
// Already logged in
}
else {
onSignedOutCleanup();
// Not signed in
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setAvailableProviders(
Arrays.asList(new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()))
.build(),
RC_SIGN_IN);
}
}
};
}
用户登录效果很好,但我的问题是,如何根据来自 FireBaseAuth 的 Google 信息存储用户信息? (我相信我必须为用户创建一个用户模型和一个表)。
感谢任何帮助,谢谢!
【问题讨论】:
标签: android firebase firebase-authentication navigation-drawer