그래서 firebase를 사용하여 React + redux 앱을 만들고 있습니다. 나는 경로 보호를 위해 반응 라우터 onEnter 콜백 함수 (checkAuth)를 사용하고있다.Firebase onAuthStateChanged가 React App에서 예상대로 실행되지 않습니다.
export default function getRoutes (checkAuth) {
return (
<Router history={browserHistory}>
<Route path='/' component={MainContainer}>
<IndexRoute component = {HomeContainer} onEnter = {checkAuth}/>
<Route path='auth' component = {AuthenticateContainer} onEnter = {checkAuth} />
<Route path='feed' component = {FeedContainer} onEnter = {checkAuth} />
<Route path='logout' component = {LogoutContainer} />
</Route>
</Router>
)
}
checkAuth 함수는 currentUser가 있는지 checkIfAuthed 함수를 호출합니다.
function checkAuth (nextState, replace) {
const isAuthed = checkIfAuthed(store)
console.log('isAuthed from checkAuth method', isAuthed)
const nextPathName = nextState.location.pathname
console.log('nextPathName', nextPathName)
// debugger
if (nextPathName === '/' || nextPathName === 'auth') {
if (isAuthed === true) {
// debugger
replace('feed')
}
} else {
// debugger
if (isAuthed !== true) {
// debugger
replace('auth')
}
}
}
ReactDOM.render(
<Provider store = {store}>
{getRoutes(checkAuth)}
</Provider>,
document.getElementById('app')
)
checkIfAuthed 기능은 다음과 같습니다
export function checkIfAuthed (store) {
// debugger
// const user = firebase.auth().currentUser
firebase.auth().onAuthStateChanged((user) => {
console.log('isAuthed from on state changed', user)
// debugger
if (user === null) {
// debugger
return false
} else if (store.getState().isAuthed === false) {
// debugger
const userInfo = formatUserInfo(user.displayName, user.photoURL, user.uid)
// debugger
store.dispatch(authUser(user.uid))
// debugger
store.dispatch(fetchingUserSuccess(user.uid, userInfo))
// debugger
return true
}
// debugger
return true
})
}
그러나, CONST 이 항상 checkAuth (에서 런타임에 정의되지 isAuthed) 대체 할 최고의 function.thus ("공급 ") 결코 달리지 않는다. 거짓인지 또는 진실인지 기대하고있었습니다.
또한 checkIfAuthed 함수에서 const user = firebase.auth(). currentUser를 사용하는 경우 바꾸기 기능이 실행되지만 사용자가 로그인 버튼을 눌러야합니다. 위의 firebase 옵저버는 자동으로 실행됩니다.