Why is my middleware not working?
Answered
American Pipit posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
American PipitOP
When I'm on the
This is my project structure:
/jobs
path, this middleware doesn't return the NextResponse.redirect('/login')
and I don't know why. I am not authenticated, and neither does the middleware.ts
console.log anything. I've double-checked the paths that I'm matching.import { NextResponse, type NextRequest } from 'next/server';
import { createClient } from '@/utils/supabase/middleware';
import { redirect } from 'next/navigation';
export async function middleware(request: NextRequest) {
const { supabase, response } = createClient(request);
const {
data: { session },
error,
} = await supabase.auth.getSession();
if (request.nextUrl.pathname.startsWith('/login') && session) {
redirect('/');
}
if (request.nextUrl.pathname.startsWith('/jobs') && !session) {
return NextResponse.redirect('/login');
}
return response;
}
export const config = {
matcher: ['/login/:path*', '/jobs/:path*'],
};
This is my project structure:
![Image](https://cdn.discordapp.com/attachments/1178791797565161472/1178791797976223755/Screenshot_2023-11-27_at_21.17.32.png?ex=65776e69&is=6564f969&hm=0ab33b2252706000e8ecbcdb758e02bc113ea3f8f682cf5ed69b9dbb731e995f&)
Answered by vr1
have you tried moving the middleware inside src? I got a warning a few days ago
4 Replies
![Avatar](https://cdn.discordapp.com/avatars/807096603826847774/28b469de39df9d9194e5a0e933334a4a.webp?size=256)
vr1
have you tried moving the middleware inside src? I got a warning a few days ago
Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
American PipitOP
yep, that fixed it
thanks for your answer 😦
🙂 *