2016-11-23 1 views
0

에서 아폴로 클라이언트에 대한 networkinteface 미들웨어를 구현하고은 어떻게 기본 사용 아폴로 반응에 로그인을 구현해야 반응 네이티브

가) 어떻게 교체하거나 클라이언트의 NetworkInterface에서의 미들웨어를 제거하기 위해 알고 싶어합니다. 직접 _middlewares 속성에 액세스하면 안됩니까? 나는 미도리웨어를 푸시하는 방법을 보았지만 제거 할 방법을 보지 못했습니다. use

b) 예제를 localstorage에서 asyncstorage로 변경하려면 대신 redux 저장소에서 직접 네트워크 인터페이스를 읽어야합니까? 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

// in src/index.js 
const networkInterface = createNetworkInterface({ uri: 'https://api.graph.cool/simple/v1/__PROJECT_ID__' }) 

networkInterface.use([{ 
    applyMiddleware (req, next) { 
    if (!req.options.headers) { 
     req.options.headers = {} 
    } 

    // change this from localstorage to asyncstorage, or perhaps redux store 
    if (localStorage.getItem('auth0IdToken')) { 
     req.options.headers.authorization = `Bearer ${localStorage.getItem('auth0IdToken')}` 
    } 
    next() 
    }, 
}]) 

답변

1

a) 현재 ([email protected]) NetworkInterface에 연결된 미들웨어를 제거 할 수있는 방법이 없습니다.

b) 귀하는 다음과 같은 스키마를 사용할 수 있습니다

// in src/index.js 
const networkInterface = createNetworkInterface({ uri: 'https://api.graph.cool/simple/v1/__PROJECT_ID__' }) 

// structure of auth reducer: 
// auth: { 
// token: "xyz" 
// } 

//create redux's store before using of middleware 
const store: Store<any> = createStore(combineReducers({ 
    auth = authReducer 
}) 

networkInterface.use([{ 
    applyMiddleware (req, next) { 
    const tokenFromReduxStore = store.getState().auth.token; 

    if (!req.options.headers) { 
     req.options.headers = {} 
    } 

    // change this from localstorage to asyncstorage, or perhaps redux store 
    if (tokenFromReduxStore) { 
     req.options.headers.authorization = tokenFromReduxStore 
    } 
    next() 
    }, 
}]) 
관련 문제