Skip to content

Instantly share code, notes, and snippets.

@mattfysh
Last active June 7, 2023 14:45
Show Gist options
  • Select an option

  • Save mattfysh/14d37f808583e444be6f40bc939ebe38 to your computer and use it in GitHub Desktop.

Select an option

Save mattfysh/14d37f808583e444be6f40bc939ebe38 to your computer and use it in GitHub Desktop.

Revisions

  1. mattfysh revised this gist Jul 7, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ const amplifyAuthExchange = authExchange({
    getAuth: async () => {
    const session = await Auth.currentSession()
    if (session) {
    // defines the authState elsewhere
    return {
    token: session.getAccessToken().getJwtToken(),
    }
  2. mattfysh revised this gist Jul 7, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion client.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ import { set } from 'lodash'

    const amplifyAuthExchange = authExchange({
    addAuthToOperation: ({ authState, operation }) => {
    if (!authState || !authState.token) {
    if (!authState?.token) {
    return operation
    }
    const newContext = produce(operation.context, context => {
  3. mattfysh revised this gist Jul 5, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    import { makeOperation } from '@urql/svelte'
    import { authExchange } from '@urql/exchange-auth'
    import { Auth } from 'aws-amplify'
    import produce from 'immer'
  4. mattfysh revised this gist Jul 5, 2021. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    import { authExchange } from '@urql/exchange-auth'
    import { Auth } from 'aws-amplify'
    import produce from 'immer'
    import { set } from 'lodash'

    const amplifyAuthExchange = authExchange({
    addAuthToOperation: ({ authState, operation }) => {
    if (!authState || !authState.token) {
  5. mattfysh created this gist Jul 5, 2021.
    32 changes: 32 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const amplifyAuthExchange = authExchange({
    addAuthToOperation: ({ authState, operation }) => {
    if (!authState || !authState.token) {
    return operation
    }
    const newContext = produce(operation.context, context => {
    set(context, 'fetchOptions.headers.Authorization', authState.token)
    })
    return makeOperation(operation.kind, operation, newContext)
    },
    willAuthError: ({ authState }) => {
    try {
    const [,payload] = authState.token.split('.')
    const { exp } = JSON.parse(Buffer.from(payload, 'base64'))
    return exp * 1000 < Date.now()
    } catch(e) {
    return true
    }
    },
    didAuthError: ({ error }) =>
    error.graphQLErrors.some(e => e.message === 'Unauthorized'),
    getAuth: async () => {
    const session = await Auth.currentSession()
    if (session) {
    return {
    token: session.getAccessToken().getJwtToken(),
    }
    }
    Auth.signOut()
    return null
    },
    })