问题:
0)用户是在Firebase的auth系统中创建的(我在Auth选项卡中看到它),
1)但是没有对数据库进行任何更改。
2)页面似乎无法加载。
3)仅#34;开始1 ..."登录到控制台。
代码:
router.post('/register', function(req, res, next) {
var username = req.body.username;
var email = req.body.email;
var password = req.body.password;
var password2 = req.body.password2;
// Validation
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email is not valid').isEmail();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
res.render('users/register', {
errors: errors
});
} else {
console.log("Started 1...");
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error, userData) {
console.log("Started 2...");
if(error){
var errorCode = error.code;
var errorMessage = error.message;
req.flash('error_msg', 'Registration Failed. Make sure all fields are properly filled.' + error.message);
res.redirect('/users/register');
console.log("Error creating user: ", error);
} else {
console.log("Successfully created");
console.log("Successfully created user with uid:", userData.uid);
var user = {
uid: userData.uid,
email: email,
username: username
}
var userRef = firebase.database().ref('users/');
userRef.push().set(user);
req.flash('success_msg', 'You are now registered and can login');
res.redirect('/users/login');
}
});
}
});
编辑1:
这似乎正在发生:
用户在auth系统中创建。页面加载大约5分钟(很长!),然后告诉我注册失败,因为电子邮件地址已经在使用(它不是)。
似乎用户已创建,但注册失败,因为代码在创建用户后循环回来,就好像它尚未创建一样,因此会产生类型错误:此电子邮件地址已存在于我们的数据库中。
但为什么会这样呢?
答案 0 :(得分:4)
不确定这一切,但这就是我认为发生的事情:
创建了用户,但您没有正确处理它。没有处理成功案例(履行承诺),只有被拒绝的案件。此外,当您在发生错误时尝试写入数据库时,这意味着用户未经过身份验证,这意味着如果您尚未更改firebase安全规则,则无法编写。 (默认的firebase安全规则可防止未经过身份验证的用户读取/写入您的数据库)
这一行:
map1 = data1['subject_id'].map(lambda x: x in list(data2['subject_id']))
map2 = data2['subject_id'].map(lambda x: x in list(data1['subject_id']))
pd.concat([data1[map1], data2[map2]])
应改为:
firebase.auth().createUserWithEmailAndPassword(email,password)
.catch(function(error, userData) { ...
请注意,发生错误时,您将无法访问
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(userData => { // success - do stuff with userData })
.catch(error => { // do stuff with error })
,只能访问错误。
希望这有帮助!
答案 1 :(得分:1)
我最近遇到了这个问题。似乎某些功能,例如createUser()
中的listUsers()
或firebase-admin
,在使用NodeJS时需要使用私钥文件初始化应用程序,这意味着您不能使用此类功能然后像这样初始化您的应用程序:
const admin = require('firebase-admin');
const config = {
apiKey: "an_amazing_API_KEY",
authDomain: "lolthatsfunny.firebaseapp.com",
databaseURL: "https://lolthatsfunny.firebaseio.com",
projectId: "lolthatsfunny",
storageBucket: "lolthatsfunny.appspot.com",
messagingSenderId: 1212121212
};
admin.initializeApp(config);
相反,您必须:
const admin = require("firebase-admin");
const serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://cuidatushabitos-cth.firebaseio.com"
});
现在,您将能够使用前面提到的功能,例如createUser()
:
admin.auth().createUser({
email: 'user@example.com',
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);
});