I've been struggling about the change password functionality of my app. I am confused on how I can compare the current pass and store the new password thru firebase database.
Please don't be harsh on me on comments, educate me please. I did research first before asking and tried several times. Most of the tutorials I found are about updating data while clicking an item in listview. The reason why I use db to store users is because I am going to create 2 keys that identifies if user is student or professor.I just want to ask help how am I able to implement change of password.
ChangePassAccount.class
public class ChangePassAccount extends AppCompatActivity {
Button btnSave;
EditText cpass, npass;
String pass, newpass;
DatabaseReference dbPDF;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_change_pass_account);
dbPDF = FirebaseDatabase.getInstance().getReference("users").child("password");
cpass = (EditText)findViewById(R.id.currentpass);
npass = (EditText)findViewById(R.id.newpass);
btnSave = (Button) findViewById(R.id.btnsave);
btnSave.setBackgroundResource(R.drawable.button);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pass = cpass.getText().toString();
newpass = npass.getText().toString();
}
});
}
}
i'll suggest you to using firebase auth to manage User Login or Changes password etc.. So maybe you only has to store user Data by UserInfo
this is a sample from Firebase-Manage User to change the user password
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String newPassword = "SOME-SECURE-PASSWORD";
user.updatePassword(newPassword)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User password updated.");
}
}
});
This is not a very technical answer but I ran into this problem in iOS recently.
There is a FirebaseAuth method named 'reauthenticate' that can be found here. This takes a credential that you create by asking for the user's password and getting the email from currentUser. If this returns successful you will know that password is correct. After reauthenticated you can call updatePassword with the new password.
As a suggestion you should not need to store the password separately in the database since Firebase will handle all this for you.
Hope this helps out, good luck!
You'd use a Firebase Database transaction for that. This gives you the current value, which you then compare against the old password. If they match, you return the new password.
Something like:
passwordRef.runTransaction(new Transaction.Handler() {
#Override
public Transaction.Result doTransaction(MutableData mutableData) {
String password = mutableData.getValue(String.class);
if (password.equals(oldPassword) {
mutableData.setValue(newPassword);
return Transaction.success(mutableData);
}
}
#Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
Log.d(TAG, "passwordTransaction:onComplete:" + databaseError);
}
});
First you need to read the current password using single value event listener
pass = cpass.getText().toString();
newpass = npass.getText().toString();
dbPDF.addValueEventListener(addListenerForSingleValueEvent(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
String password = dataSnapshot.getValue(String.class);
if(password.equals(pass)){
dbPDF.setValue(newpass);
}else{
// show wrong pass dialog
}
// ...
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
Also make sure dbref is correct
String username = "helpmepls";
dbPDF = FirebaseDatabase.getInstance().getReference("users").child(username).child("password");
Related
Firebase user.currentuser() is always null in oncreate. I want to implement this access controll so that only a user called a credit controller can add items to the recyclerview using the floating action button, while other users can only see the items added.
I want the floating action button's visibility to be determined as soon as the activity is open. Is there any other way to achieve this?
I used the reveal button to achieve this but it's a very bad user experience to click it every time you want to go into this activity.
This is my code below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collections);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
userEmail = user.getEmail();
FloatingActionButton buttonAddNote = findViewById(R.id.button_add_note);
Button revealButton = findViewById(R.id.revealButton);
revealButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(userEmail.equals("creditcontroller#outlook.com")){
buttonAddNote.setVisibility(View.VISIBLE);
revealButton.setVisibility(View.GONE);
}
else revealButton.setVisibility(View.GONE);
}
});
buttonAddNote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(CollectionsActivity.this, AddEditNoteActivity.class);
intent.putExtra("USER_EMAIL", userEmail);
startActivityForResult(intent, ADD_NOTE_REQUEST);
}
});
Unfortunately I can't upload images yet but the reveal button is just a button that's centered in the activity. Upon clicking it its visibility is set to gone and everything else is set to visible
The code is working but the user experience is pretty bad.
Firebase calls are asynchronous which means that you can't get results instantly.
You should add a callback to your calls.
FirebaseUser user = firebaseAuth.getCurrentUser();
userEmail = user.getEmail();
database.child(userEmail).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("email").getValue(String.class);
Log.d(TAG, "email: " + email);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
The code is just an example. It may not work in your case, because you must know the structure of your nodes.
You can read more in the Firebase documentation: https://firebase.google.com/docs/database/android/read-and-write
I'm developing a social media application. In that, I need to check whether the user already exists or not when he login with otp (like in WhatsApp).
I have found similar questions like this in stackoverflow, but none of the questions have correct answer. Help me!
after a research, I found this answer.
In firebase, each phone number have a unique user id. So, if a user signs up from different devices, or reinstalls the application with a same phone number, he/she will get the same user Id. So with that, we can check our database like this:
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser user = task.getResult().getUser();
String uid = user.getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference docRef = db.collection("users").document(uid);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(final DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
//redirect to home page
}
else{
//redirect to sign up page
}
}
Hope this will help someone :)
You can just use the admin SDK and lookup the user by phone number:
https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
admin.auth().getUserByPhoneNumber(phoneNumber)
.then(function(userRecord) {
// User found.
})
.catch(function(error) {
// User not found.
});
If you try to create an user that already exists, Firebase will throw you an exception. You can handle that exception to do what you want.
Hope this helps!
First store the Firebase Phone Auth in FirebaseDatabase after every successful registration, then add a check when user try and enter the phone number if it already exist in database, If present then you got your answer otherwise register the user.
I hope the concept is clear. Will update you with the code soon.
#Soorya. If you want to make your application like WhatsApp then you have to simply authenticate the user using phone number and OTP. When the user gets verified successfully then check whether user details are available in Firebase Database or FireStore. If available then simply redirect the user to home screen otherwise redirect the user to the registration page. For that refer below code snippet.
mFirebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
goToNextActivity(((AuthResult) task.getResult()).getUser());
return;
}
}
});
private void goToNextActivity(FirebaseUser firebaseUser) {
FirebaseDatabase
.getInstance()
.getReference()
.child("your table name")
.child("user id details")
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot == null || dataSnapshot.getChildrenCount() <= 0) {
goToRegisterDetails(firebaseUser);
return;
}
User user = (User) dataSnapshot.getValue(User.class);
if (user == null
|| user.getUser_name() == null
|| user.getUser_name().isEmpty()
|| user.getUser_mobile() == null
|| user.getUser_mobile().isEmpty()) {
goToRegisterDetails(firebaseUser);
} else {
goToMainActivity(user);
}
}
public void onCancelled(DatabaseError databaseError) {
OTPVerificationActivity.this.hideProgressDialog();
Logger.e(databaseError.getMessage());
}
});
}
I hope that you will get your answer. You can easily achieve that you want to do in your application.
I am creating an app and part of it's features in user interaction. I want to store user comments on a post and I do not want to limit the amount of comments any one user can make. I do this by assigning a random number as the .setValue of the database entry.
With this implementation, whenever the comment is sent to the database it is stuck in an infinite loop where it will continually update with the same string entered in the text box but it will constantly generate new posts.
Full code;
sendComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
commitPost();
finish();
}
});
}
private void commitPost() {
commentProgress.setMessage("Posting");
commentProgress.show();
final String commentInput = commentText.getText().toString().trim();
if(TextUtils.isEmpty(commentInput)){
Snackbar.make(commentLayout,"You haven't finished your post yet",Snackbar.LENGTH_SHORT);
}else{
commentDB.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String uniqueId = UUID.randomUUID().toString();
commentDB.child(postID).child("Comments").child(uniqueId).setValue(commentInput);
commentProgress.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
The problem lies in the commentDB.addValueEventListener
The problem is that you set a listener for data changes and you also change data inside it (so it is called).
You don't need the listener, just add:
else {
String uniqueId = UUID.randomUUID().toString();
commentDB.child(postID)
.child("Comments")
.child(uniqueId)
.setValue(commentInput);
commentProgress.dismiss();
}
Users are created using Email and Password. This is how I do the Sign-up:
mSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mEmailStr = removeSpaces(mEmail.getText().toString());
mPasswordStr = mPassword.getText().toString();
mUsernameStr = mUsername.getText().toString();
mIsSgl = mSglCheckBox.isChecked();
mUsernameStr=mUsername.getText().toString();
final User mUser = new User();
mUser.setEmail(mEmailStr);
mUser.setPassword(mPasswordStr);
mUser.setIsSgl(mIsSgl);
mUser.setStudyGroupName(mStudyGroupName);
mUser.setUsername(mUsernameStr);
FirebaseAuth.getInstance().createUserWithEmailAndPassword(mUser.getEmail(), mUser.getPassword()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getActivity(), "Sucsses", Toast.LENGTH_SHORT).show();
generateUser(mUser);
startActivity(new Intent(getActivity(), MainActivity.class));
} else {
Toast.makeText(getActivity(), "not Sucsses", Toast.LENGTH_SHORT).show();
}
}
});
}
});
This is how I push the data into database:
public void generateUser(User user)
{
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference users;
if(user.getIsSgl())
{
users = database.getReference(user.getStudyGroupName()).child("SGL");
}
else
{
users = database.getReference(user.getStudyGroupName()).child("Student");
}
users.push().setValue(user);
}
This is how I Sign-in:
mSignin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mEmailStr = SignupActivityFragment.removeSpaces(mEmail.getText().toString());
mPasswordStr = mPassword.getText().toString();
mAuth.signInWithEmailAndPassword(mEmailStr, mPasswordStr).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()){
FirebaseDatabase database = FirebaseDatabase.getInstance();
// thats not worked for me
database.getReference("StudyGroups").child("Student").orderByChild("email").equalTo(mEmailStr).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
userKey = childSnapShot.getKey();
}
Toast.makeText(getContext(),"Userkey: " + userKey,Toast.LENGTH_LONG).show();
Log.v("himaAbousalem",userKey);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Toast.makeText(getActivity(), "Sucsses ", Toast.LENGTH_SHORT).show();
startActivity (new Intent(getActivity(),Controller.class));
}else {
Toast.makeText(getActivity(), "not sucsses", Toast.LENGTH_SHORT).show();
}
}
});
}
});
I want to query the database so that when a user signs-in by Email and password, it returns all the data of that user from the database.
How can I make the key of userId in Auth equal to the userId in database and how do I use that feature?
tl;dr - In this case, store each user using their associated uid generated by Fireabase Auth instead of using a push ID.
In your situation the challenge with using a push ID to store info specific to each user is that when a user signs in you don't know the push ID you used when you first stored their info. To find a user each time they sign in you would have to search through every user in your database until you find a matching email/password to retrieve their correct profile information - the more users you have, the longer it would take to do the search. One alternative, which would probably be faster, is to use Firebase Authentication to create users and the Firebase Database to store any user specific information.
When you create a new user using Firebase Authentication it will assign a unique user id to the user that will be the same throughout the lifetime of the user. You then use the unique user id generated by Firebase Auth instead of a push ID to store user info in the database. The next time a user signs in you get the user's uid from Firebase Auth and use it to query the database to get that user's information.
Check the guide for how to create a password-based user and how to sign a user in using Firebase Auth: Create a password-based account
In order to use the unique uid generated by Firebase Auth I suggest a few changes to your code.
Update database structure
I suggest you update your database structure by adding a new location (maybe "allUsers") for use when you create/sign in users. Right now it looks like your are breaking up students into groups. If you need to keep this structure, for reasons beyond authentication, you can use it along with my suggestion. The reason for a single location which stores all users is that you need a definite location to query when a user signs in. When using Firebase Auth, without a single location which stores all users there is no way to tell what group a user belongs to when they first sign in. You would have to check every group in order to find a match and that may take a long time. Having a location which stores all users solves that problem. Also, the query for retrieving user information from that single location is much simpler. If you do need to keep a user's information in multiple places just be sure to update their information in both places if any changes occur.
Create a class variable used to distinguish between create user and sign in existing user.
If you use the same Activity to create a new user and sign in an existing user then create a boolean variable to make a distinction between when a new user is being created and when an existing user is signing in. It will be used later in the AuthStateListener. If you handle user creation in a separate activity from general sign in then you shouldn't need this variable because each activity would have a separate AuthStateListener.
boolean mIsNewUser = false;
Move the call to generateUser() from the create user completion listener to an AuthStateListener. Also move your database query from the sign in completion listener to the AuthStateLisener
Whenever you create a user successfully they will automatically be signed in too. So, if you move your call to generateUser() from the createUserWithEmailAndPassword OnCompleteListener to your AuthStateListener you can get access to the created user's uid. When signing an existing user move your database query to the AuthStateListener as well, again so we can access the user's uid. I'm also going to create a helper method for the database query called getExistingUserInfo. As an FYI, the onComplete() callback in the OnCompleteListeners for creating and signing in users gives you access to an AuthResult which according to the API has a method for returning the current user but the documentation says to access user information in the AuthStateListener.
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
if(mIsNewUser){
//call to generate user using Uid instead of pushID
generateUser(mUser, user.getUid());
} else{
getExistingUserInfo(user.getUid());
}
startActivity(new Intent(getActivity(), MainActivity.class));
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
// ...
}
}
#Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
Update your generateUser() helper method to use the uid instead of a push ID:
I'm going to assume you want to keep you existing database structure and add the single location for all users as suggested. Based on this I've made a couple of changes to the write operation you were using in generateUser(). Mainly, instead of using setValue() to write to the database I'm using updateChildren(). By using updateChildren() we can take advantage of Firebase's ability to do atomic updates. This will allow us to write to the appropriate student group location and the location storing all users simultaneously. By taking this approach if the write operation to either location fails neither location will be updated. This way you can be certain if a student is added to a group they will also be listed in the allUsers location.
private void generateUser(User user, String uid)
{
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
String userType;
String allusers = "allUsers/" + uid;
Map<String, Object> newUserUpdate = new HashMap<>();
if (user.getUsername() != null) {
if (user.isSgl()) {
userType = user.getStudyGroupName() + "/" + "SGL" + "/" + uid;
} else {
userType = user.getStudyGroupName() + "/" + "Student" + "/" + uid;
}
newUserUpdate.put(userType, user.serialize());
newUserUpdate.put(allusers, user.serialize());
database.updateChildren(newUserUpdate);
}
}
Update database query to use new location which stores all users
As I mentioned above, by creating a single location for all users you can reduce the complexity of the query used to find a user's info when they sign in. Again, if you need to store users by group you can keep that but be sure to update both locations if a users info changes.
public void getExistingUserInfo(String uid){
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference("allUsers").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//get user data from dataSnapshot
Toast.makeText(getContext(),"Userkey: " + userKey,Toast.LENGTH_LONG).show();
Log.v("himaAbousalem",userKey);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Set the variable used to distinguish between existing user sign in and new user creation in the create user completion listener
FirebaseAuth.getInstance().createUserWithEmailAndPassword(mUser.getEmail(), mUser.getPassword()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()) {
//set boolean used in the AuthListener
mIsNewUser = true;
Toast.makeText(getActivity(), "Sucsses", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "not Sucsses", Toast.LENGTH_SHORT).show();
}
}
});
So I have the following code that I got from the firebase documentation (which I implemented in my app already and it's working fine):
Firebase ref = new Firebase("https://myapp.firebaseio.com");
ref.createUser("bobtony#firebase.com", "correcthorsebatterystaple", new Firebase.ValueResultHandler<Map<String, Object>>() {
#Override
public void onSuccess(Map<String, Object> result) {
System.out.println("Successfully created user account with uid: " + result.get("uid"));
}
#Override
public void onError(FirebaseError firebaseError) {
// there was an error
}
});
after I create a user it prints on the console its uid. However, when I enter in my myapp.firebaseio.com there is nothing there.. So I have some questions:
Where does firebase stores this new user created?
How can I add some custom fields? (this functions uses just email and password) i.e Username
So, What I have tried to do was inside the onSuccess() I used ref.push() some values to myapp.firebaseio.com but then .. how can I check if the users uid created by the createUser() is the same as the one who I pushed? (the id's are differente!)
I hope my text it's clear, if isn't asked and I can try to explain again!
Thanks a bunch!
User information is not stored inside your Firebase database. For anonymous and OAuth users, no information is stored anywhere. The information for email+password users is kept in a separate database that you don't have access to. The email+password users are visible in the Login & Auth tab of your dashboard of course, just not in your database.
If you want to store user information in your own Firebase database, you have to store it there yourself when you create or authenticate the user. There is a section on storing user data in the Firebase documentation that shows how to do this.
One advantage of having to store the information yourself, is that you get to determine exactly what is and what isn't stored.
As Frank said; no user information is automatically put in the firebase itself on creating a user (have a look in Login&Auth in the dashboard sidebar instead). The new user is not even logged in after creation. This is the code I use to login and put a new user in the firebase when registering:
static void createUser(final String username, final String password) {
final Firebase rootRef = new Firebase("YOUR_FIREBASE_URL");
rootRef.createUser(
username,
password,
new Firebase.ResultHandler() {
#Override
public void onSuccess() {
// Great, we have a new user. Now log them in:
rootRef.authWithPassword(
username,
password,
new Firebase.AuthResultHandler() {
#Override
public void onAuthenticated(AuthData authData) {
// Great, the new user is logged in.
// Create a node under "/users/uid/" and store some initial information,
// where "uid" is the newly generated unique id for the user:
rootRef.child("users").child(authData.getUid()).child("status").setValue("New User");
}
#Override
public void onAuthenticationError(FirebaseError error) {
// Should hopefully not happen as we just created the user.
}
}
);
}
#Override
public void onError(FirebaseError firebaseError) {
// Couldn't create the user, probably invalid email.
// Show the error message and give them another chance.
}
}
);
}
This is working well for me so far. I guess something could go wrong if the connection is interrupted right in the middle of everything (might end up with a user without it's initial info). Don't depend too much on it getting set...
May be previous one deprecated as per Firebase . They are create new concept
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
Log.e("task",String.valueOf(task));
getUserDetailse(auth);
}
}
});
/get user Detailse against FirebaseAuth auth/
public static void getUserDetailse(FirebaseAuth auth)
{
//
auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull final FirebaseAuth firebaseAuth) {
final FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.i("AuthStateChanged", "User is signed in with uid: " + user.getUid());
String name = user.getDisplayName();
String email = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
// The user's ID, unique to the Firebase project. Do NOT use this value to
// authenticate with your backend server, if you have one. Use
// FirebaseUser.getToken() instead.
String uid = user.getUid();
Log.e("user",name+email+photoUrl);
} else {
Log.i("AuthStateChanged", "No user is signed in.");
}
}
});
}
check for detailse