Middleware next auth
Unanswered
Maltese posted this in #help-forum
MalteseOP
When this middleware function is triggered?
export default withAuth(
function middleware(req) {27 Replies
@Maltese When this middleware function is triggered? js
export default withAuth(
function middleware(req) {
You’ve the flexibility to define it
By exporting a matcher
MalteseOP
yes
I did that
but somethins its working, sometimes not
I mean, if I refresh my page now it will run, but if I refresh it again and again ( without cache ) it wont run
@Maltese but somethins its working, sometimes not
What do you mean, whenever there is a non cached request from client side it’ll work
@Maltese I mean, if I refresh my page now it will run, but if I refresh it again and again ( without cache ) it wont run
You can export that route as dynamic
Search docs for disabling cache
MalteseOP
so because my route is static it will run the middleware only once?
isnt middleware for every request?
@Maltese isnt middleware for every request?
Unique request
MalteseOP
what unique request means
I put
export const dynamic = 'force-dynamic' and no result@Maltese what unique request means
IIRC, if the cookies or headers are different
Or Params
@Maltese I put `export const dynamic = 'force-dynamic'` and no result
After putting, restart app once.
@"use php" After putting, restart app once.
MalteseOP
still no result
Try deleting .next folder
If still doesn’t work, I’ll check once I reach home
MalteseOP
here is my repo, if you can please give it a try, because I can't figure out why the middleware is not running on every request - https://github.com/bockster6669/blog-app
@"use php" If still doesn’t work, I’ll check once I reach home
MalteseOP
thank u
MalteseOP
if I use
export default function middleware() {
console.log('middleware ran')
} instead of export default withAuth(
function middleware(req) {
console.log('middleware ran')
},
); then everything works as expected, the middleware is ran for every request, but if I use withAuth middleware than its not running for every requestMalteseOP
interesting but if I remove the regex and instead I put hard coded paths, then it works fine
export const config = {
matcher: ['/create-post', '/', '/about'],
};
but the regex I am using its from Clerk and it should cover all routes
export const config = {
matcher: ['/create-post', '/', '/about'],
};
but the regex I am using its from Clerk and it should cover all routes
umm can you send the regex used by clerk
@Maltese if I use js
export default function middleware() {
console.log('middleware ran')
}
instead of js
export default withAuth(
function middleware(req) {
console.log('middleware ran')
},
);
then everything works as expected, the middleware is ran for every request, but if I use withAuth middleware than its not running for every request
Greenish Elaenia
I've only seen the docs write it the first way. Where are you getting the withAuth from?
@Greenish Elaenia I've only seen the docs write it the first way. Where are you getting the withAuth from?
MalteseOP
its from next-auth