TypeError: Router.use() requires a middleware function

default discord avatar
kal_b
8 months ago
31

We are trying to use payload.authenticate as an auth middleware but the error

TypeError: Router.use() requires a middleware function

keeps popping up. Anyone face the same issues or has solutions please help.

  • default discord avatar
    notchr
    8 months ago

    @kal_b This generally happens when you call payload.authenticate() before you init payload

  • default discord avatar
    kal_b
    8 months ago

    yeah, i have made sure to initialize payload before calling it....and I am not calling it just passing a reference

  • default discord avatar
    notchr
    8 months ago
    const start = async () => {
      // Initialize Payload
      await payload.init({
        secret: process.env.PAYLOAD_SECRET,
        mongoURL: process.env.MONGODB_URI,
        express: app,
        email: {
          fromName: "Admin",
          fromAddress: "admin@example.com",
          logMockCredentials: true, // Optional
        },
        onInit: async () => {
          payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
        },
      });
     // after init
    payload.authenticate()
    
      // Add your own express routes here
    
      app.listen(3000);
    };
  • default discord avatar
    kal_b
    8 months ago


    The only difference I am seeing is that you are awaiting the payload init.

    Screenshot_782.png
  • default discord avatar
    notchr
    8 months ago

    ah it must be awaited



    want to try that first?

  • default discord avatar
    kal_b
    8 months ago

    Okay, let me try that out.

  • default discord avatar
    notchr
    8 months ago

    Create a start method like I shared above and let me know if it persists

  • default discord avatar
    kal_b
    8 months ago

    Yeah, I created an IIFE and made it an async arrow function so that I could await the init and it worked 😀😀

  • default discord avatar
    notchr
    8 months ago

    Yay!



    That async change happened a few updates ago



    I think a lot of people missed it



    But im glad updating it to async worked!

  • default discord avatar
    kal_b
    8 months ago

    The should change the docs



    Yeah thanks.

  • default discord avatar
    notchr
    8 months ago

    Ah I think they did, per the official example:



    import express from "express";
    import payload from "payload";
    
    require("dotenv").config();
    const app = express();
    
    const start = async () => {
      await payload.init({
        secret: process.env.PAYLOAD_SECRET,
        mongoURL: process.env.MONGODB_URI,
        express: app,
      });
    
      app.listen(3000, async () => {
        console.log(
          "Express is now listening for incoming connections on port 3000."
        );
      });
    };
    
    start();
  • default discord avatar
    kal_b
    8 months ago

    Hey, but now It keeps responding with a

    (payload): Forbidden: You are not allowed to perform this action.


    Yeah I see that but it is not updated here '

    https://payloadcms.com/docs/authentication/using-middleware

    '

  • default discord avatar
    notchr
    8 months ago

    Good find!



    @jesschow @jmikrut

    https://payloadcms.com/docs/authentication/using-middleware

    does not use the async method for initialization

  • discord user avatar
    jmikrut
    Payload Team
    8 months ago

    Thanks for bringing this up - @jesschow can you update those docs?

  • default discord avatar
    kal_b
    8 months ago

    Any solution for this? I made sure I am using the correct API KEY I am still getting this error @jmikrut

  • discord user avatar
    jmikrut
    Payload Team
    8 months ago

    what does the header look like that you are using?

  • default discord avatar
    kal_b
    8 months ago

    What header?

  • default discord avatar
    notchr
    8 months ago

    So after you've called payload.authenticate() you're then trying to perform an action and that's where you get the error?



    Can we check out a code snippet of the action you're performing?

  • default discord avatar
    kal_b
    8 months ago

    It's my issue we are upgrading our dependencies and these are issues caused due to that I believe



    @notchr Thank you very much 🙏

  • default discord avatar
    notchr
    8 months ago

    Any time!

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.