Ter*_*ill 10 middleware auth0 next.js
我在 /pages/api/_middleware.js 中有这段代码:
import { getSession } from '@auth0/nextjs-auth0'
export default async function middleware(req, ev) {
const session = await getSession(req)
console.log(session)
return NextResponse.next()
}
Run Code Online (Sandbox Code Playgroud)
每当我运行一个 API 调用时,我都会收到以下消息:
错误 - node_modules@auth0\nextjs-auth0\dist\index.browser.js?b875 (11:0) @ Object.getSession 错误:getSession 方法只能从服务器端使用
我不确定 lib 是否可以@auth0/nextjs-auth0
,但我只是懒洋洋地检查appSession
cookie 是否在存储中,如下所示:
import type { NextRequest } from 'next/server'
export function middleware(req: NextRequest) {
if (req.nextUrl.pathname === '/' && req.cookies.appSession) {
return Response.redirect('/app')
}
if (req.nextUrl.pathname === '/app' && !req.cookies.appSession) {
return Response.redirect('/')
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3952 次 |
最近记录: |