redwood: Context does not always expose the currentUser

I’m using several services that rely on authentified user and I noticed a buggy behaviour.

This would be my sample code:

export const createStructure = async ({ input }: CreateStructureArgs) => {
  const { accountId, ...user } = context?.currentUser

  // irrelevant code goes here
}

It is tested using this:

scenario('Creates a Sale Enabled Team', async (scenario) => {
    mockCurrentUser(scenario.user.one)

    const result = await createStructure({
      input: Seed.Structure(),
    })

  // irrelevant expectations shamefully hidden here

  })

Now here’s what’s blocking me:

  • my tests on that service rarely fail
  • they mostly fail when tests are run only on the filtered file
  • they almost never fail when all tests are ran
  • my front application will always crash on that code, locally and online.

Output of the error:

api | TypeError: Cannot destructure property 'accountId' of '_b' as it is undefined.
api | 
api | at /Workspace/sport-office/api/dist/services/structures/structures.js(Object.createStructure):236
api | 231    input
api | 232  }) => {
api | 233    var _a;
api | 234    import_logger.logger.debug(import_api.context);
api | 235    const _b = (_a = import_api.context) == null ? void 0 : _a.currentUser, {
api | 236      accountId
api | 237    } = _b, user = __objRest(_b, [
api | 238      "accountId"
api | 239    ]);

Introduced a couple of weeks ago but I thought it was some random issue, now it’s more pressing.

RedwoodJS v0.36.4 Auth provider: Firebase

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Hi @papaponmx - could you provide a bit more detail please?

Please could you include: a) What auth provider you’re using b) Snippet of your getCurrentUser function src/lib/auth (are you sure you’re not swallowing the errors here?) c) What conditions you see this under? e.g. graphql query with @requireAuth directive, from a private page d) Could you confirm if importing context like this: import {context} from '@redwoodjs/graphql-server alleviates your issue

(cc @noire-munich, same for you please if you experience the same on v37)

I can confirm it’s no longer an issue on v0.37 for us 👍

Closing for now. Please re-open as needed!

Hi @papaponmx - could you provide a bit more detail please?

Please could you include: a) What auth provider you’re using b) Snippet of your getCurrentUser function src/lib/auth (are you sure you’re not swallowing the errors here?) c) What conditions you see this under? e.g. graphql query with @requireAuth directive, from a private page d) Could you confirm if importing context like this: import {context} from '@redwoodjs/graphql-server alleviates your issue

(cc @noire-munich, same for you please if you experience the same on v37)