While registering for user, it is getting failed and gives exception in logcat as
" I/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq#1a8a2b0 "
All the correct and updated dependencies are implemented.....
While clicking on signup button on android studio it is getting failed and gives the message as "Failed while registering, An internal error has occurred [7 :]"
public class RegistrationActivity extends AppCompatActivity
{
private EditText email;
private EditText password;
private TextView signIn;
private Button registerBtn;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
firebaseAuth = FirebaseAuth.getInstance();
progressDialog = new ProgressDialog(this);
email = findViewById(R.id.email_register);
password = findViewById(R.id.password_register);
registerBtn = findViewById(R.id.btn_register);
signIn = findViewById(R.id.sign_in_txt);
registerBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
String myEmail = email.getText().toString().trim();
String myPass = password.getText().toString().trim();
if (TextUtils.isEmpty(myEmail))
{
email.setError("This field is required.");
return;
}
if (TextUtils.isEmpty(myPass))
{
password.setError("This field is required.");
return;
}
progressDialog.setMessage("Registering please wait...");
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(myEmail, myPass).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>()
{
#Override
public void onComplete(#NonNull Task<AuthResult> task)
{
if (task.isSuccessful())
{
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
Toast.makeText(getApplicationContext(), "Successfully Registered...", Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
else
{
Toast.makeText(getApplicationContext(), "Failed while registering..." + task.getException().getMessage(), Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
});
signIn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
}
}
Related
I am trying to create a register activity to allow a user to register however I keep getting the error:
Cannot resolve method 'setTimestampsInSnapshotsEnabled' in 'Builder'"
It's only the section which has the setTimestampsInSnapshotsEnabled that is giving me the issue. Do I need an implementation?
Here is my Java code:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener
{
private static final String TAG = "RegisterActivity";
//widgets
private EditText mEmail, mPassword, mConfirmPassword;
private ProgressBar mProgressBar;
//vars
private FirebaseFirestore mDb;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mEmail = (EditText) findViewById(R.id.input_email);
mPassword = (EditText) findViewById(R.id.input_password);
mConfirmPassword = (EditText) findViewById(R.id.input_confirm_password);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
findViewById(R.id.btn_register).setOnClickListener(this);
mDb = FirebaseFirestore.getInstance();
hideSoftKeyboard();
}
/**
* Register a new email and password to Firebase Authentication
* #param email
* #param password
*/
public void registerNewEmail(final String email, String password){
showDialog();
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
if (task.isSuccessful()){
Log.d(TAG, "onComplete: AuthState: " + FirebaseAuth.getInstance().getCurrentUser().getUid());
//insert some default data
User user = new User();
user.setEmail(email);
user.setUsername(email.substring(0, email.indexOf("#")));
user.setUser_id(FirebaseAuth.getInstance().getUid());
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build();
mDb.setFirestoreSettings(settings);
FirebaseFirestoreSettings newUserRef = mDb
.collection(getString(R.string.collection_users))
.document(FirebaseAuth.getInstance().getUid());
newUserRef.set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
hideDialog();
if(task.isSuccessful()){
redirectLoginScreen();
}else{
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "Something went wrong.", Snackbar.LENGTH_SHORT).show();
}
}
});
}
else {
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "Something went wrong.", Snackbar.LENGTH_SHORT).show();
hideDialog();
}
// ...
}
});
}
/**
* Redirects the user to the login screen
*/
private void redirectLoginScreen(){
Log.d(TAG, "redirectLoginScreen: redirecting to login screen.");
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private void showDialog(){
mProgressBar.setVisibility(View.VISIBLE);
}
private void hideDialog(){
if(mProgressBar.getVisibility() == View.VISIBLE){
mProgressBar.setVisibility(View.INVISIBLE);
}
}
private void hideSoftKeyboard(){
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_register:{
Log.d(TAG, "onClick: attempting to register.");
//check for null valued EditText fields
if(!isEmpty(mEmail.getText().toString())
&& !isEmpty(mPassword.getText().toString())
&& !isEmpty(mConfirmPassword.getText().toString())){
//check if passwords match
if(doStringsMatch(mPassword.getText().toString(), mConfirmPassword.getText().toString())){
//Initiate registration task
registerNewEmail(mEmail.getText().toString(), mPassword.getText().toString());
}else{
Toast.makeText(RegisterActivity.this, "Passwords do not Match", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(RegisterActivity.this, "You must fill out all the fields", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
}
If I look at the current documentation for FirebaseFirestoreSettings.Builder, I don't see any setTimestampsInSnapshotsEnabled method. So that probably explains the error message: you're trying to call a method that doesn't exist.
My guess is that your code was meant for an older version of the SDK when the method did exist. Since it doesn't exist anymore now, you either have to use the version of the SDK that the code was made for, or remove the call.
Update: it looks like timestampsInSnapshotsEnabled was removed in version 22 of the Android SDK for Firestore back in October 2020.
"I just started learning Firebase"
I did set callbacks in PhoneAuthOptions yet I keep getting the error:
java.lang.NullPointerException: You must specify callbacks on your PhoneAuthOptions. Please call #setCallbacks().
This is my first activity, from which I am getting user's phone number and passing it to second activity:
public class SendOTPActivity extends AppCompatActivity {
private EditText mPhoneNumber;
private Button mBtnSendOtp;
String phoneNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_o_t_p);
mPhoneNumber = findViewById(R.id.phone_number);
mBtnSendOtp = findViewById(R.id.btn_send_otp);
phoneNumber = mPhoneNumber.getText().toString();
mBtnSendOtp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),VerifyOTPActivity.class);
intent.putExtra("PHONE_NUMBER",phoneNumber);
startActivity(intent);
mBtnSendOtp.setEnabled(false);
}
});
}
}
This is second activity (Verification Activity):
public class VerifyOTPActivity extends AppCompatActivity {
private EditText mOtpCode;
private Button mBtnVerifyOtp;
private TextView textView;
private ProgressBar progressBar;
private String phoneNumber;
private String code;
private FirebaseAuth mAuth;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_o_t_p);
mAuth = FirebaseAuth.getInstance();
mOtpCode = findViewById(R.id.otp_code);
mBtnVerifyOtp = findViewById(R.id.btn_verify_otp);
textView = findViewById(R.id.text_view);
progressBar = findViewById(R.id.progress_bar);
phoneNumber = getIntent().getStringExtra("PHONE_NUMBER");
code = mOtpCode.getText().toString();
textView.setText("An OTP has been sent to +91 "+phoneNumber);
startPhoneNumberVerification(phoneNumber);
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
#Override
public void onVerificationCompleted(#NonNull PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
if (code!=null){
progressBar.setVisibility(View.VISIBLE);
}
signInWithPhoneAuthCredential(phoneAuthCredential);
}
#Override
public void onVerificationFailed(#NonNull FirebaseException e) {
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
Toast.makeText(VerifyOTPActivity.this, "Provided phone number might not be correct", Toast.LENGTH_SHORT).show();
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
Toast.makeText(VerifyOTPActivity.this, "Some error has occurred please try again later!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCodeSent(#NonNull String s, #NonNull PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = s;
mResendToken = token;
}
};
}
#Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) {
mAuth.signInWithCredential(phoneAuthCredential)
.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
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
FirebaseUser user = task.getResult().getUser();
// Update UI
} else {
// Sign in failed, display a message and update the UI
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(VerifyOTPActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
// The verification code entered was invalid
}
}
}
});
}
private void startPhoneNumberVerification(String phoneNumber){
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber("+91"+phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(mCallbacks)
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
In onCreate you call startPhoneNumberVerification(phoneNumber); before you create your OnVerificationStateChangedCallbacks. Try to move startPhoneNumberVerification(phoneNumber); below the block where you create your callback. That should get rid of the NullPointerException.
first i'm sorry of my bad english , i have a problem with my code , when i want to query and check if the user that sign in is in the database or not , and also check of his password and if he tourist or tour guide and when i debug the program , the first statement is passed and the second which is check of the password , he never passed and start the next page , it say that the password is wrong , how i fix this problem pleass :(
TextView show7, signup_btn_label;
ImageButton imagebuttonn1;
EditText tourg_email_address, tourg_password;
Button btnn_login;
private FirebaseAuth mAuth1;
private DatabaseReference jLoginDatabase;
private FirebaseDatabase fDatabase;
ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logintourguide);
//initialise mProgressDialog
mProgressDialog = new ProgressDialog(Logintourguide.this);
show7 = (TextView) findViewById(R.id.forgot_password);
show7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), Forgetpassword.class);
startActivity(i);
}
});
imagebuttonn1 = findViewById(R.id.imagebuttonn);
imagebuttonn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
signup_btn_label = (TextView) findViewById(R.id.signup_btn_label);
signup_btn_label.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), activity_signup_tourguide.class);
startActivity(i);
}
});
tourg_email_address = (EditText) findViewById(R.id.tourg_email_address);
tourg_password = (EditText) findViewById(R.id.tourg_password);
btnn_login = (Button) findViewById(R.id.btnn_login);
mAuth1 = FirebaseAuth.getInstance();
btnn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String emailPattern = "[a-zA-Z0-9._-]+#[a-z]+\\.+[a-z]+";
final String email = tourg_email_address.getText().toString().trim();
final String pass = tourg_password.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(Logintourguide.this, "Please enter your email", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass)) {
Toast.makeText(Logintourguide.this, "Please enter your password", Toast.LENGTH_LONG).show();
return;
}
if (tourg_password.length() < 8) {
Toast.makeText(Logintourguide.this, "password must be 8 or long", Toast.LENGTH_LONG).show();
return;
}
if (!email.matches(emailPattern)) {
Toast.makeText(Logintourguide.this, "invalid email address", Toast.LENGTH_LONG).show();
return;
}
//show dialog
mProgressDialog.show();
//set content view
mProgressDialog.setContentView(R.layout.progress_dialog);
//set transparent background
mProgressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
///FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
// DatabaseReference rootRef = FirebaseDatabase.getInstance()
final DatabaseReference uidRef = FirebaseDatabase.getInstance().getReference("Users");
final Query checkUser = uidRef.orderByChild("email").equalTo(email);
checkUser.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
if (pass.equals(dataSnapshot.child("password").getValue())) {
if (dataSnapshot.child("sign up as").getValue().equals("Tour Guide")) {
String passwordfromDB = (String) dataSnapshot.child("password").getValue(String.class);
String emailfromDB = (String) dataSnapshot.child("email").getValue(String.class);
String fullNamefromDB = (String) dataSnapshot.child("fullName").getValue(String.class);
String phoneNumberfromDB = (String) dataSnapshot.child("phoneNumber").getValue(String.class);
//put it iin that intent so we can pass it to tourguide class
Intent intent = new Intent(getApplicationContext(), TourGuide_Profile.class);
intent.putExtra("full name", fullNamefromDB);
intent.putExtra("email", emailfromDB);
intent.putExtra("phone No", phoneNumberfromDB);
intent.putExtra("password", passwordfromDB);
mProgressDialog.dismiss();
Toast.makeText(Logintourguide.this, "welcome back tour guide!",
Toast.LENGTH_SHORT).show();
startActivity(intent);
} else {
mProgressDialog.dismiss();
Toast.makeText(Logintourguide.this, "Sorry ! You are not authorized to access this application!",
Toast.LENGTH_SHORT).show();
}
} else {
mProgressDialog.dismiss();
Toast.makeText(Logintourguide.this, "wrong password",
Toast.LENGTH_SHORT).show();
}
} else {
mProgressDialog.dismiss();
Toast.makeText(Logintourguide.this, "no such user exist !",
Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("Tag", databaseError.getMessage());
}
});
// uidRef.addListenerForSingleValueEvent(valueEventListener);
// checkUser.addListenerForSingleValueEvent(valueEventListener);
}
});
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm making a shopping App and I've almost finished the App. But I wanted to add the Forget password feature after all. I could've done it using FirebaseAuth but for registration and login, I haven't used FirebaseAuth.
My user details are going straight to the firebase database and are being saved in the Users table. Since I'm new to android app developing I don't know how to implement my app with forget password feature.
This is my database structure :
This is my RegisterActivity:
public class RegisterActivity extends AppCompatActivity {
private Button CreateAccountButton;
private EditText InputUsername, InputPhoneNumber, InputPassword, InputRePassword, InputEmail, InputName;
private ProgressDialog loadingBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
CreateAccountButton = (Button) findViewById(R.id.register_btn);
InputUsername = (EditText) findViewById(R.id.register_username);
InputPhoneNumber = (EditText) findViewById(R.id.register_Number);
InputPassword = (EditText) findViewById(R.id.register_password);
InputRePassword = (EditText) findViewById(R.id.register_repassword);
InputEmail = (EditText) findViewById(R.id.register_email);
InputName = (EditText) findViewById(R.id.register_name);
loadingBar = new ProgressDialog(this);
CreateAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CreateAccount();
}
});
}
private void CreateAccount() {
String name = InputName.getText().toString();
String username = InputUsername.getText().toString();
String phone = InputPhoneNumber.getText().toString();
String password = InputPassword.getText().toString();
String repassword = InputRePassword.getText().toString();
String email = InputEmail.getText().toString();
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "Please enter your Name", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(username)) {
Toast.makeText(this, "Please enter your Username", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(phone)) {
Toast.makeText(this, "Please enter your Phone Number", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "Please enter your Password", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(repassword)) {
Toast.makeText(this, "Please re-enter your Password", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(email)) {
Toast.makeText(this, "Please enter your Email", Toast.LENGTH_SHORT).show();
} else {
loadingBar.setTitle("Creating Account");
loadingBar.setMessage("Please wait.");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
ValidatephoneNumber(username, name, phone, password, repassword, email);
}
}
private void ValidatephoneNumber(final String username, final String name, final String phone, final String password, final String repassword, final String email)
{
final DatabaseReference RootRef;
RootRef = FirebaseDatabase.getInstance().getReference();
RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if(!(dataSnapshot.child("Users").child(username).exists()))
{
HashMap<String, Object> userdataMap = new HashMap<>();
userdataMap.put("username", username);
userdataMap.put("name", name);
userdataMap.put("phone", phone);
userdataMap.put("password", password);
userdataMap.put("email", email);
RootRef.child("Users").child(username).updateChildren(userdataMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(RegisterActivity.this, "Your account has been created!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
}
else
{
loadingBar.dismiss();
Toast.makeText(RegisterActivity.this, "Connection Error. Please try again.", Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
Toast.makeText(RegisterActivity.this,"This Username already exists! Try another Username", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError)
{
}
});
}
}
And here's my LoginActivity:
public class LoginActivity extends AppCompatActivity {
private EditText InputUsername, InputPassword;
private Button LoginButton;
private ProgressDialog loadingBar;
private String parentDbName = "Users";
private CheckBox chkBoxRememberMe;
private TextView AdminLink, UserLink, ForgetPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton = (Button) findViewById(R.id.main_login_btn);
InputUsername = (EditText) findViewById(R.id.login_username);
InputPassword = (EditText) findViewById(R.id.password);
AdminLink = (TextView) findViewById(R.id.adminPanel);
UserLink = (TextView) findViewById(R.id.userLogin);
loadingBar = new ProgressDialog(this);
chkBoxRememberMe = (CheckBox) findViewById(R.id.remember_me_chkb);
ForgetPassword = (TextView) findViewById(R.id.forgetPassword);
Paper.init(this);
LoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
LoginUser();
}
});
AdminLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
LoginButton.setText("Login Admin");
AdminLink.setVisibility(View.INVISIBLE);
UserLink.setVisibility(View.VISIBLE);
parentDbName = "Admins";
}
});
UserLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
LoginButton.setText("Login");
AdminLink.setVisibility(View.VISIBLE);
UserLink.setVisibility(View.INVISIBLE);
parentDbName = "Users";
}
});
ForgetPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, ForgotPassowrdActivity.class));
}
});
}
private void LoginUser() {
String username = InputUsername.getText().toString();
String password = InputPassword.getText().toString();
if (TextUtils.isEmpty(username))
{
Toast.makeText(this, "Please enter your Username", Toast.LENGTH_SHORT).show();
}
else if (TextUtils.isEmpty(password))
{
Toast.makeText(this, "Please enter your Password", Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle("Logging in to the Account");
loadingBar.setMessage("Please wait.");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
AllowAccessToAccount(username, password);
}
}
private void AllowAccessToAccount(final String username, final String password)
{
if(chkBoxRememberMe.isChecked())
{
Paper.book().write(Prevelant.userUsernameKey, username);
Paper.book().write(Prevelant.userPasswordKey,password);
}
final DatabaseReference RootRef;
RootRef = FirebaseDatabase.getInstance().getReference();
RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.child(parentDbName).child(username).exists())
{
Users usersData = dataSnapshot.child(parentDbName).child(username).getValue(Users.class);
if(usersData.getUsername().equals(username))
{
if(usersData.getPassword().equals(password))
{
if(parentDbName.equals("Admins"))
{
Toast.makeText(LoginActivity.this, "Welcome Admin, You've logged in successfully!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(LoginActivity.this, AdminCategoryActivity.class);
startActivity(intent);
}
else if(parentDbName.equals("Users"))
{
Toast.makeText(LoginActivity.this, "Logged in successfully!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
Prevelant.currentOnlineUser = usersData;
startActivity(intent);
}
}
else
{
Toast.makeText(LoginActivity.this, "This Password is incorrect!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
}
else
{
Toast.makeText(LoginActivity.this, "This Username is not registered!", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError)
{
}
});
}
}
Can I please know how to implement my code for forget password feature? I really appreciate your help!
This comes inbuilt with firebase:
FirebaseAuth.getInstance().sendPasswordResetEmail("user#example.com")
I am trying to make a Firebase Chat application and I can't seem to find the reason why I get the error when I am creating users. In the Firebase Console, Sign-In Method is Enabled on Email/Password.
I have tried:
-Introducing users from the Firebase console, that works
-Introducing legit credentials, so I won't raise up invalid password, username, email errors
-I tried using the code provided by the Android Assistant Firebase, the same error is given to me
Could someone point where I may be wrong?
public class RegisterActivity extends AppCompatActivity {
private TextInputLayout mDisplayName;
private TextInputLayout mEmail;
private TextInputLayout mPassword;
private Button mCreateBtn;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
mDisplayName = (TextInputLayout) findViewById(R.id.reg_display_name);
mEmail = (TextInputLayout) findViewById(R.id.reg_email);
mPassword = (TextInputLayout) findViewById(R.id.reg_password);
mCreateBtn = (Button) findViewById(R.id.reg_create_btn);
mCreateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String display_name = mDisplayName.getEditText().getText().toString();
String email = mEmail.getEditText().getText().toString();
String password = mPassword.getEditText().getText().toString();
register_user(display_name, email, password);
}
});
}
private void register_user(String display_name, String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
else {
Toast.makeText(RegisterActivity.this, "You got some error with creating the new user.", Toast.LENGTH_LONG).show();
}
}
});
}
}
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(), Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
}
});
Try this one I think you have forgotten addOnCompleteListener(Executor executor, OnCompleteListener listener), the Executor determines the thread that will be used to invoke the listener.