Getting info of user off firebase Android - android

I'm trying to create an app on Android that stores user contact info.
When the user creates a "contact card" to send to another user, I'd like it to display things I stored when registering the user, name, year, number etc.
When I push, firebase creates a unique key, and when I create a FirebaseUser it creates a uid. for ease, I save the uid as part of the user structure.
I'm now attempting to access that user using the uid, unfortunately the uid is not the key.
So, I'm trying to query.
I create a Firebaseuser
currentUser = firebaseAuth.getCurrentUser.getUid.
then I try to query by
DatabaseReference ref = firebase.getinstance().getReference("user);
ref.orderByChild("Uid").equalto(currentUser.getUid());
Is this correct? after I try to add a single valuelistener to obtain the actual values inside this user. Unfortunately this crashes. Can someone point me in the right direction?

You can use the user uid as the key to save the user info like this:
private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();
User user = new User(name, email);
mDatabase.child("users").child(userUid).setValue(user);
Then you can get the user info like this
ValueEventListener userListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get User object and use the values to update the UI
User user= dataSnapshot.getValue(User.class);
// ...
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting User failed, log a message
Log.w(TAG, "loadUser:onCancelled", databaseError.toException());
// ...
}
};
mDatabase.child("users").child(userUid).addValueEventListener(userListener);
If this doesn't work, write the error log on the OP please ;)

Related

How can we delete firebase user if he doesn't verify email?

I want to delete user from firebase authentication and also want to delete data from real-time database if the user doesn't verify email address with in 1 hours. Deleting a user is easy but if doesn't verify in 1 hour then how could I do this? The problem is that firebase is server less.
Write and deploy a scheduled function that periodically:
Queries your database for users who have not verified (you will need a child to record that).
Delete the database record and also delete the user account.
You will need to use the Firebase Admin SDK for both of these steps.
It's so easy! just create a real-time database of the unverified user database. and when the user signup the time will be also registered in the database. So when the apps start it check the unverified users and there you write the if-else statement if the difference between time is greater than 1hour the user will be deleted.
It's so simple as I said :)
Run firebaseAuth.getCurrentUser().isEmailVerified() function inside signInWithEmailAndPassword function to see if user is verified or not. if user is verfied then only create users database otherwise give an exception and break the function, it won't save unverified users data.
firebaseAuth.signInWithEmailAndPassword(Email,Pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
if (firebaseAuth.getCurrentUser().isEmailVerified()){
FirebaseUser user = firebaseAuth.getCurrentUser();
Uid = user.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (!snapshot.hasChild(Uid)){
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(Uid);
HashMap<String,String> userMap = new HashMap<>();
userMap.put("Name","default");
userMap.put("email",Email);
databaseReference.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
//call your function here when user sign in first time after email verification
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
//call your function when user already registered and verified
//and already saved his information to database
}else{
//show your exception here
}
}else{
//show your exception here
}
}
});

UserId returning null

My app is returning me a null UID. At the time of registration, I'm using phone authentication to registered the and getting some custom field from the user which I save under the current user UID in the Firebase database. In the registration activity, it is giving me the UID perfectly, but when I log in the user through credential and on successful login it diverts the user to MainActivity, and it gives me null at that time.
Note
I am logging in the user by matching his/her username and password from the DB which I saved at registration. Is this the issue that I'm not getting UID that I'm not using any Firebase signin method? Rather, I am matching the values from the DB in the login activity and on successful match I logged in the user. Because if there is any other issue it won't give me a UID on Register Activity also, but after login it gives me null at Main Activity and Profile Activity.
Signin Code
FirebaseDatabase.getInstance().getReference().child("Content").child("Profiles").orderByChild("mobile_number").equalTo(mobileNum)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot user : dataSnapshot.getChildren()) {
Profiles profiles = user.getValue(Profiles.class);
if (profiles.getPassword().equals(password)) {
progressDialog.dismiss();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
else {
progressDialog.dismiss();
HelperClass.showSnakbarMsg(rootView, "Username or password did not match. Please try again.");
}
}
}
Code not working
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
String userId = firebaseAuth.getUid();
Log.d("user",userId);
It's returning me null in Main Activity after login.
If I log in by matching values in a Firebase realtime database, do I get the UID of the user or for getting the UID? It is a must to use some Firebase authentication to sign in like (email/password or phone authentication or Gmail sign in, etc.)
After a lot of debugging and going through Firebase documentation, I got to this conclusion that to get the user UID, it is a must to use any Firebase authentication method to login. I'm getting null because I am matching values from the Firebase database and if they matched I logged the user in.
In all this process, Firebase didn't get to know that who I'm. Because I'm only using its DB, not its official signin methods, like email, password, etc. So this is the reason I am getting a null UID.
And as far as for registration I am using Phoneauthentication to verify the number after verification automatically. Firebase gives me a UID for that user as I'm using its authentication so for that time it recognizes me as a user and assigns me a unique number which I get and made parent node for custom data I save with Firebase authentication at time of registering.
And then in Login, while matching the user credentials, I can get that UID as its parent node of my values by this code and you use it anywhere by saving it in shared preferences or passing through intent from activity to activity.
FirebaseDatabase.getInstance().getReference().child("Content").child("Profiles").orderByChild("mobile_number").equalTo(mobileNum)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot user : dataSnapshot.getChildren()) {
** String key = user.getKey(); **
Toast.makeText(LoginActivity.this, key, Toast.LENGTH_SHORT).show();
Profiles profiles = user.getValue(Profiles.class);
if (profiles.getPassword().equals(password)) {
progressDialog.dismiss();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}

How can a user1 blocks user2 from sending messages to user1. - Firebase

I have an application where users can chat with each other. One to one. I want to give users the ability to block a particular users list from sending him messages.
A code example would be appreciated.
Assuming user 1 is trying to send message to user2.
You can use below snippet to run before sending the message.
I assume you have a users node in your firebase database containing all users in the database.
Logic: user 1 is trying to send message to user 2. If user 1 is present in block list of user 2, do not allow to send.
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference()
.child("users")
.child("id_of_user2")
.child("block-list") // fetching block-list of user2
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> blockList = dataSnapshot.getValue(List.class);
if (blockList.contains(id_of_user1)){
// block list of user2 contains id of user1,
// DO NOT ALLOW TO SEND MESSAGE
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How do I show list of other logged in users to a user when he/she logs in to the app in firebase, Android?

How do I show list of other logged in users to a user when he/she logs in to the android app using firebase.
once the user opens the app he/she will have to choose a color among 5 colors.Now after choosing he will be shown a list of other logged in users, who have chosen the same color while logging in.
I'm using firebase for my project...any idea how to achieve this?
Thanks in advance :)
Firstly, define you DatabaseReference where are the users.
private DatabaseReference databaseReferencia =
FirebaseDatabase.getInstance().getReference();
private DatabaseReference userInReference= databaseReferencia.child("userIn");
private FirebaseAuth mAuth;
After sign in action and on open the app, insert this indentificator:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String name = user.getDisplayName();
String uid = user.getUid();
userInReference.child(uid).setValue(name);
}
After that, insert your listener.
userInReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//ADD USERS IN YOUR LIST
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Sorry my broke english.

How to know which user is trying to Sign-in?

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();
}
}
});

Categories

Resources