Skip to content

Instantly share code, notes, and snippets.

@anantn
Created December 18, 2012 00:54
Show Gist options
  • Save anantn/4323949 to your computer and use it in GitHub Desktop.
Save anantn/4323949 to your computer and use it in GitHub Desktop.
Firebase: Detecting if data exists. This snippet detects if a user ID is already taken
function go() {
var userId = prompt('Username?', 'Guest');
checkIfUserExists(userId);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userExistsCallback(userId, exists) {
if (exists) {
alert('user ' + userId + ' exists!');
} else {
alert('user ' + userId + ' does not exist!');
}
}
// Tests to see if /users/<userId> has any data.
function checkIfUserExists(userId) {
var usersRef = new Firebase(USERS_LOCATION);
usersRef.child(userId).once('value', function(snapshot) {
var exists = (snapshot.val() !== null);
userExistsCallback(userId, exists);
});
}
@tumaponchard16
Copy link

Can anyone has an idea on how to do it in Laravel?

Copy link

ghost commented Mar 4, 2016

nice

@Abdul007Malik
Copy link

Abdul007Malik commented Apr 26, 2016

I am making chat application using java, "https://github.com/Abdul007Malik/Conversa.git", I am confused and cannot progress further help me if you can, my questions:

  1. if I know the auth.getUID of others can i modify there data or firebase have different mechanism to check authentication.
  2. how can I send the invitation(inviting to join the group) to friends if i know there phone numbers but dont know there particular userid.

@sebasj14
Copy link

This was just perfect. Thank you!

@davidgilbertson
Copy link

davidgilbertson commented May 10, 2016

A promise based version (where db is a Firebase ref)

export function checkIfUserExists(authData) {
  return db
    .child('users')
    .child(authData.uid)
    .once('value')
    .then(dataSnapshot => {
      return Promise.resolve({
        authData,
        userExists: dataSnapshot.exists(),
      });
    });
}

Which would be called like so:

  db.authWithOAuthPopup(provider)
    .then(checkIfUserExists)
    .then(({authData, userExists}) => {
      if (userExists) {
        // update user
      } else {
        // go create a user
      }
    })
    .catch(err => {
      console.warn('Error signing in.', err);
    });

Gist: https://gist.github.com/davidgilbertson/52a72f7c5e35502127e5848a8bf881b2

@amanuel2
Copy link

amanuel2 commented Jun 4, 2016

Thanks a lot sir. Helped me construct my own idea

@samyoungnyc
Copy link

has anyone figured this out using Python? I'm using the following library: https://pypi.python.org/pypi/python-firebase/1.2

@amirshahzadmcs
Copy link

Please any body can tell me that i have request record in firebase . i want to get spesific user which userId is 91 how i can access this user by query on firebase

{
"request" :
{
"123" :
{
"userId" : "90",
"name" : "mary"
},
"124" :
{
"userId" : "91",
"name" : "johan"
},
"124" :
{
"userId" : "92",
"name" : "maikal"
}
}
}

@sudikrt
Copy link

sudikrt commented Jul 15, 2016

How to retrieve the user email using the user id (client id)?

@j-garin
Copy link

j-garin commented Aug 4, 2016

sudikrt, create a table with names and emails and add data to it after user has registered. then you will be able to search for records by email or id.

@ayudh37
Copy link

ayudh37 commented Nov 3, 2016

The crux: var exists = (snapshot.val() !== null);

@ayoola-solomon
Copy link

if you want to listen on a node. You can change the implementation to the snippet below.

  const checkIfUserExists = (userId) => {
    const usersRef = new Firebase(USERS_LOCATION);
    usersRef.child(userId).on('value', (snap) => {
    const exists = (snap.val() !== null);
    userExistsCallback(userId, exists);
   });
 }

@clins1994
Copy link

didn't know about the once() method. thanks 👍

@l-bimba
Copy link

l-bimba commented Jun 26, 2017

Can you do this in swift?

@guilhermemauro
Copy link

Nice!

@Sowmayjain
Copy link

Sowmayjain commented Jul 23, 2017

+2

@Sunitadaffodil
Copy link

I am making chat application using java, "https://github.com/Abdul007Malik/Conversa.git", I am confused and cannot progress further help me if you can, my questions:

  1. if I know the auth.getUID of others can i modify there data or firebase have different mechanism to check authentication.
  2. how can I send the invitation(inviting to join the group) to friends if i know there phone numbers but dont know there particular userid.

Did you find any solution for this. I am facing same issue where I know email id but not user id

@tayouthae
Copy link

Can you give us python examples??

@lohriialo
Copy link

@sylvain-guehria
Copy link

thank's man, very helpful =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment