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.
@kal_b This generally happens when you call payload.authenticate() before you init payload
yeah, i have made sure to initialize payload before calling it....and I am not calling it just passing a reference
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);
};
The only difference I am seeing is that you are awaiting the payload init.
ah it must be awaited
want to try that first?
Okay, let me try that out.
Create a start method like I shared above and let me know if it persists
Yeah, I created an IIFE and made it an async arrow function so that I could await the init and it worked 😀😀
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!
The should change the docs
Yeah thanks.
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();
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'
Good find!
@jesschow @jmikrut
https://payloadcms.com/docs/authentication/using-middlewaredoes not use the async method for initialization
Thanks for bringing this up - @jesschow can you update those docs?
Any solution for this? I made sure I am using the correct API KEY I am still getting this error @jmikrut
what does the header look like that you are using?
What header?
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?
It's my issue we are upgrading our dependencies and these are issues caused due to that I believe
@notchr Thank you very much 🙏
Any time!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.