middleware issue
Unanswered
Squidward posted this in #help-forum
hello community kindly help me on this issue
my middleware does not work corrrectly
i am trying to intercept requests to some routes then check if i am loggged in or not before letting the request slide
if user is not logged in redirect to login route
but then the issue is my middleware always redirect even when user is logged in
i had to cancel my pages and leave only api routes so my user will atleast see a ui
but i still have the issue since the api routes will be used in pages
note: i am using nextauth.js for authentication
this is my middleware code
import { getToken } from "next-auth/jwt";
import { NextResponse } from "next/server";
export async function middleware(req) {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
const protectedPaths = [
// "/admin",
// "/admin/settings",
// "/admin/addProject",
// "/admin/messages",
// "/admin/projects",
"/api/settings", "/api/settings/about",
"/api/settings/capabilities",
"/api/settings/socials",
"/api/settings/testimonials",
];
const signinPath = ['/auth/signin']
if (
protectedPaths.some(
(path) => req.nextUrl.pathname.startsWith(path) && !token
)
) {
return Response.redirect(new URL("/auth/signin", req.url), 302);
}
// else if (signinPath.some(
// (path) => req.nextUrl.pathname.startsWith(path) && token
// )) {
// return Response.redirect(new URL("/admin", req.url), 302);
// }
return NextResponse.next();
}
export const config = {
matcher: [
// "/admin",
// "/admin/settings",
// "/admin/addProject",
// "/admin/messages",
// "/admin/projects",
"/api/settings", "/api/settings/about",
"/api/settings/capabilities",
"/api/settings/socials",
"/api/settings/testimonials",
],
};
kindly assist
i have tried all my best but no success
note everything works well when i remove this middleware code
my middleware does not work corrrectly
i am trying to intercept requests to some routes then check if i am loggged in or not before letting the request slide
if user is not logged in redirect to login route
but then the issue is my middleware always redirect even when user is logged in
i had to cancel my pages and leave only api routes so my user will atleast see a ui
but i still have the issue since the api routes will be used in pages
note: i am using nextauth.js for authentication
this is my middleware code
import { getToken } from "next-auth/jwt";
import { NextResponse } from "next/server";
export async function middleware(req) {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
const protectedPaths = [
// "/admin",
// "/admin/settings",
// "/admin/addProject",
// "/admin/messages",
// "/admin/projects",
"/api/settings", "/api/settings/about",
"/api/settings/capabilities",
"/api/settings/socials",
"/api/settings/testimonials",
];
const signinPath = ['/auth/signin']
if (
protectedPaths.some(
(path) => req.nextUrl.pathname.startsWith(path) && !token
)
) {
return Response.redirect(new URL("/auth/signin", req.url), 302);
}
// else if (signinPath.some(
// (path) => req.nextUrl.pathname.startsWith(path) && token
// )) {
// return Response.redirect(new URL("/admin", req.url), 302);
// }
return NextResponse.next();
}
export const config = {
matcher: [
// "/admin",
// "/admin/settings",
// "/admin/addProject",
// "/admin/messages",
// "/admin/projects",
"/api/settings", "/api/settings/about",
"/api/settings/capabilities",
"/api/settings/socials",
"/api/settings/testimonials",
],
};
kindly assist
i have tried all my best but no success
note everything works well when i remove this middleware code