我们目前正在使用 NextJS 13 开发一个应用程序next-auth
,到目前为止一切都很好。我们的应用程序使用自定义登录页面CredentialsProvider
我们使用 next-auth 中间件来保护我们的路由。我们希望阻止我们的用户访问/login
如果他们已经通过身份验证。我们成功地在客户端实现了这一点useSession()
但我们正在寻找一种方法来在中间件中使用这种逻辑。以目前的水平可以实现吗next-auth
中间件实现?以下是我们当前的中间件和路由配置。谢谢。
//middleware.ts
import withAuth from 'next-auth/middleware';
export default withAuth({
pages: {
signIn: `/login`,
},
});
and
//route.ts
import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
const handler = NextAuth({
pages: {
signIn: `/login`,
},
session: {
strategy: 'jwt',
},
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
username: { label: 'Username', type: 'text' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials, req) {
//auth logic here
},
}),
],
});
export { handler as GET, handler as POST };