第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

FirebaseAuth 和 FirebaseDatabase 不保存 Google 注冊

FirebaseAuth 和 FirebaseDatabase 不保存 Google 注冊

慕森王 2023-03-17 10:15:31
我有一個將 createUserWithEmailAndPassword 數(shù)據(jù)保存到實時數(shù)據(jù)庫和身份驗證數(shù)據(jù)庫的系統(tǒng)。但是在使用谷歌登錄創(chuàng)建了一個類似的系統(tǒng)之后,沒有任何東西會保存到數(shù)據(jù)庫中,也沒有任何東西保存到身份驗證數(shù)據(jù)庫中。我試過使用 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();我期待該應用程序將信息保存到實時數(shù)據(jù)庫和身份驗證數(shù)據(jù)庫中。這似乎都沒有用......
查看完整描述

3 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

SaveToDataBase您忘記在成功登錄后調用。這就是為什么沒有日志和數(shù)據(jù)庫條目的原因。



查看完整回答
反對 回復 2023-03-17
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

我正在考慮您是否已經將 firebase 添加到您的項目中,如果沒有,請點擊此鏈接https://firebase.google.com/docs/auth/android/google-signin


然后,您必須通過從左側面板中選擇身份驗證在 Firebase 中啟用谷歌登錄,然后選擇登錄提供商選項卡并啟用谷歌登錄。


你的項目級構建腳本應該是這樣的


buildscript {

repositories {

    google()

    jcenter()


}

dependencies {

    classpath 'com.android.tools.build:gradle:3.4.1'


    classpath 'com.google.gms:google-services:4.2.0'


    // NOTE: Do not place your application dependencies here; they belong

    // in the individual module build.gradle files

}

}


allprojects {

repositories {

    google()

    jcenter()


}

}


task clean(type: Delete) {

delete rootProject.buildDir

}

應用程序級別的 build.gradle 文件應該具有這些依賴項


//firebasecore

implementation 'com.google.firebase:firebase-core:17.0.0'

//firebase auth

implementation 'com.google.firebase:firebase-auth:18.0.0'

//google auth

implementation '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;


@Override

protected void onStart() {

    super.onStart();


 user = mAuth.getCurrentUser();

    if(user!=null)

    {

        startActivity(new Intent(Login_Activity.this,MainActivity.class));

        Login_Activity.this.finish();


    }


}




@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login_);


    gLogin=findViewById(R.id.gLogin);


    // ...

// Initialize Firebase Auth

    mAuth = FirebaseAuth.getInstance();


    // Configure sign-in to request the user's ID, email address, and basic

// profile. ID and basic profile are included in DEFAULT_SIGN_IN.

    GoogleSignInOptions gso = new 

 GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)

            .requestIdToken(getString(R.string.default_web_client_id))

            .requestEmail()

            .build();


    // Build a GoogleSignInClient with the options specified by gso.

    mGoogleSignInClient= GoogleSignIn.getClient(this, gso);




    gLogin.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            signIn();

        }

    });



}



private void signIn() {

    Intent signInIntent = mGoogleSignInClient.getSignInIntent();

    startActivityForResult(signInIntent, RC_SIGN_IN);

 Toast.makeText(this, "starting activity", Toast.LENGTH_SHORT).show();

}



@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);

    Toast.makeText(this, "inside on Activity result", 

 Toast.LENGTH_SHORT).show();


        try {

   Toast.makeText(this, "authenticating", Toast.LENGTH_SHORT).show();


// Google Sign In was successful, authenticate with Firebase

    GoogleSignInAccount account = task.getResult(ApiException.class);

            firebaseAuthWithGoogle(account);

        } catch (ApiException e) {

            // Google Sign In failed, update UI appropriately

            Log.w("firebase exception", "Google sign in failed", e);

            // ...

        }

        //handleSignInResult(task);

    }

}


private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

    Log.d("authenticate", "firebaseAuthWithGoogle:" + acct.getId());


    AuthCredential credential = 

GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    mAuth.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("message","signInWithCredential:success");

                        user = mAuth.getCurrentUser();

                        Log.d("user id", user.getUid());

                        startActivity(new 

Intent(Login_Activity.this,MainActivity.class));

                        Login_Activity.this.finish();


                    } else {

              // If sign in fails, display a message to the user.

                        Log.w("message","signInWithCredential:failure", task.getException());


                    }

                }

            });

}

您需要使用 google 登錄選項請求 ID 令牌,您可以使用此代碼,它將使用 google 登錄記錄您,并在 firebase 身份驗證用戶數(shù)據(jù)庫中登錄。


對于數(shù)據(jù)庫,您應該檢查一次數(shù)據(jù)庫規(guī)則的讀寫權限,它應該可以工作


查看完整回答
反對 回復 2023-03-17
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

在收到來自 Google Signin 的結果后,您沒有調用 firebase signin。


在你的內部handleSignInResult你有谷歌登錄的結果,你只需要創(chuàng)建 GoogleAuth 憑據(jù)并將其用于signInwithCredentials.


AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

mAuth.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 = mAuth.getCurrentUser();

                        saveUpdateUserProfile(user);

                    } else {

                        // If sign in fails, display a message to the user.

                        Log.w(TAG, "signInWithCredential:failure", task.getException());

                        Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();


                    }

                }

            });

這將創(chuàng)建/登錄 firebase 用戶,然后您可以檢查數(shù)據(jù)庫以查看用于登錄的 google 帳戶是否是用于保存用戶信息的新帳戶。


PS 你也可以優(yōu)化你的數(shù)據(jù)庫查詢。您當前的查詢將從數(shù)據(jù)庫中獲取所有用戶。此外,您不應將電子郵件地址用作數(shù)據(jù)庫中的鍵。


更高效的數(shù)據(jù)庫結構可以使用 firebase 用戶 ID 作為鍵:


users: {

 firebaaseUID1: {},

 firebaaseUID2: {},

 .

 .

}

你SaveToDataBase現(xiàn)在可以:


void SaveToDataBase(FirebaseUser 用戶,布爾值 isGoogleSignIn){


database.getReference().child("Users").child(user.getUid())

    .addListenerForSingleValueEvent(new ValueEventListener() {

            @Override

            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                if (dataSnapshot.exists()){

                    // firebase user data is present in db, do appropiate action or take user to home screen

                }

                else {

                    LoadingBar.setMessage("Please wait while we load the credentialls in");

                    LoadingBar.setTitle("Register");

                    LoadingBar.setCanceledOnTouchOutside(false);

                    LoadingBar.show();

                    HashMap<String, Object> Userdatamap = new HashMap<>();


                    Userdatamap.put("Email", user.getEmail());


                    // Userdatamap

                    //         .put("phoneNumber", "Google intigrated sign in does not allow phone number requesting... This will be fixed in later patches");


                    Userdatamap.put("Name", user.getDisplayName());


                    if (isGoogleSignIn)

                        Userdatamap.put("Created with", "Intigrated Google sign in");


                    database

                            .child("Users")

                            .child(user.getUid())

                            .updateChildren(Userdatamap)

                            .addOnCompleteListener(new OnCompleteListener<Void>() {

                                @Override

                                public void onComplete(@NonNull Task<Void> task) {

                                    LoadingBar.dismiss();

                                    Toast.makeText(LoginOrSignupActivity.this, "Database save successful", Toast.LENGTH_SHORT).show();

                                    Log.e("SignUpError :", task

                                            .getException()

                                            .getMessage());

                                }

                            }).addOnFailureListener(new OnFailureListener() {

                                @Override

                                public void onFailure(@NonNull Exception e) {

                                    Toast.makeText(LoginOrSignupActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();

                                    Log.e(Tag, "error: ");

                                }

                            });

                }

            }


            @Override

            public void onCancelled(@NonNull DatabaseError databaseError) {}

        });


    }

}



查看完整回答
反對 回復 2023-03-17
  • 3 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號