2016-06-12 4 views
3

그래서 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 옵저버는 자동으로 실행됩니다.

답변

0

당신은 우리가 사용자 인증을 읽을 때 여기에이 구현

서비스를 확인하고 REDUX 상태 개체 https://github.com/x-team/unleash/blob/develop/app/reducers/userReducer.js

에 사용자 상태를 설정하면 감속기

돌아 오는 https://github.com/x-team/unleash/blob/develop/app/services/authService.js에 값을 설정할 수 있습니다 액션 제작자 https://github.com/x-team/unleash/blob/develop/app/actions/UserActions.js

로그인 상태 확인 https://github.com/x-team/unleash/blob/develop/app/components/UnleashApp.jsx#L17

가장 중요한 부분은 authService입니다. 어떤 질문인지 알려주세요.

관련 문제