javascript - 在 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
?还有,我如何同时使用这两个功能?我需要调用我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 repo 上有一个非常简单的小项目,它实现了用户/密码身份验证以及其他几个,我建议你研究一下。我把它作为工作面试的测试作业。是角。 https://github.com/tomcatmwi/morbidomega
推荐阅读
- rundeck - 如何从 rundeck 中删除旧的执行和作业
- java - 我正在使用休眠将我的 Java 程序连接到 mysql 数据库,但出现错误
- bash - 如何在有问题的其他命令中将参数传递给 shell 脚本
- angular - 区分 Slick Grid 中的首次数据加载与过滤数据加载
- python - 如何从 Pandas 中的日期 CSV 数据中查找年均值以绘制条形图
- javascript - iframe 中的 Safari PHP 会话不起作用 (2019)
- arrays - 我有 10 个变量,我必须用 1 或 -1 填充它们,我如何列出所有可能的情况?
- sql-server - MS Access 2019 - SQL Server 2017 - 无法更新记录集
- python - 将数据从 JSON 转换为 pandas 数据框
- python-3.x - 如何在 PyQt5 中使用 QStandardItemModel 将 JSON 文件中的数据显示到 ListView 中?