Skip to content

Commit

Permalink
Fix Maximum Call Stack Size Exceeded when using requireAuth with Func… (
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 committed Apr 12, 2022
1 parent ea06d5f commit 5114405
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
context as globalContext,
getAsyncStoreInstance,
setContext,
} from '../../globalContext'

describe('Global context with context isolation', () => {
beforeAll(() => {
process.env.DISABLE_CONTEXT_ISOLATION = '0'
})

it('Should work when assigning directly into context', async () => {
const asyncStore = getAsyncStoreInstance()

asyncStore.run(new Map(), () => {
// This is the actual test
globalContext.myNewValue = 'bazinga'

expect(globalContext.myNewValue).toBe('bazinga')
})

// Check that context was isolated
expect(globalContext.myNewValue).not.toBe('bazinga')
})

it('Should work when using setContext', async () => {
const asyncStore = getAsyncStoreInstance()

asyncStore.run(new Map(), () => {
// This is the actual test
setContext({ anotherValue: 'kittens' })

expect(globalContext.anotherValue).toBe('kittens')
})

// Check that context was isolated
expect(globalContext.anotherValue).not.toBe('kittens')
})
})
2 changes: 0 additions & 2 deletions packages/graphql-server/src/functions/useRequireAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { getAuthenticationContext } from '@redwoodjs/api'

import {
getAsyncStoreInstance,
setContext,
context as globalContext,
} from '../globalContext'

Expand Down Expand Up @@ -39,7 +38,6 @@ export const useRequireAuth = ({ handlerFn, getCurrentUser }: Args) => {
: null

globalContext.currentUser = currentUser
setContext(globalContext)
}
} catch (e) {
return {
Expand Down
7 changes: 7 additions & 0 deletions packages/graphql-server/src/globalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export const createContextProxy = () => {
const ctx = store?.get('context') || {}
return ctx[property]
},
set: (_target, property: string, newVal) => {
const store = getAsyncStoreInstance().getStore()
const ctx = store?.get('context') || {}
ctx[property] = newVal
store?.set('context', ctx)
return true
},
})
} else {
return GLOBAL_CONTEXT
Expand Down

0 comments on commit 5114405

Please sign in to comment.