Reputation: 1007
I am trying to create a user in firebase database using cloud functions. This is my code :
exports.AddUser = functions.https.onRequest(function(req, res){
if(req.method == 'GET'){
email = req.query.email;
password = req.query.password;
name = req.query.name;
admin.auth().createUserWithEmailAndPassword(email, password).then(function(user){
user.sendEmailVerification().then(function(){
res.send("verification email sent.");
}).catch(function(){
res.end(user)
})
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
res.send(errorMessage);
});
}else{
email = req.body.email;
password = req.body.password;
name = req.body.name;
admin.auth().createUserWithEmailAndPassword(email, password).then(function(user){
user.sendEmailVerification().then(function(){
res.send("verification email sent.");
}).catch(function(error){
res.end(user)
});
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
res.send(errorMessage);
});
}
});
This is my package.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1"
},
"private": true
}
It works when I use admin.createUser()
only but then I cannot use user.sendEmailVerification()
because for some reasons its only available if the user is created using admin.createUserWithEmailAndPassword()
.
This is what I get in firebase console :
TypeError: admin.auth(...).createUserWithEmailAndPassword is not a function
at /user_code/index.js:26:26
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:37:41)
at /var/tmp/worker/worker.js:684:7
at /var/tmp/worker/worker.js:668:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
What do I do? Here,
const functions = require('firebase-functions');
const admin = require('firebase-admin');
I am new to firebase and cloud functions so apologies.
Upvotes: 6
Views: 5583
Reputation: 311
you just have to use auth().createUser instead of createUserWithEmailAndPassword
admin.auth().createUser({
email: '[email protected]',
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.uid);
})
.catch(function(error) {
console.log('Error creating new user:', error);
});
check this out: https://firebase.google.com/docs/auth/admin/manage-users#create_a_user
Upvotes: 9
Reputation: 317372
The message is telling you everything you need to know. There is no method of that name in the node admin SDK.
You can see a list of all the methods on admin.auth()
in the API docs.
createUserWithEmailAndPassword only exists in the client SDK.
Upvotes: 8