flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

fl_users on register #77

Closed shaunnez closed 5 years ago

shaunnez commented 5 years ago

When a user "registers" with the firebase project, should they not automatically be created in the fl_user schema? E.g. when createUserWithEmailAndPassword happens

jperasmus commented 5 years ago

Currently, we opted to go with a client-side only connection to your Firebase project, which means we have no way of knowing when a new user is created unless it is something that happens within the app. In the near future, we'll allow project owners to additionally provide us with a service account so that we can perform more of these server-side only operations for some of the more advanced features to come. This will be opt-in.

What I've seen another user do for the scenario you mentioned is to set up a cloud function that listens to new users being created in the app and then you can use the flamelinkApp.users.addToDB() method to write the user to fl_users as well.

shaunnez commented 5 years ago

Yup that's what I've done just wanted to see if it was something I was missing or coming in the future. Thanks! (heres my function)

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as flamelink from 'flamelink/app';

export const onCreateUser = functions.auth.user().onCreate(async (user) => {
  try {
    const flamelinkApp = flamelink({ firebaseApp: admin.app(), dbType: 'cf' });
    const result = await flamelinkApp.users.addToDB({
      uid: user.uid,
      data: {
        displayName: user.email ? user.email.split('@')[0] : '',
        email: user.email,
        enabled: 'Yes',
        permissions: '1'
      }
    });
    console.log('onCreateUser success', result);
  } catch (ex) {
    console.log('onCreateUser error', ex);
  }
});
jperasmus commented 5 years ago

Perfect 👍 Just out of interest, the permissions option is currently only used for users inside the Flamelink app.