-
-
Save shadowcodex/dcfe7d11b2e8cb0ca51d to your computer and use it in GitHub Desktop.
// Assuming you have included the firebase script tag above this javascript | |
// For reference here is the tag: <script src="https://cdn.firebase.com/js/client/2.3.2/firebase.js"></script> | |
// Assumes you have already authenticated the user. | |
// Setup your firebase reference | |
var ref = new Firebase('https://your-app.firebaseIO-demo.com/'); | |
// Setup a way to get your userid (Assuming using provided firebase authentication method...) | |
function getUser(authData) { | |
switch(authData.provider) { | |
case 'password': | |
return authData.password.email; | |
case 'twitter': | |
return authData.twitter.username; | |
case 'facebook': | |
return authData.facebook.email; | |
case 'google': | |
return authData.google.email; | |
case 'github': | |
return authData.github.username; | |
} | |
} | |
// Get authentication data | |
var authData = ref.getAuth(); | |
// Get your user information | |
var userid = getUser(authData); | |
// Call your function to check if they are a first time user (aka exists). | |
checkForFirstTime(userid); | |
// Setup what to do with the user information. | |
function userFirstTimeCallback(userId, exists) { | |
if (exists) { | |
alert('user ' + userId + ' exists!'); | |
// Do something here you want to do for non-firstime users... | |
} else { | |
alert('user ' + userId + ' does not exist!'); | |
// Do something here you want to do for first time users (Store data in database?) | |
} | |
} | |
// Tests to see if /users/<userId> exists. | |
function checkForFirstTime(userId) { | |
usersRef.child('users').child(userId).once('value', function(snapshot) { | |
var exists = (snapshot.val() !== null); | |
userFirstTimeCallback(userId, exists); | |
}); | |
} |
This help me. Thanks
usersRef.child('users').child(userId).once('value', function(snapshot) {
var exists = (snapshot.val() !== null);
I might be missing something here, but it seems like this implementation would be compromising your "users" collection. Your security rules would have to allow access to your 'users' collection by unauthenticated users. Even if it is read-only, it seems a bit dangerous.
i found another way to do it
let userData = firebase.auth().currentUser;
if(userData.metadata.creationTime === userData.metadata.lastSignInTime){
console.log("user for the first time")
}else{
console.log("user already present")
}
i found another way to do it let userData = firebase.auth().currentUser; if(userData.metadata.creationTime === userData.metadata.lastSignInTime){ console.log("user for the first time") }else{ console.log("user already present") }
Hey, thanks a lot for this one. This is quite simple to use and also solves my problem.
i found another way to do it let userData = firebase.auth().currentUser; if(userData.metadata.creationTime === userData.metadata.lastSignInTime){ console.log("user for the first time") }else{ console.log("user already present") }
thanks
i found another way to do it let userData = firebase.auth().currentUser; if(userData.metadata.creationTime === userData.metadata.lastSignInTime){ console.log("user for the first time") }else{ console.log("user already present") }
Used this one.. clean and work perfect 😄
// Get your user information
var userid = getUser(authData);
shouldn't it be var userId