[目前,我正在研究一种登录模式,该模式将使用firebase函数对用户进行身份验证。如果它们不存在,我计划使用reactstrap库将这些框变成红色,并发出诸如“无效的电子邮件或密码”之类的消息。但是,我是React的新手,当我以电子邮件形式调用onAuthenticate = {firebase.authenticateUser}时,很难从firebase函数获取返回对象/编号。我如何获取返回对象(如果用户不存在,则为-1),检查它,然后关闭模态,或者如果用户不存在,则用react腕带将其变为红色?
我的firebase函数的代码段,然后是下面的我的React模式。
'''
exports.authenticateUser = async (email, password) => {
firebase.auth().signInWithEmailAndPassword(email, password)
.then(() => {
const success = {
success: true,
};
alert('User exists.');
console.log('user exists');
return success;
})
.catch((error) => {
alert('Invalid email or password.');
console.log('no user');
return -1;
});
};
return (
<Modal
title="Please enter log-in information"
onClose={onClose}
footer={modalFooter}
titleBackgroundColor="#FFFACD"
>
<FormGroup>
<Label for="examplePassword">Invalid input</Label>
<Input invalid />
<FormFeedback tooltip>Oh noes! that name is already taken</FormFeedback>
<FormText>Example help text that remains unchanged.</FormText>
</FormGroup>
<EmailForm
onAuthenticate={firebase.authenticateUser}
const returnObj = {firebase.authenticateUser}
/>
</Modal>
);
'''
您的用户应该是从诺言中返回的值。请尝试以下操作,如果用户已登录,它应该登录;如果出现问题,它应该记录错误。
firebase.auth().signInWithEmailAndPassword(email, password)
.then(user => console.log(user))
.catch(error => console.log(error))
如果用户不存在并且没有错误,则此方法将创建用户。
尚不清楚,这是否解决了您的问题,或者您是否正在寻找一种方法来验证这是否是用户的首次登录?因为在用户中数据库中尚不存在该用户?
如果这是您要执行的操作,注册方法将无济于事。请参考Firebase Auth, how to know new user signed up, rather than existing user sign in?