[英]What is the proper way to implement authentication in Firebase?
好的,所以我在 Firebase 中构建了一些应用程序并且非常喜欢使用简单的 API,但总是与身份验证斗争。 看到这个代码
我有一个简单的电子邮件和密码“表单”(不是表单元素,只有两个输入),然后是一个按钮(div),我单击以在下面调用此函数
const logUserIn = () => {
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then((res) => {
console.log('here') // this WORKS but the history redirect below does not work :/
localStorage.setItem('authUser', JSON.stringify(res))
history.push('/account')
})
.catch((error) => {
console.log('ERROR:', error)
})
}
但是当我把下面的几行
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user logged in')
} else {
console.log('user not logged in')
}
})
它似乎正确地捡起它
但我的问题是, signInWithEmailAndPassword
的意义是signInWithEmailAndPassword
? 还有,我如何同时使用这两个功能? 我需要调用onAuthStateChanged
的.then
我的一部分signInWithEmailAndPassword
功能?
如何拥有一致的状态真的很令人困惑。 刷新页面后,我的应用程序似乎可以工作,但我希望它无需刷新即可工作(显然)
关于最佳方法的任何想法?
编辑
也当我点击退出
firebaseApp.auth().onAuthStateChanged((user) => {
if (user) {
history.push('/account')
} else {
console.log('NO USER')
}
})
NO USER
日志,但我点击了一条经过身份验证的路线,它会带我到那里,直到我刷新
signInWithEmailAndPassword
是允许对没有 Google、Facebook、Twitter 等帐户的人实施经典的用户名/密码身份验证。 如果您决定添加它,Google 将处理用户帐户和所有内容,但您必须构建登录表单,填写我忘记密码和注册为新用户链接。 除此之外,它只是另一个提供商。
我在我的 Github 存储库上有一个非常简单的小项目,它实现了用户/密码身份验证以及其他几个项目,我建议您研究一下。 我把它作为工作面试的测试任务。 是角。 https://github.com/tomcatmwi/morbidomega
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.