Reputation: 91
I'm trying to create user account using Firebase auth in nodejs. It doesn't work and I don't know why. Here's the code and the error generated by nodejs:
var config = {.......}; // i can't share the config
var app = firebase.initializeApp(config);
var db = app.database();
var auth = app.auth();
auth.createUserWithUsernameAndPassword("anemail@gmail.com", "@NewPassWord").then(
function(user){ console.log(user);},
function(error){ console.error(error);}
);
Error generate by NodeJS:
TypeError: Object [object Object] has no method 'createUserWithUsernameAndPassword'
at Object.<anonymous> (/home/antonio/Projects/firebase/app.js:20:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Upvotes: 9
Views: 12146
Reputation: 491
NOTE: This isn't an answer but is extending this answer by @Gregg above.
I too was struggling to create users using an service account with Firebase + node + express.
For anyone else interested:
In the root app.js
I have:
var admin = require("firebase-admin");
var serviceAccount = require("./key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<app-name>.firebaseio.com"
});
and in the auth.js route I have (as per @Gregg):
const firebase = require('firebase-admin');
firebase.auth().defaultAuth.createUser({
email: req.body.email,
password: req.body.password,
displayName: req.body.username,
})
I know this is only a slight extension to the earlier answer however the docs seem to be very unclear, and I've noticed a large number of people that have the same or similar problem.
Upvotes: 2
Reputation: 1476
As of May 2017, this is what we are using successfully.
// Create the authenticated user
admin.auth().createUser({
email: email,
password: password,
displayName: `${firstName} ${lastName}`,
})
Upvotes: 3
Reputation: 1244
I finally got it working in Node.js. Is your app
an instance of firebase-admin
by any chance, as opposed to firebase
?
In my express
app I initialise firebase
in the main app.js
like so (pretty standard fare):
var firebase = require("firebase");
firebase.initializeApp
({
apiKey: "xxx",
authDomain: "xxx",
databaseURL: "xxx",
storageBucket: "xxx",
messagingSenderId: "xxx"
});
Then in the route
responsible for creating users I do this:
var firebase = require("firebase");
router.post('/register', function(req, res, next)
{
var email = req.body.email;
var password = req.body.password;
firebase.auth().createUserWithEmailAndPassword
(
email,
password
)
.then(function(userRecord)
{
res.location('/user/' + userRecord.uid);
res.status(201).end();
})
.catch(function(error)
{
res.write
({
code: error.code
});
res.status(401).end();
});
});
Upvotes: 5
Reputation: 2273
Cyrus' response is correct.
However, please note that as of Nov 2016 the way to do this is the following:
firebaseAdmin.auth().createUser({
email: "user@example.com",
password: "secretPassword"
})
.then(function(userRecord) {
// A UserRecord representation of the newly created user is returned
console.log("Successfully created new user:", userRecord.uid);
})
.catch(function(error) {
console.log("Error creating new user:", error);
});
See a more complete guide in the official documentation.
Upvotes: 0
Reputation: 2660
well, I don't think that the method "createUserWithUsernameAndPassword"
works,
Use this instead createUserWithEmailAndPassword
you used username when firebase use email
Upvotes: 3