简体   繁体   English

在 Firebase 中实施身份验证的正确方法是什么?

[英]What is the proper way to implement authentication in Firebase?

ok so I've built a few apps in Firebase and really enjoy using the simple API but always struggle with auth.好的,所以我在 Firebase 中构建了一些应用程序并且非常喜欢使用简单的 API,但总是与身份验证斗争。 see this code看到这个代码

I have a simple email and password "form" (not a form element, just two inputs) and then a button (div) I click to call this function below我有一个简单的电子邮件和密码“表单”(不是表单元素,只有两个输入),然后是一个按钮(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)
        })
}

however when I put the lines below但是当我把下面的几行

    firebase.auth().onAuthStateChanged((user) => {
        if (user) {
            console.log('user logged in')
        } else {
            console.log('user not logged in')
        }
    })

it seemed to pick it up correctly它似乎正确地捡起它

but my question is, what is the point of signInWithEmailAndPassword ?但我的问题是, signInWithEmailAndPassword的意义是signInWithEmailAndPassword and also, how do I use both functions together?还有,我如何同时使用这两个功能? do I need to call onAuthStateChanged in the .then part of my signInWithEmailAndPassword function?我需要调用onAuthStateChanged.then我的一部分signInWithEmailAndPassword功能?

it's really confusing how to have a consistent state.如何拥有一致的状态真的很令人困惑。 my app seems to work after I refresh the page but I want it to work without refreshes (obviously)刷新页面后,我的应用程序似乎可以工作,但我希望它无需刷新即可工作(显然)

any ideas on the best approach?关于最佳方法的任何想法?

EDIT编辑

also when I click sign out也当我点击退出

firebaseApp.auth().onAuthStateChanged((user) => {
    if (user) {
        history.push('/account')
    } else {
        console.log('NO USER')
    }
})

NO USER logs but then I click on an authenticated route it takes me there until I refresh NO USER日志,但我点击了一条经过身份验证的路线,它会带我到那里,直到我刷新

The point of signInWithEmailAndPassword is to allow the implementation of classic username/password authentication for people who don't have a Google, Facebook, Twitter, etc. account. signInWithEmailAndPassword是允许对没有 Google、Facebook、Twitter 等帐户的人实施经典的用户名/密码身份验证。 If you decide to add it, Google will handle the user accounts and everything, but you'll have to build the login form, complete with the I forgot my password and Register as new user links.如果您决定添加它,Google 将处理用户帐户和所有内容,但您必须构建登录表单,填写我忘记密码注册为新用户链接。 Other than that it's simply another provider.除此之外,它只是另一个提供商。

I have a very simple little project on my Github repo which implements user/password authentication along with several others, I suggest you to look into it.我在我的 Github 存储库上有一个非常简单的小项目,它实现了用户/密码身份验证以及其他几个项目,我建议您研究一下。 I made it as a test assignment for a job interview.我把它作为工作面试的测试任务。 It's Angular.是角。 https://github.com/tomcatmwi/morbidomega https://github.com/tomcatmwi/morbidomega

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实现此JSON.parse的正确方法是什么? - what is the proper way to implement this JSON.parse? 实现常量的正确方法是什么? - What's the proper way to implement constant variable? 使用 reduce() 函数实现此例程的正确方法是什么? - what is the proper way to implement this routine with reduce() function? 在 TypeScript 中实现“toJson”function 的正确方法是什么? - What is the proper way to implement a 'toJson' function in TypeScript? 实现此自定义打字稿模块的正确方法是什么? - what's the proper way to implement this custom typescript module? 实现AngularJS控制器中使用的可重用JavaScript对象的正确方法是什么? - What is the proper way to implement a reusable JavaScript object used in AngularJS controllers? 筛选ViewModel存储并实现到ExtJS Panel的正确方法是什么? - What is the proper way to filtering ViewModel stores and implement to ExtJS Panel? 使用变量在 VueJs 中使用 SASS 实现暗模式的正确方法是什么 - what's the proper way to implement darkmode in VueJs with SASS using variables Firebase auth + firestore(“链接”用户的正确方法是什么) - Firebase auth + firestore (what is the proper way to "link" users) 显示以 firebase 身份验证为条件的 DOM 元素的首选方式是什么 - What is preferred way to show DOM elements conditioned on firebase authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM