FirebaseAuth 和 FirebaseDatabase 不保存 Google 注册

我有一个将 createUserWithEmailAndPassword 数据保存到实时数据库和身份验证数据库的系统。但是在使用谷歌登录创建了一个类似的系统之后,没有任何东西会保存到数据库中,也没有任何东西保存到身份验证数据库中。

我试过使用 Log.e,我试过调试应用程序,也试过解码代码……

继承人一些代码:

package com.brandshopping.brandshopping;


import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;


import android.app.ProgressDialog;

import android.content.Intent;

import android.nfc.Tag;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

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.auth.api.Auth;

import com.google.android.gms.common.api.ApiException;

import com.google.android.gms.tasks.OnCompleteListener;

import com.google.android.gms.tasks.OnFailureListener;

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.HashMap;


public class LoginOrSignupActivity extends AppCompatActivity {


    private Button LoginBtn, RegisterWithEmailBtn, RegisterWithGoogleBtn;

    private String Tag;

    private ProgressDialog LoadingBar;


    private FirebaseDatabase firebasedatabase = FirebaseDatabase.getInstance();

    private DatabaseReference database = firebasedatabase.getReference();

我期待该应用程序将信息保存到实时数据库和身份验证数据库中。这似乎都没有用......


慕森王
浏览 110回答 3
3回答

12345678_0001

SaveToDataBase您忘记在成功登录后调用。这就是为什么没有日志和数据库条目的原因。

交互式爱情

我正在考虑您是否已经将 firebase 添加到您的项目中,如果没有,请点击此链接https://firebase.google.com/docs/auth/android/google-signin然后,您必须通过从左侧面板中选择身份验证在 Firebase 中启用谷歌登录,然后选择登录提供商选项卡并启用谷歌登录。你的项目级构建脚本应该是这样的buildscript {repositories {&nbsp; &nbsp; google()&nbsp; &nbsp; jcenter()}dependencies {&nbsp; &nbsp; classpath 'com.android.tools.build:gradle:3.4.1'&nbsp; &nbsp; classpath 'com.google.gms:google-services:4.2.0'&nbsp; &nbsp; // NOTE: Do not place your application dependencies here; they belong&nbsp; &nbsp; // in the individual module build.gradle files}}allprojects {repositories {&nbsp; &nbsp; google()&nbsp; &nbsp; jcenter()}}task clean(type: Delete) {delete rootProject.buildDir}应用程序级别的 build.gradle 文件应该具有这些依赖项//firebasecoreimplementation 'com.google.firebase:firebase-core:17.0.0'//firebase authimplementation 'com.google.firebase:firebase-auth:18.0.0'//google authimplementation 'com.google.android.gms:play-services-auth:17.0.0'并且登录应该有这样的代码public class Login_Activity extends AppCompatActivity {ImageView gLogin;private static final int RC_SIGN_IN=1;private FirebaseAuth mAuth;GoogleSignInClient mGoogleSignInClient;Firebase user;@Overrideprotected void onStart() {&nbsp; &nbsp; super.onStart();&nbsp;user = mAuth.getCurrentUser();&nbsp; &nbsp; if(user!=null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; startActivity(new Intent(Login_Activity.this,MainActivity.class));&nbsp; &nbsp; &nbsp; &nbsp; Login_Activity.this.finish();&nbsp; &nbsp; }}@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_login_);&nbsp; &nbsp; gLogin=findViewById(R.id.gLogin);&nbsp; &nbsp; // ...// Initialize Firebase Auth&nbsp; &nbsp; mAuth = FirebaseAuth.getInstance();&nbsp; &nbsp; // Configure sign-in to request the user's ID, email address, and basic// profile. ID and basic profile are included in DEFAULT_SIGN_IN.&nbsp; &nbsp; GoogleSignInOptions gso = new&nbsp;&nbsp;GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .requestIdToken(getString(R.string.default_web_client_id))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .requestEmail()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; // Build a GoogleSignInClient with the options specified by gso.&nbsp; &nbsp; mGoogleSignInClient= GoogleSignIn.getClient(this, gso);&nbsp; &nbsp; gLogin.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; signIn();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}private void signIn() {&nbsp; &nbsp; Intent signInIntent = mGoogleSignInClient.getSignInIntent();&nbsp; &nbsp; startActivityForResult(signInIntent, RC_SIGN_IN);&nbsp;Toast.makeText(this, "starting activity", Toast.LENGTH_SHORT).show();}@Overridepublic void onActivityResult(int requestCode, int resultCode,Intent data) {&nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; // Result returned from launching the Intent from&nbsp;GoogleSignInClient.getSignInIntent(...);&nbsp; &nbsp; if (requestCode == RC_SIGN_IN) {// The Task returned from this call is always completed, no need to&nbsp;&nbsp; &nbsp;//attach&nbsp; &nbsp; &nbsp; &nbsp; // a listener.&nbsp;Task<GoogleSignInAccount> task =&nbsp;&nbsp;GoogleSignIn.getSignedInAccountFromIntent(data);&nbsp; &nbsp; Toast.makeText(this, "inside on Activity result",&nbsp;&nbsp;Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp;Toast.makeText(this, "authenticating", Toast.LENGTH_SHORT).show();// Google Sign In was successful, authenticate with Firebase&nbsp; &nbsp; GoogleSignInAccount account = task.getResult(ApiException.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firebaseAuthWithGoogle(account);&nbsp; &nbsp; &nbsp; &nbsp; } catch (ApiException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Google Sign In failed, update UI appropriately&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.w("firebase exception", "Google sign in failed", e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //handleSignInResult(task);&nbsp; &nbsp; }}private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {&nbsp; &nbsp; Log.d("authenticate", "firebaseAuthWithGoogle:" + acct.getId());&nbsp; &nbsp; AuthCredential credential =&nbsp;GoogleAuthProvider.getCredential(acct.getIdToken(), null);&nbsp; &nbsp; mAuth.signInWithCredential(credential)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addOnCompleteListener(this, new OnCompleteListener<AuthResult>()&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<AuthResult> task)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; // Sign in success, update UI with the signed-in user's information&nbsp; Log.d("message","signInWithCredential:success");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user = mAuth.getCurrentUser();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("user id", user.getUid());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(new&nbsp;Intent(Login_Activity.this,MainActivity.class));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Login_Activity.this.finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If sign in fails, display a message to the user.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.w("message","signInWithCredential:failure", task.getException());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });}您需要使用 google 登录选项请求 ID 令牌,您可以使用此代码,它将使用 google 登录记录您,并在 firebase 身份验证用户数据库中登录。对于数据库,您应该检查一次数据库规则的读写权限,它应该可以工作

UYOU

在收到来自 Google Signin 的结果后,您没有调用 firebase signin。在你的内部handleSignInResult你有谷歌登录的结果,你只需要创建 GoogleAuth 凭据并将其用于signInwithCredentials.AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);mAuth.signInWithCredential(credential)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<AuthResult> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Sign in success, update UI with the signed-in user's information&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, "signInWithCredential:success");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirebaseUser user = mAuth.getCurrentUser();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saveUpdateUserProfile(user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If sign in fails, display a message to the user.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.w(TAG, "signInWithCredential:failure", task.getException());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });这将创建/登录 firebase 用户,然后您可以检查数据库以查看用于登录的 google 帐户是否是用于保存用户信息的新帐户。PS 你也可以优化你的数据库查询。您当前的查询将从数据库中获取所有用户。此外,您不应将电子邮件地址用作数据库中的键。更高效的数据库结构可以使用 firebase 用户 ID 作为键:users: {&nbsp;firebaaseUID1: {},&nbsp;firebaaseUID2: {},&nbsp;.&nbsp;.}你SaveToDataBase现在可以:void SaveToDataBase(FirebaseUser 用户,布尔值 isGoogleSignIn){database.getReference().child("Users").child(user.getUid())&nbsp; &nbsp; .addListenerForSingleValueEvent(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(@NonNull DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dataSnapshot.exists()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // firebase user data is present in db, do appropiate action or take user to home screen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadingBar.setMessage("Please wait while we load the credentialls in");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadingBar.setTitle("Register");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadingBar.setCanceledOnTouchOutside(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadingBar.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HashMap<String, Object> Userdatamap = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Userdatamap.put("Email", user.getEmail());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Userdatamap&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.put("phoneNumber", "Google intigrated sign in does not allow phone number requesting... This will be fixed in later patches");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Userdatamap.put("Name", user.getDisplayName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isGoogleSignIn)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Userdatamap.put("Created with", "Intigrated Google sign in");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; database&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .child("Users")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .child(user.getUid())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .updateChildren(Userdatamap)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addOnCompleteListener(new OnCompleteListener<Void>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<Void> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadingBar.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(LoginOrSignupActivity.this, "Database save successful", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("SignUpError :", task&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getException()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).addOnFailureListener(new OnFailureListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(@NonNull Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(LoginOrSignupActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e(Tag, "error: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(@NonNull DatabaseError databaseError) {}&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java