gpt4 book ai didi

How to get Session from Next-Auth in API Route [duplicate](如何在API Route中从Next-Auth获取Session [duplicate])

转载 作者:bug小助手 更新时间:2023-10-26 20:58:11 24 4
gpt4 key购买 nike




when I print my session from next auth in a component like this I can see all of its data.

当我在这样的组件中打印下一次身份验证中的会话时,我可以看到它的所有数据。


const session = useSession();

// ...
<p>{JSON.stringify(session)}</p>

I need to access the session's content in one of my Endpoints, and when I run it, it returns null (in the network tab) in the response, even though I can see that the same exact component is printing it on the screen. (It should not return null because I'm logged in!)

我需要在一个端点中访问会话的内容,当我运行它时,它在响应中返回null(在网络选项卡中),即使我可以看到相同的组件正在屏幕上打印它。(It不应该返回null,因为我已经登录了!)


I've tried this approach:

我试过这种方法:


app/api/entries/route.js:

App/api/条目/route.js:


    export async function GET(req) { try { const session = await getSession({ req }); return  NextResponse.json(session); } catch (error) { throw new Error("Something went wrong."); } }


I've checked this GitHub issue where someone is encountering a similar bug, but the code that fixed it is what I have implemented. GitHub Issue

我检查了这个GitHub问题,其中有人遇到了类似的错误,但修复它的代码就是我实现的代码。GitHub问题


更多回答

Checkout next-auth.js.org/getting-started/client#getsession.

检出next-auth.js.org/getting-started/client#getsession.

thats client side though

不过,这是客户端

This is what I wanted you to realize.

这就是我想让你意识到的。

优秀答案推荐

I had to use getServerSession() instead of getSession().

我不得不使用getServerSession()而不是getSession()。


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com