在我的项目中,用户只能通过输入用户名和密码来登录。 但是我坚持忘记密码选项。在我的代码中,我使用addListenerForSingleValueEvent方法进行注册。
我使用DatabaseReference在firebase auto中存储用户信息
users = FirebaseDatabase.getInstance()。getReference(“ Users”);
this users work like this screen shot
此代码如下:
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//replace get method 27.01.19
if (dataSnapshot.child(user.getUserName()).exists()) {
Toast.makeText(MainActivity.this, "User Already Exist", Toast.LENGTH_SHORT).show();
} else {
users.child(user.getUserName())
.setValue(user);
Toast.makeText(MainActivity.this, "Registration Successfully", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
我为注册This is the screen shot of signup设置了提醒对话框
////////for registration dialog////////
private void showDialog() {
AlertDialog.Builder alertdialog = new AlertDialog.Builder(MainActivity.this);
alertdialog.setTitle("Sign Up");
alertdialog.setMessage("Please fill the credentials");
LayoutInflater layoutInflater = this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.signup, null);
editNewEmail = view.findViewById(R.id.newemail);
editNewUser = view.findViewById(R.id.newUsername);
editNewPassword = view.findViewById(R.id.newpassword);
alertdialog.setView(view);
alertdialog.setIcon(R.drawable.ic_account_circle_black_24dp);
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertdialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final User user = new User(editNewUser.getText().toString()
, editNewPassword.getText().toString()
, editNewEmail.getText().toString());
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//replace get method 27.01.19
if (dataSnapshot.child(user.getUserName()).exists()) {
Toast.makeText(MainActivity.this, "User Already Exist", Toast.LENGTH_SHORT).show();
} else {
users.child(user.getUserName())
.setValue(user);
Toast.makeText(MainActivity.this, "Registration Successfully", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
dialog.dismiss();
}
});
alertdialog.show();
}
我的登录方法在Screenshot of signIn
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(editUser.getText().toString()).exists()) {
User user = dataSnapshot.child(editUser.getText().toString()).getValue(User.class);
if (user.getPassword().equals(editPassword.getText().toString())) {
Intent intent = new Intent(MainActivity.this, Home.class);
Common.currentUser = user;
startActivity(intent);
finish();
} else
Toast.makeText(MainActivity.this, "Password Wrong", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this, "Please Register", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
但是我坚持使用“忘记密码”选项,该选项可以通过发送电子邮件地址来帮助用户忘记密码并重新发送密码This is the demo screen shot of forget password
我了解如果我知道如何在Firebase中存储电子邮件 身份验证,然后我将对此进行管理。 我尝试存储电子邮件地址表单注册表单,但是我不知道如何通过Firebase中的singnUp表单存储身份验证中的电子邮件。
我对
有清晰的概念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
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
但是我需要知道如何在不使用createUserWithEmailAndPassword的情况下以Firebase身份验证存储电子邮件