Skip to content

Commit

Permalink
feat(presenter): authorization header
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogge committed Mar 18, 2024
1 parent a21b47a commit 26cd72e
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions presenter/renderer/plugins/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { ApolloClient, InMemoryCache } from '@apollo/client/core'
import { setContext } from '@apollo/client/link/context'
import { createHttpLink } from '@apollo/client/link/http'
// import { onError } from '@apollo/client/link/error'
import { User } from 'oidc-client-ts'

import { ENDPOINTS } from '#src/env'

// HTTP connection to the API
const authLink = setContext((_, { headers }) => {
let token = ''
const auth = localStorage.getItem('auth')
if (auth) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const parsed = JSON.parse(auth)
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (parsed.user) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const user: User = parsed.user
token = user.access_token ? user.access_token : ''
}
}
return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
},
}
})

const httpLink = createHttpLink({
// You should use an absolute URL here
uri: ENDPOINTS.GRAPHQL_URI,
})

Expand All @@ -23,12 +45,10 @@ const errorLink = onError(({ graphQLErrors, networkError }) => {
})
*/

// Cache implementation
const cache = new InMemoryCache()

// Create the apollo client
export const apolloClient = new ApolloClient({
ssrMode: true,
link: httpLink, // errorLink.concat(httpLink),
link: authLink.concat(httpLink), // errorLink.concat(httpLink),
cache,
})

0 comments on commit 26cd72e

Please sign in to comment.