Skip to content

Commit

Permalink
feat: Sample auth
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Jan 17, 2020
1 parent d6d5c76 commit ef30a6e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 14 additions & 1 deletion examples/react-graphql/client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ import ApolloClient from 'apollo-boost'
import { ApolloProvider } from 'react-apollo'
import * as serviceWorker from './serviceWorker'

const client = new ApolloClient({ uri: 'http://localhost:4000/' })
const client = new ApolloClient({
uri: 'http://localhost:4000/',

// Authorization is out of scope for this example,
// but this is where you could add your auth logic
request: operation => {
const token = 'hardcoded-example-token'
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : '',
},
})
},
})

ReactDOM.render(
<ApolloProvider client={client}>
Expand Down
11 changes: 10 additions & 1 deletion examples/react-graphql/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ const server = new ApolloServer({
W3c.Gql.resolvers,
SD.Gql.resolvers,
),
context: () => ({ core, dataStore }),
context: ({ req }) => {
// Authorization is out of scope for this example,
// but this is where you could add your auth logic
const token = req.headers.authorization || ''
if (token !== 'Bearer hardcoded-example-token') {
throw Error('Auth error')
}

return { core, dataStore }
},
introspection: true,
})

Expand Down

0 comments on commit ef30a6e

Please sign in to comment.