gpt4 book ai didi

java - 用户名和密码不会保存在 Firebase 数据库中

转载 作者:行者123 更新时间:2023-12-01 17:30:28 25 4
gpt4 key购买 nike

下面是我在 android studio 中的注册页面和登录页面的代码。然而,在注册用户详细信息时,代码似乎没有上传到我的 firebase 数据库中。因此,在我的登录页面上无法使用此数据。不确定如何在我的代码中纠正这个问题。任何帮助,将不胜感激。

package com.example.spaceattack;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class RegisterActivity extends AppCompatActivity {
EditText emailId, password;
Button btnSignUp;
TextView tvSignIn;
FirebaseAuth mFirebaseAuth;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.editText);
password = findViewById(R.id.editText2);
btnSignUp = findViewById(R.id.button2);
tvSignIn = findViewById(R.id.textView);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailId.getText().toString();
String pwd = password.getText().toString();
if(email.isEmpty()) {
emailId.setError("Please enter email id");
emailId.requestFocus();
}
else if(pwd.isEmpty()) {
password.setError("Enter your password");
password.requestFocus();
}
else if(email.isEmpty() && pwd.isEmpty()) {
Toast.makeText(RegisterActivity.this,"Fields are Empty",Toast.LENGTH_SHORT).show();
}
else if(! (email.isEmpty() && pwd.isEmpty())) {

mFirebaseAuth.createUserWithEmailAndPassword(email, pwd) .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()) {
Toast.makeText(RegisterActivity.this,"SignUp Unsuccessful, Try again",Toast.LENGTH_SHORT).show();

}

else {
startActivity(new Intent(RegisterActivity.this, WaterActivity.class));
}
}
});

}
else {

Toast.makeText(RegisterActivity.this,"Error!",Toast.LENGTH_SHORT).show();

}

}
});

tvSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent (RegisterActivity.this, LoginActivity.class);
startActivity(i);
}
});
}
}







package com.example.spaceattack;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LoginActivity extends AppCompatActivity {
EditText emailId, password;
Button btnSignUp;
TextView tvSignUp;
FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

mFirebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.editText);
password = findViewById(R.id.editText2);
btnSignUp = findViewById(R.id.button2);
tvSignUp = findViewById(R.id.textView2);

mAuthStateListener = new FirebaseAuth.AuthStateListener() {


@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
if (mFirebaseUser != null) {
Toast.makeText(LoginActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
} else {
Toast.makeText(LoginActivity.this, "Please Login", Toast.LENGTH_SHORT).show();

}
}
};

btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailId.getText().toString();
String pwd = password.getText().toString();
if (email.isEmpty()) {
emailId.setError("Please enter email id");
emailId.requestFocus();
} else if (pwd.isEmpty()) {
password.setError("Enter your password");
password.requestFocus();
} else if (email.isEmpty() && pwd.isEmpty()) {
Toast.makeText(LoginActivity.this, "Fields are Empty", Toast.LENGTH_SHORT).show();
} else if (!(email.isEmpty() && pwd.isEmpty())) {

mFirebaseAuth.signInWithEmailAndPassword(email, pwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "Login Error, Please Login Again", Toast.LENGTH_SHORT).show();
} else {
Intent intToHome = new Intent(LoginActivity.this, WaterActivity.class);
startActivity(intToHome);
}

}

});
} else {

Toast.makeText(LoginActivity.this, "Error!", Toast.LENGTH_SHORT).show();

}

}
});

tvSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intSignUp = new Intent(LoginActivity.this, WaterActivity.class);
startActivity(intSignUp);
}

});
}

@Override
protected void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
}

最佳答案

据我所知,您问题中的代码均不会写入 Firebase 实时数据库。请记住,Firebase 身份验证仅将用户配置文件写入其自己的内部存储。如果您希望将该信息存储在数据库中,则必须自己通过应用程序代码执行此操作。

关于java - 用户名和密码不会保存在 Firebase 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61131392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com