So this question is pretty straightforward. I have a Cloud Function that should validate if it is a new user that is logging in with SSO (Facebook, Twitter or Google) in my web app.
Now the control is nessecary because if the user bypasses the “sign-up.html” and goes straight to “sign-in.html” and logged in, Firebase do create a uID and an Authentication is registered on all of them BUT:
No document is created in my Users collection Firestore and the “checkNewUser” Function here below never triggers.
Can you spot something missing with the code? Thanks:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.checkNewUser = functions.auth.user().onCreate(async (user) => {
const uid = user.uid;
const email = user.email || null;
const displayName = user.displayName || 'Anonymous';
const userDoc = db.collection('users').doc(uid);
const userSnapshot = await userDoc.get();
if (!userSnapshot.exists) {
// New user
await userDoc.set({
uid: uid,
email: email,
displayName: displayName,
isOnline: true,
});
} else {
// Existing user
await userDoc.update({
isOnline: true,
});
}
return null;
});