fix: multiple small fixes across auth

This commit is contained in:
Sérgio Salgado 2021-04-16 05:53:22 +01:00 committed by Josh Harvey
parent 9fa97725ec
commit bbc37c0202
22 changed files with 296 additions and 291 deletions

View file

@ -12,7 +12,7 @@ import AppContext from 'src/AppContext'
const URI =
process.env.NODE_ENV === 'development' ? 'https://localhost:8070' : ''
const getClient = (history, location, setUserData) =>
const getClient = (history, location, setUserData, setRole) =>
new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
@ -28,6 +28,21 @@ const getClient = (history, location, setUserData) =>
})
if (networkError) console.log(`[Network error]: ${networkError}`)
}),
new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
const context = operation.getContext()
const {
response: { headers }
} = context
if (headers) {
const role = headers.get('role')
setRole(role)
}
return response
})
}),
new HttpLink({
credentials: 'include',
uri: `${URI}/graphql`
@ -52,8 +67,8 @@ const getClient = (history, location, setUserData) =>
const Provider = ({ children }) => {
const history = useHistory()
const location = useLocation()
const { setUserData } = useContext(AppContext)
const client = getClient(history, location, setUserData)
const { setUserData, setRole } = useContext(AppContext)
const client = getClient(history, location, setUserData, setRole)
return <ApolloProvider client={client}>{children}</ApolloProvider>
}