Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

TypeError: Router.use() requires a middleware function

default discord avatar
kal_b2 years ago
20

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
    notchr2 years ago
    @774334428163080201

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

  • default discord avatar
    kal_b2 years 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
    notchr2 years 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_b2 years ago


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

  • default discord avatar
    notchr2 years ago

    ah it must be awaited



    want to try that first?

  • default discord avatar
    kal_b2 years ago

    Okay, let me try that out.

  • default discord avatar
    notchr2 years ago

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

  • default discord avatar
    kal_b2 years 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
    notchr2 years 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_b2 years ago

    The should change the docs



    Yeah thanks.

  • default discord avatar
    notchr2 years 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_b2 years 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
    notchr2 years ago

    Good find!



    @854377910689202256

    @364124941832159242

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

    does not use the async method for initialization

  • discord user avatar
    jmikrut
    2 years ago

    Thanks for bringing this up -

    @854377910689202256

    can you update those docs?

  • default discord avatar
    kal_b2 years ago

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

    @364124941832159242
  • discord user avatar
    jmikrut
    2 years ago

    what does the header look like that you are using?

  • default discord avatar
    kal_b2 years ago

    What header?

  • default discord avatar
    notchr2 years 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_b2 years ago

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



    @1049775120559898725

    Thank you very much 🙏

  • default discord avatar
    notchr2 years ago

    Any time!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.