From 7af74ff8988961c988f03c95961b70c2918521ae Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 25 Aug 2017 09:33:53 -0400 Subject: yay --- lib/initApollo.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'lib/initApollo.js') diff --git a/lib/initApollo.js b/lib/initApollo.js index 4ca3b05..de5c7f2 100644 --- a/lib/initApollo.js +++ b/lib/initApollo.js @@ -9,15 +9,35 @@ if (!process.browser) { } function create (initialState) { + + const networkInterface = createNetworkInterface({ + uri: 'https://api.graph.cool/simple/v1/cj5xz8szs28930145gct82bdj', // Server URL (must be absolute) + opts: { // Additional fetch() options like `credentials` or `headers` + credentials: 'same-origin', + } + }); + + // use the auth0IdToken in localStorage for authorized requests + networkInterface.use([ + { + applyMiddleware(req, next) { + if (!req.options.headers) { + req.options.headers = {}; + } + + // get the authentication token from local storage if it exists + if (typeof localStorage !== 'undefined' && localStorage.getItem("auth0IdToken")) { + req.options.headers.authorization = `Bearer ${localStorage.getItem("auth0IdToken")}`; + } + next(); + } + } + ]); + return new ApolloClient({ initialState, ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once) - networkInterface: createNetworkInterface({ - uri: 'https://api.graph.cool/simple/v1/cj5xz8szs28930145gct82bdj', // Server URL (must be absolute) - opts: { // Additional fetch() options like `credentials` or `headers` - credentials: 'same-origin' - } - }) + networkInterface }) } -- cgit v1.3