From a7e8e553d085c876a658b340d0cf9f627127514b Mon Sep 17 00:00:00 2001 From: Micah Mutrux Date: Fri, 31 Jan 2020 07:43:22 -0500 Subject: [PATCH] Work in Progress: create user This is a stand-alone function that will successfully create a new user account for the email address supplied in a POST body. You can give it a try and see it working by using the firebase emulators (see https://firebase.google.com/docs/rules/emulator-setup) and using the cUrl sample in the function comments. This functionality needs to be incorporated into another function that has access to the parsed csv data so that users are created with the right data. (Creating a user probably won't be a stand-alone endpoint, unless there is a compelling reason to do so. I just used a new function for testing.) --- functions/index.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/functions/index.js b/functions/index.js index c490ae8..7c996f5 100644 --- a/functions/index.js +++ b/functions/index.js @@ -7,6 +7,7 @@ const User = require('./models/user'); const Goal = require('./models/goal'); const {isValidEmail} = require('./models/data-validators'); const {db} = require('./models/user'); +var admin = require('firebase-admin'); exports.helloWorld = functions.https.onRequest((request, response) => { response.send("Hello from Firebase!"); @@ -19,6 +20,49 @@ exports.helloWorld = functions.https.onRequest((request, response) => { } }); +/** + * WIP: Create a new user + * + * Since the use of this app should be restricted to existing CVOEO clients, users + * cannot create their own account - the app does that for them. This is the second + * step in creating an account for users: + * 1) MoMM pulls csv data from OutcomeTracker and identifies new ReachUp clients + * 2) MoMM creates a new user in Firebase with the csv data + * 3) The new user opens the app for the first time and clicks "register" + * 4) The "register" behavior is actually the "forgot password" behavior; it walks + * the user through resetting the password for the firebase account we've created + * + curl -X POST \ + http://localhost:5001/cvoeo-45350/us-central1/createAccount \ + -H 'Content-Type: application/json' \ + -d '{ + "email": "micahm@gmail.com" + }' + */ +exports.createAccount = functions.https.onRequest((request, response) => { + console.info(`Let's try creating an account for ${request.body.email}!`); + + // see: https://firebase.google.com/docs/auth/admin/manage-users#create_a_user + admin.auth().createUser({ + email: request.body.email, + emailVerified: false, + phoneNumber: '+11234567890', + password: 'secretPassword', + displayName: 'John Doe', + photoURL: 'http://www.example.com/12345678/photo.png', + disabled: false + }) + .then(function(userRecord) { + // See the UserRecord reference doc for the contents of userRecord. + console.log('Successfully created new user:', userRecord.email); + response.send({ status: 200, data: { userRecord }}); + }) + .catch(function(error) { + console.log('Error creating new user:', error); + response.send({ status: 500, error: error }); + }); +}); + /*This firebase function is for testing purposes to be able to use a file saved locally as input. To run this function, have a firebase server set locally then run the following command: curl -X POST -H "Content-Type:application/json" -d '{"pathToFile":""}'