在 NextJS 中使用 Next-Auth 时不间断重新渲染
P粉811329034
P粉811329034 2023-09-05 11:08:49
[React讨论组]
<p>这是我的“/app/api/auth/[...nextauth]/route.js”</p> <pre class="brush:php;toolbar:false;">import NextAuth from &quot;next-auth/next&quot;; import GoogleProvider from &quot;next-auth/providers/google&quot;; export const authOptions = { providers: [ GoogleProvider({ clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }), ], }; const handler = NextAuth(authOptions); export { handler as GET, handler as POST };</pre> <p>和sessionProvider.js</p> <pre class="brush:php;toolbar:false;">&quot;use client&quot;; import React from &quot;react&quot;; import { SessionProvider } from &quot;next-auth/react&quot;; const Sessionprovider = ({ children }) =&gt; { return &lt;SessionProvider&gt;{children}&lt;/SessionProvider&gt;; }; export default Sessionprovider;</pre> <p>我用这个将子元素包装在 Layout.js 中</p> <pre class="brush:php;toolbar:false;">&lt;html lang=&quot;en&quot;&gt; &lt;body className={inter.className}&gt; &lt;Sessionprovider&gt;{children}&lt;/Sessionprovider&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p>这是我的 page.js</p> <pre class="brush:php;toolbar:false;">&quot;use client&quot;; import { getServerSession } from &quot;next-auth&quot;; import { signIn, signOut, useSession } from &quot;next-auth/react&quot;; import { authOptions } from &quot;./api/auth/[...nextauth]/route&quot;; export default async function Home() { const { data: session, status } = useSession(); console.log(session); const _signInWithGoogle = async () =&gt; { await signIn(&quot;google&quot;); }; return ( &lt;div className=&quot;flex justify-center items-center flex-col gap-3 p-5&quot;&gt; &lt;h1 className=&quot;font-semibold text-lg&quot;&gt;Home Page&lt;/h1&gt; {session ? ( &lt;button onClick={() =&gt; signOut()} className=&quot;p-1 bg-blue-400 text-white font-semibold rounded-md hover:bg-blue-500&quot; &gt; Sign Out &lt;/button&gt; ) : ( &lt;button onClick={_signInWithGoogle} className=&quot;p-1 bg-blue-400 text-white font-semibold rounded-md hover:bg-blue-500&quot; &gt; Sign In &lt;/button&gt; )} &lt;/div&gt; ); }</pre> <p>当我运行应用程序时,它会不停地重新渲染。对此的任何帮助将不胜感激。由于控制台显示用户对象,它仍然保留在登录组件中。</p>
P粉811329034
P粉811329034

全部回复(1)
P粉828463673

您需要从客户端组件中删除async

"use client";

export default function Home() {
}

https://github.com/vercel/next.js/issues/48822

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号