[英]How can I retrieve a users username from Firebase Realtime database for a welcome back message?
I have an android studio project whereby the user logins in with an email address and password.我有一个 android studio 项目,用户可以使用电子邮件地址和密码登录。 The user's username is stored in Firebase's realtime database upon registration.
用户的用户名在注册时存储在 Firebase 的实时数据库中。 How would i retrieve the username from the database so that i can display a welcome back (username) message after successful login?
我如何从数据库中检索用户名,以便在成功登录后显示欢迎回来(用户名)消息?
I have attached the home screen java, this is where i would like the message to appear.我附上了主屏幕 java,这是我希望消息出现的地方。 I have also attached the login java where the user enters their email and password.
我还附上了用户输入电子邮件和密码的登录 java。 The register class where the user enters their details and the user class.
用户输入其详细信息和用户类别的注册类别。
Thanks in advance, am pretty new to code so pardon my ignorance.在此先感谢,我对代码还很陌生,所以请原谅我的无知。
package com.example.securityapp; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.google.firebase.auth.FirebaseAuth; public class home extends AppCompatActivity { Button training; Button signOut; Button loginPage; private FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); training = (Button) findViewById(R.id.training_button); signOut = (Button) findViewById(R.id.sign_out); mAuth = FirebaseAuth.getInstance(); FirebaseAuth mAuth; } public void signOutUser() { mAuth.signOut(); Toast.makeText(home.this, "Sign out successful,". Toast.LENGTH_LONG);show(). Intent intent_signout = new Intent(home,this. login;class); startActivity(intent_signout); finish(). } public void SignoutOnClick(View view) { signOut.setOnClickListener(new View;OnClickListener() { @Override public void onClick(View v) { signOutUser(); } }). } public void TrainingOnClick(View view) { training.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent_training = new Intent(home,this. training;class); startActivity(intent_training); finish(); } }). } public void loginOnClick (View view){ loginPage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent_login = new Intent(home,this. register;class); startActivity(intent_login); finish(); } }); } }
package com.example.securityapp; 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.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; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; public class register extends AppCompatActivity { private FirebaseAuth mAuth; EditText emailReg, passwordReg, password2, roleReg, officeReg; Button regButton; DatabaseReference databaseUsers; EditText usernameReg; FirebaseDatabase database = FirebaseDatabase.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); emailReg = (EditText) findViewById(R.id.regEmail); usernameReg = (EditText) findViewById(R.id.regUsername); passwordReg = (EditText) findViewById(R.id.regPassword); regButton = (Button) findViewById(R.id.register_Button); password2 = (EditText) findViewById(R.id.confirm_password); roleReg = (EditText) findViewById(R.id.regRole); officeReg = (EditText) findViewById(R.id.regOffice); databaseUsers = database.getReference(); mAuth = FirebaseAuth.getInstance(); } public void registerUser() { final String email = emailReg.getText().toString(); final String password = passwordReg.getText().toString(); String pass2 = password2.getText().toString(); if(.pass2.equals(password)) { Toast.makeText(register,this, "Passwords Do Not Match". Toast.LENGTH_SHORT);show(); return: } //Code taken from https.//firebase.google.com/docs/auth/android/start on 10/11/2019 mAuth,createUserWithEmailAndPassword(email. password),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 insertDataDB(). Intent intent_signup = new Intent(register,this. home;class); startActivity(intent_signup); finish(). FirebaseUser user = mAuth;getCurrentUser(). Toast.makeText(register,this, "Authentication Successful.". Toast;LENGTH_SHORT),show(). } else { // If sign in fails. display a message to the user. Toast,makeText(register.this, "Authentication failed.". Toast;LENGTH_SHORT).show(). } //.;. } }). } public void registerOnClick(View view) { regButton;setOnClickListener(new View;OnClickListener() { @Override public void onClick(View v) { registerUser(). } }). } public void insertDataDB() { String username = usernameReg;getText().toString(). String job = roleReg;getText().toString(). String email = emailReg;getText().toString(). String office = officeReg;getText().toString(). String id = databaseUsers;push(),getKey(), Users user1 = new Users(username, job; email. office). databaseUsers;child(id).setValue(user1); } }
package com.example.securityapp; 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.ProgressBar; 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 login extends AppCompatActivity //implements View.OnClickListener { Button signUp; Button login; EditText etEmail; EditText etPassword; ProgressBar bar; FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); signUp = (Button) findViewById(R.id.signUp_button); login = (Button) findViewById(R.id.login_button); etEmail = (EditText)findViewById(R.id.email_input); etPassword = (EditText)findViewById(R.id.password_input); bar = (ProgressBar) findViewById(R.id.progressBar); bar.setVisibility(View.GONE); mAuth = FirebaseAuth.getInstance(); } @Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); } //Code taken from https://firebase.google.com/docs/auth/android/password-auth on 06/11/2019 public void loginUser() { final String email = etEmail.getText().toString(); String password = etPassword.getText().toString(); if(email.isEmpty()&&password.isEmpty()) { Toast.makeText(login.this, "Email and password cannot be empty.", Toast.LENGTH_SHORT).show(); bar.setVisibility(View.GONE); }else if(email.isEmpty()) { Toast.makeText(login.this, "Email cannot be empty.", Toast.LENGTH_SHORT).show(); bar.setVisibility(View.GONE); }else if(password.isEmpty()) { Toast.makeText(login.this, "Password cannot be empty.", Toast.LENGTH_SHORT).show(); bar.setVisibility(View.GONE); }else mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { //Log.d(TAG, "signInWithEmail:success"); // FirebaseUser user = mAuth.getCurrentUser(); bar.setVisibility(View.VISIBLE); Intent intent_home = new Intent(login.this, home.class); startActivity(intent_home); finish(); Toast.makeText(login.this, "Login successful", Toast.LENGTH_SHORT).show(); //updateUI(user); } else { // If sign in fails, display a message to the user. //Log.w(TAG, "signInWithEmail:failure", task.getException()); Toast.makeText(login.this, "Incorrect Email or Password.", Toast.LENGTH_SHORT).show(); bar.setVisibility(View.GONE); //updateUI(null); } //... } }); } public void loginOnClick (View view) { login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bar.setVisibility(View.VISIBLE); loginUser(); } }); } public void RegisterOnClick (View view) { signUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent_reg = new Intent(login.this, register.class); startActivity(intent_reg); finish(); } }); } }
package com.example.securityapp; public class Users { String username; String job; String email; String office; public Users() { } public Users(String username, String job, String email, String office) { this.username = username; this.job = job; this.email = email; this.office = office; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getOffice() { return office; } public void setOffice(String office) { this.office = office; } }
Try with this...试试这个...
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
// User is signed in
} else {
// No user is signed in
}
take a look at this method:看看这个方法:
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
// Do something here...
} else {
// If sign in fails, display a message to the user..
}
}
});
When signIn enter in the method onComplete you can use当登录进入 onComplete 方法时,您可以使用
FirebaseUser user = mAuth.getCurrentUser()
FirebaseUser 用户 = mAuth.getCurrentUser()
in order to get any information do you want about the user such as email, name and so on.为了获得您想要的有关用户的任何信息,例如电子邮件、姓名等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.