2016-05-27 23 views
7

nodejs'de Firebase auth'u kullanarak kullanıcı hesabı oluşturmaya çalışıyorum. Çalışmıyor ve nedenini bilmiyorum.node.js'de Firebase Auth ile nasıl bir kullanıcı oluşturabiliriz?

var config = {.......}; // i can't share the config 

var app = firebase.initializeApp(config); 

var db = app.database(); 
var auth = app.auth(); 

auth.createUserWithUsernameAndPassword("[email protected]", "@NewPassWord").then(
    function(user){ console.log(user);}, 
    function(error){ console.error(error);} 
); 

Hata NodeJS olarak oluşturacağı: İşte kod ve nodejs tarafından üretilen hata var iyi

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 
+0

Bende aynı sorun var! Lütfen yardım için http://stackoverflow.com/questions/37504466/firebase-auth-createuserwithemailandpassword-undefined-is-not-a-function?lq=1 –

+0

Müşteriden anonim kullanıcı kimlik doğrulaması kullanarak bunun için bir çalışma buldum. yan. Lütfen kullanım durumunuzu detaylandırın, böylece buna dayalı bir cevap yazabilirim. –

cevap

2

, ben sanmıyorum o işleri "createUserWithUsernameAndPassword" yöntemi yerine bu createUserWithEmailAndPassword

Kullanım

Firebase: e-posta kullanirken kullanici adi kullananlar

0

Cyrus'un cevabı doğru.

firebaseAdmin.auth().createUser({ 
    email: "[email protected]", 
    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); 
    }); 

official documentation yılında daha kapsamlı bir rehber bakınız:

Ancak Nov 2016 Bunu yapmanın yolu itibariyle aşağıdaki olduğuna dikkat edin.

1

Nihayet Node.js.'de çalıştım. appfirebase-admin'un bir örneğidir, firebase'un aksine herhangi bir şansa sahip misiniz? Benim express uygulamasında

ben şöyle ana app.js (oldukça standart ücret) içinde firebase sıfırlanır:

kullanıcıları yapmam bu yaratma sorumlu route yılında Sonra
var firebase = require("firebase"); 

firebase.initializeApp 
({ 
    apiKey: "xxx", 
    authDomain: "xxx", 
    databaseURL: "xxx", 
    storageBucket: "xxx", 
    messagingSenderId: "xxx" 
}); 

:

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(); 
    }); 
}); 
+0

Kullanıcının oturum açma veya kimlik doğrulama işlemlerini nerede gerçekleştiriyorsunuz? –

3

olarak Mayıs 2017, bu başarıyla kullanıyoruz.

// Create the authenticated user 
    admin.auth().createUser({ 
    email: email, 
    password: password, 
    displayName: `${firstName} ${lastName}`, 
    }) 
0

NOT: Bu bir cevap değil ama yukarıda @Gregg tarafından this cevabını genişletiyor.

Ben de Firebase + node + express ile bir hizmet hesabı kullanarak kullanıcı oluşturmak için mücadele ediyordum. İçin

başka ilgilenen herkes:

var admin = require("firebase-admin"); 
var serviceAccount = require("./key.json"); 

admin.initializeApp({ 
    credential: admin.credential.cert(serviceAccount), 
    databaseURL: "https://<app-name>.firebaseio.com" 
}); 

ve auth.js rotada I (@Gregg uyarınca) sahiptir:

kökü app.js ben var

const firebase = require('firebase-admin'); 

firebase.auth().defaultAuth.createUser({ 
    email: req.body.email, 
    password: req.body.password, 
    displayName: req.body.username, 
}) 

Biliyorum ki bu, önceki cevaba sadece hafif bir uzantıdır, ancak dokümanlar çok belirsiz görünmektedir ve aynı veya benzer bir soruna sahip olan çok sayıda insanı fark ettim.

İlgili konular