dongerpep
dongerpep

Reputation: 109

Create new Account using Cloud Functions Request/Express

I am trying to create new account using Cloud Functions. This is my function:

    const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const cors = require('cors')({origin: true});

admin.initializeApp(functions.config().firebase);

// Create the server
const app = express(); 

app.use((err, req, res, _next) => {
  console.log('Error handler', err);
  console.log(req);
  if(err){
    res.status(400).send({
      message:'error',
      message2:req,
      message3:req.body
    });
  } else {
   admin.auth().createUser({
  email: "[email protected]", //change with post request data
  password: "secretPassword", //change with post request data

})
  .then(function(userRecord) {
    console.log("Successfully created new user:", userRecord.uid);
  })
  .catch(function(error) {
    console.log("Error creating new user:", error);
  });



  }
});
app.use(cors);
app.get('/', (req, res) => {
    res.send(`Hello ${req}`);
  });
exports.registeration = functions.https.onRequest(app);

Right now i am just trying to test.I use request library on client side:

request.post(
    'urloffunction',
    { json: { key: 'Testing testing' } },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

Basically what i am trying to do is send some account data to server function and create a new account with Cloud Functions. enter image description here

Upvotes: 0

Views: 56

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

If you want to use an Express app to handle HTTP requests from Cloud Functions, you'll have to declare at least one route in that app that will match the incoming requests. Your sample declares no routes, which means that none of your code will get executed for any request.

You can see an official example that sets an Express route here: https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint/

Upvotes: 2

Related Questions