I recently implemented the next-auth middleware to protect all pages on my website. I followed the official documentation on next-auth (next-auth) and verified that it successfully redirects users who are not logged in.
However, I encountered an issue where even when I am logged in, it keeps redirecting me to the login page.
If anyone can help me diagnose what I might have done wrong, I would greatly appreciate it.
[...nextauth].ts
import NextAuth, { NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import client from "@/libs/server/client";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { Session } from "next-auth/";
interface UserSession extends Session {
id: string;
}
export const authOptions: NextAuthOptions = {
// Configure one or more authentication providers
adapter: PrismaAdapter(client),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}),
],
callbacks: {
session: ({ session, user }) => {
(session as UserSession).id = user.id;
return session;
},
redirect: ({ url, baseUrl }) => {
return baseUrl;
},
},
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: "/enter",
},
};
export default NextAuth(authOptions);
middleware.ts
export { default } from "next-auth/middleware";
export const config = {
matcher: ["/mypage","/with", "/product/:path*"],
};