Skip to content

Instantly share code, notes, and snippets.

@saleebm
Last active August 1, 2021 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saleebm/e6855debe058cae2d675518bcf135ece to your computer and use it in GitHub Desktop.
Save saleebm/e6855debe058cae2d675518bcf135ece to your computer and use it in GitHub Desktop.
Auth Links for Apollo for WP-GraphQL
import fetch from 'isomorphic-unfetch'
import { v4 as uuid } from 'uuid'
const fetchNewAccessToken: FetchNewAccessToken = async (currentRefreshToken) => {
// console.log(`Refresh token: ${currentRefreshToken}`)
try {
const fetchResult = await fetch(GRAPHQL_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'credentials': 'same-origin' },
body: JSON.stringify({
query: `
mutation refreshJwtAuthToken {
refreshJwtAuthToken(input: {
clientMutationId: "${uuid()}",
jwtRefreshToken: "${currentRefreshToken}"
}) {
authToken
}
}
`,
}),
})
const refreshResponse = await fetchResult.json()
console.log(
`Refresh token response: ${refreshResponse.data?.refreshJwtAuthToken?.authToken}`,
)
if (!refreshResponse?.data?.refreshJwtAuthToken?.authToken) {
return undefined
}
return refreshResponse.data.refreshJwtAuthToken.authToken
} catch (e) {
console.error('Failed to fetch fresh access token', e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment