I am a beginner to Firebase cloud Functions. My main motivation of using cloud functions is for authentication.
index.js (in functions folder)
var admin = require("firebase-admin");
const functions = require('firebase-functions');
const createuser = require('./funs/create_user');
var serviceAccount = require('./funs/serviceAccount.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "//Given by Firebase"
});
exports.createuser = functions.https.onRequest(createuser);
create_user.js
const admin = require("firebase-admin");
module.exports = function (req,res) {
if(!req.body.phone)
{
return res.status(422).send({error: "Bad Input"});
}
const phone = String(req.body.phone).replace(/[^\d]/g,"");
admin.auth().createUser({ uid: phone })
.then((user) => res.send(user))
.catch((err)=>res.status(422).send({error:err}));
//return 1;
}
When I call the function using Postman, I pass data to the cloud function as:
{
"phone": "1212121212"
}
I get response as "Bad Input" as specified inside of if statement.
On removal of the if condition i get this as my response
{
"error": {
"code": "auth/invalid-uid",
"message": "The uid must be a non-empty string with at most 128
characters."
}
}
