我已经使用nextAuth和Next js 4.23.1创建了一个会话,并且它可以工作,但是我不知道如何在我的中间件中获取会话或令牌,我try 将处理程序传递给getSession()并执行任何工作.
我可以通过useSession在任何Use CLINTE组件中获取会话
我如何在服务器端获得会话和/或令牌,谢谢您的帮助
in api/auth/[..nextauth]/route个
import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import { _AUTH } from "@/app/services/shared/endpoints";
export const handler = NextAuth({
session: {
strategy: 'jwt'
},
providers: [
CredentialsProvider({
async authorize(credentials, req){
const res = await fetch(_AUTH._AUTH_LOGIN, {
method: 'POST',
body: JSON.stringify({user:{...credentials}}),
headers: { "Content-Type": "application/json" }
})
const user = await res.json()
if (!user) {
return
} else {
return user
}
}
})
],
pages: {
signIn: '/login',
}
})
export { handler as GET, handler as POST }
middleware个
import { handler } from "./app/api/auth/[...nextauth]/route"
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getSession } from "next-auth/react"
export async function middleware(request: NextRequest) {
const session = await getSession(handler)
if(session !== undefined || session !== null){
return NextResponse.redirect(new URL('/empresa/mis-empresas', request.url))
}
}
export const config = {
matcher: [
'/empresa/mis-empresas',
]
}