Google Auth w/ Firebase: Check If User Exists

6,048 views
Skip to first unread message

Felix Xiao

unread,
Jan 4, 2016, 1:16:16 PM1/4/16
to Firebase Google Group
Hi everyone, I am trying to build a web app that uses Firebase's authentication with Google, but with an extra step. I am almost there but am currently stuck on step 3. Has anyone done something like this before or could point me in the right direction? Thanks ahead of time!

1. User clicks on login button and Firebase authenticates with Google:

login() {
this.ref = new Firebase('https://myapp.firebaseio.com/');
  this.ref.onAuth(this.authDataCallback.bind(this));
this.ref.authWithOAuthPopup('google', function (error, authData) {
if (error) {
console.log('Login Failed!', error);
} else {
console.log('Authenticated successfully');
}
}, {
scope: "email"
});
}

2. Upon success, an authDataCallback method is called with an authData object. I can use this to fetch the user's google email and log it.

authDataCallback(authData) {
  if (authData) {
    console.log(authData["google"]["email"]);
  }
}

3. I want to also check to see if that user already exists in my Firebase data. I only want to allow the authentication if the user already exists.

authDataCallback(authData) {
  if (authData) {
    var userEmail = authData["google"]["email"];
    // TODO: check to see if this email exists in firebase /users
// Do not authenticate unless it does
  }
}


I currenlty have a https://myapp.firebaseio.com/users reference that I can use. Inside of this data setup, keys are email addresses so it looks like:
{
    "addr...@email.com" : {
        username: "Felix",
        points: 20
    },
    "addr...@email.com" : {
        username: "Bob",
        points: 10
    },
    etc.
}

So basically, if a user authenticates with addr...@email.com or addr...@email.com, I want them to be authenticated and for it to succeed. Otherwise, I want the authentication to fail.

Thank you so much! I'll post back if I get any success, I'm still working on it now.



Kato Richardson

unread,
Jan 4, 2016, 1:26:02 PM1/4/16
to Firebase Google Group
Hi Felix,

Is there some condition under which user credentials would be created, but not the profile/data? Assuming no, you only need to write a security rule that checks to see if the user profile exists and don't allow database access unless they do. See here: https://groups.google.com/forum/#!topic/firebase-talk/DfvceSDMbNk for an example.

☼, Kato



--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/f92452ad-f002-4d02-ae39-7680ea271088%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Cannon

unread,
Apr 22, 2016, 1:56:43 PM4/22/16
to Firebase Google Group
Hi Kato, I am working on this exact scenario.  But I am not familiar with security rules in firebase.  If I write a rule like you recommended would that cause the authWithPassword() function to return an error if they didn't have a user profile, or would the authentication still be positive?
Reply all
Reply to author
Forward
0 new messages