我通过Github使用next-auth进行了auth。使用Github登录后,客户端上的next-auth/react
中的useSession
可以正常工作。并且来自next-auth/next
的getServerSession
工作正常。但是中间件总是重定向(对于登录用户和未登录用户)。
我使用的是Next.js 13和app router以及next-auth 4。
中间件是:
export { default } from 'next-auth/middleware'
export const config = { matcher: ["/path_with_auth1", "/path_with_auth2"] }
字符串
选项为:
export const options: NextAuthOptions = {
providers: [
GitHubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
}),
],
adapter: PrismaAdapter(prisma),
secret: process.env.NEXTAUTH_SECRET,
};
型
app/API/auth/[...nextauth]/route.ts
import NextAuth from 'next-auth';
import { options } from './options'
const handler = NextAuth(options);
export { handler as GET, handler as POST}
型
我希望它只重定向未登录的用户。你能帮我吗?如何使其正确工作?
1条答案
按热度按时间ny6fqffe1#
根据nextAuth文档,默认情况下会话策略设置为
jwt
,但当提供适配器时,它默认为database
对于我来说,将会话策略设置为
jwt
解决了这个问题。您的选项现在应该如下所示:
字符串
购买这样做,你是覆盖默认策略从数据库到jwt,因为你已经提供了一个适配器