2016-05-02 17 views
0

以下のコードでは、認証されていない場合はreplace pathnameとログインするようにリダイレクトされますが、stateは何をしていますか?次の経路名は、反応ルータのonEnterとは何ですか?

EDITEDフォーカス取得時に同じ場所にユーザーをリダイレクトしない

login.js

login() { 

/*  ref.authWithOAuthPopup(this.props.provider, (error, authData) => { 
      if (error) { 
       console.log("Authentication Failed!", error); 
      } else { 
       console.log("Authenticated successfully with payload:", authData); 
       console.log(this.state) // return null here 
      } 
     }) */ 
    } 

authenticated.js

import Firebase from 'firebase'; 
import GLOBAL from './global.js'; 

var ref = new Firebase(GLOBAL.FIREBASE_URL); 

export function requireAuth(nextState, replace) { 

    ref.onAuth((authData) => { 
     if (!authData) { 
      replace ({ 
       pathname: '/login', 
       state: { nextPathname: nextState.location.pathname } 
      }) 
     } 
    }) 
} 

route.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import { Router, Route, browserHistory, hashHistory, IndexRoute } from 'react-router'; 
import Layout from './pages/Layout'; 
import Purchased from './pages/Purchased'; 
import Home from './pages/Home'; 
import Profile from './pages/Profile'; 
import PurchasedItemDetail from './pages/PurchasedItemDetail'; 
import Login from './pages/Login'; 
import { requireAuth } from './utils/authenticated'; 

ReactDOM.render((
    <Router history={browserHistory}> 
    <Route path="/" component={Layout}> 
     <IndexRoute component={Home} onEnter={requireAuth} /> 
     <Route path="/login" component={Login} /> 
     <Route path="/purchased" component={Purchased} onEnter={requireAuth} /> 
     <Route path="/purchased/:purchasedItemID" component={PurchasedItemDetail} onEnter={requireAuth} /> 
     <Route path="/profile" component={Profile} onEnter={requireAuth} /> 
    </Route> 
    </Router> 
), document.getElementById('app')) 

答えて

0

この関数のnextStateパラメータは、ユーザが次に入力するアプリケーションの状態を表すRouter State objectです。したがって、state: { nextPathname: nextState.location.pathname }に合格すると、ルートハンドラにこのデータを送信させます。この場合、基本的にリダイレクトルートです。ログイン後、別の場所にリダイレクトされます。

+0

私はそれを取得しません。ログインコールバックで 'this.state.nextPathname'を呼びますか? – vzhen

+0

私はそれがどのように使用されているかを見なければならないでしょう。 –

+0

my/loginとは異なるファイルにあります – vzhen

関連する問題