Reinstall node_modules on payload latest runs into middleware issue again

default discord avatar
thisisnotchris
2 months ago
45

Hey all,



Just updated payload and reinstalled my node_modules folder. Noticing I'm hitting the middleware issue with payload.authenticate again. Also can confirm payload.init is before the auth call. What could be causing this?



Thank you!



@jmikrut





OK SO



This is weird



it works if..



payload.init({
    secret: process.env.PAYLOAD_SECRET,
    mongoURL: process.env.MONGODB_URI,
    express: app,
    onInit: () => {
      payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
      router.use(payload.authenticate); // OK
    },
  });
  
  // router.use(payload.authenticate);  (undefined)


but I doubt it's ok to call .use onInit ?



@patrikkozak ever seen this before?



Issue is currently blocking me at work :X

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    it's fine to do that in

    onInit

    for sure but the real question is, what has changed?

  • default discord avatar
    thisisnotchris
    2 months ago

    Exactly, nothing has 😮 (at least in my code)



    We tried to install the lexical plugin earlier today

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    can you show me your full server file?

  • default discord avatar
    thisisnotchris
    2 months ago

    And I then removed it



    sure thing



    https://gist.github.com/notchris/93c78db75e4cf8e9388c1b63ef8408e8


    Had to make a gist, dont have nitro on this work discord



    Also, everything works with the payload.authenticate inside the onInit function



    Otherwise, outside, it is undefined (even after init)

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    well i want to see what

    doesn't

    work



    where did you have it when it did not work?

  • default discord avatar
    thisisnotchris
    2 months ago

    ah here



    payload.init({
        secret: process.env.PAYLOAD_SECRET,
        mongoURL: process.env.MONGODB_URI,
        express: app,
        onInit: () => {
          payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
          router.use(payload.authenticate); // OK
        },
      });
      
      // router.use(payload.authenticate);  (undefined)


    payload.authenticate is undefined there



    if uncommented

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    yea



    so this is because you are not awaiting

    payload.init


    it's an async call

  • default discord avatar
    thisisnotchris
    2 months ago

    😮



    How did it work prior

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    it is probably just up to the luck of the draw



    sometimes it works, sometimes it doesn't depending on how fast your code executes payload.init

  • default discord avatar
    thisisnotchris
    2 months ago

    omg

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    maybe a dependency updated that slowed down mongo connection or something, or schema creation, or graphql, etc

  • default discord avatar
    thisisnotchris
    2 months ago

    its in the docs too



    how did i miss this?

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    no worries

  • default discord avatar
    thisisnotchris
    2 months ago

    Was it always wrapped in a func

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    well, that was one of the breaking changes in 1.6.0



    but it's been like that for over a month

  • default discord avatar
    thisisnotchris
    2 months ago

    ugh im such a dummy

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    maybe 2

  • default discord avatar
    thisisnotchris
    2 months ago

    One day im going to find a real bug that wasn't just me not reading correctly



    Thanks for your time man, my bad

  • discord user avatar
    jmikrut
    Payload Team
    2 months ago

    no problem!!!



    happy to help

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.