2017-11-02 5 views
0

내 redux 상점에서 모든 아폴로 요청에 토큰을 전달하고 싶습니다. 난 당신이 cookiesApollo 요청에 토큰을 전달하려면 어떻게해야합니까?

const networkInterface = createNetworkInterface({ 
    uri: '/graphql', 
    opts: { 
    credentials: 'same-origin', 
    }, 
}); 

을 전달할 수 있습니다 알고 있지만 헤더에 추가 할 수 있습니다.

networkInterface.use([{ 
    applyMiddleware(req, next) { 
    if (!req.options.headers) { 
     req.options.headers = {}; // Create the header object if needed. 
    } 
    // get the authentication token from local storage if it exists 
    const token = localStorage.getItem('token'); 
    req.options.headers.authorization = token ? `Bearer ${token}` : null; 
    next(); 
    } 
}]); 

그러나 어떻게 token을 가게에서 꺼내야합니까? localStorage을 꺼내는 것은 내 앱이 서버 쪽 렌더링이기 때문에 선택 사항이 아니므로 localStorage에 대한 액세스 권한을 항상 가지고 있어야합니다. Redux 저장소에 액세스 할 수 있어야합니다. 어떻게해야합니까?

답변

0

네트워크 인터페이스를 설치 한 곳에서 상점을 내보내고 가져올 수 있습니다. 이 answer을 참조하십시오.