0
I have the following structure in my Firebase:

users ->
         uid ->
               provider: "facebook.com"
               username: "vandanpatel"

When I log the user in, I just want to check if he has his username already set or not. I have the mechanism to get the current user. I just want to check if the current user has the field "username" or not. I have tried everything, but nothing seems to work for me. if they don't have "username" field, I would make them select the "username" and set it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vandan Patel
  • 1,012
  • 1
  • 12
  • 23
  • 2
    Have look at this question http://stackoverflow.com/questions/35243492/firebase-android-make-username-unique . You need to have proper security rules to handle this case. – Nishant Dubey Oct 08 '16 at 02:58

1 Answers1

0

You can try:-

    FIRDatabase.database().reference().child("users/\(FIRAuth.auth()!.currentUser!.uid)/username").observeSingleEvent(of: .value, with: {(snap) in

        if snap.exists(){

            //Your user already has a username

        }else{

            //You need to set the user's name and the the required segue

        }
    })

Also Check this link :- Check if username already exists

Community
  • 1
  • 1
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • I have achieved this task of uniqueness. What i am trying to do is when user tries to log in, and if his username field is already set, I would divert him to another page, so all i am trying to do is just check for my current user that if he already picked a username or not. – Vandan Patel Oct 08 '16 at 05:57
  • Answer updated, this will tell you wether or not your user already has a username stored in Firebase or not. – Dravidian Oct 08 '16 at 06:07