Skip to content

Commit 26ccb45

Browse files
author
Carlos Rufo Jimenez
committed
5.4-end-authentication-v2.1-tmp
1 parent fd5a689 commit 26ccb45

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"apollo-client-preset": "^1.0.8",
7+
"apollo-link-context": "^1.0.7",
78
"graphql": "^0.13.1",
89
"graphql-tag": "^2.8.0",
910
"react": "^16.2.0",

src/index.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,24 @@ import App from './components/App'
55
import registerServiceWorker from './registerServiceWorker'
66
import { ApolloProvider } from 'react-apollo'
77
import { ApolloClient } from 'apollo-client'
8-
import { HttpLink } from 'apollo-link-http'
8+
import { createHttpLink } from 'apollo-link-http'
99
import { InMemoryCache } from 'apollo-cache-inmemory'
1010
import { BrowserRouter } from 'react-router-dom'
1111
import { AUTH_TOKEN } from './constants'
12-
import { ApolloLink } from 'apollo-client-preset'
1312
import { setContext } from 'apollo-link-context'
1413

15-
const httpLink = new HttpLink({ uri: 'http://localhost:4000' })
16-
// const authLink = setContext((_, { headers }) => {
17-
// // get the authentication token from local storage if it exists
18-
// const token = localStorage.getItem('token')
19-
// // return the headers to the context so httpLink can read them
20-
// return {
21-
// headers: {
22-
// ...headers,
23-
// authorization: token ? `Bearer ${token}` : ''
24-
// }
25-
// }
26-
// })
14+
const httpLink = createHttpLink({
15+
uri: 'http://localhost:4000',
16+
})
2717

28-
const authLink = new ApolloLink((operation, forward) => {
18+
const authLink = setContext((_, { headers }) => {
2919
const token = localStorage.getItem(AUTH_TOKEN)
30-
const authorizationHeader = token ? `Bearer ${token}` : null
31-
operation.setContext({
20+
return {
3221
headers: {
33-
authorization: authorizationHeader
22+
...headers,
23+
authorization: token ? `Bearer ${token}` : ''
3424
}
35-
})
36-
return forward(operation)
25+
}
3726
})
3827

3928
const client = new ApolloClient({

yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ apollo-client@^2.2.2:
199199
optionalDependencies:
200200
"@types/async" "2.0.47"
201201

202+
apollo-link-context@^1.0.7:
203+
version "1.0.7"
204+
resolved "https://registry.yarnpkg.com/apollo-link-context/-/apollo-link-context-1.0.7.tgz#3719697a677d3af5de500b7e4cbb6c0d0bf3bef3"
205+
dependencies:
206+
apollo-link "^1.2.1"
207+
202208
apollo-link-dedup@^1.0.0:
203209
version "1.0.8"
204210
resolved "https://registry.yarnpkg.com/apollo-link-dedup/-/apollo-link-dedup-1.0.8.tgz#8c3028cf32557bd040ab6ba8856f38067bdacead"

0 commit comments

Comments
 (0)