[英]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.