在应用程序目录中放置 _app.js 的位置是什么?

huangapple go评论66阅读模式
英文:

Where do you put _app.js in the app directory

问题

Documentation for next-auth (version 4) indicates that we need to put the service provider in:

pages/_app.js

I am following the example:
<https://next-auth.js.org/getting-started/example>

If I'm using the experimental app directory instead of the pages directory in nextjs 13, where do we put that _app.js file?

英文:

Documentation for next-auth (version 4) indicates that we need to put the service provider in:

pages/_app.js

I am following the example:
<https://next-auth.js.org/getting-started/example>

If I'm using the experimental app directory instead of the pages directory in nextjs 13, where do we put that _app.js file?

答案1

得分: 4

以下是代码部分的翻译:

不幸的是你必须有两个实现

在实验性应用程序目录中你需要包装根布局

// app/provider.js

"use client";

import { SessionProvider } from "next-auth/react";

export default function Providers({ children }) {
  return <SessionProvider>{children}</SessionProvider>;
}

然后在根布局中

import Provider from "./provider";

export default function RootLayout({ children }) {
  return (
    <html>
      <head />
      <body>
        <Provider>{children}</Provider>
      </body>
    </html>
  );
}

类似地,你需要包装 _app.js

import { SessionProvider } from "next-auth/react";

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

API 文件位于相同目录中:pages/api/auth/[...nextauth].js

英文:

Unfortunately, you have to have two implementations.

In experimental app directory you have to wrap root layout.

// app/provider.js

&quot;use client&quot;;

import { SessionProvider } from &quot;next-auth/react&quot;;

export default function Proiders({ children }) {
  return &lt;SessionProvider&gt;{children}&lt;/SessionProvider&gt;;
}

then in root layout

import Provider from &quot;./provider&quot;;

export default function RootLayout({ children }) {
  return (
    &lt;html&gt;
      &lt;head /&gt;
      &lt;body&gt;
        &lt;Provider&gt;{children}&lt;/Provider&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  );
}

similarly you have to wrap _app.js too

import { SessionProvider } from &quot;next-auth/react&quot;

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (
    &lt;SessionProvider session={session}&gt;
      &lt;Component {...pageProps} /&gt;
    &lt;/SessionProvider&gt;
  )
}

api file is the same directory. pages/api/auth/[...nextauth].js

huangapple
  • 本文由 发表于 2023年2月14日 06:20:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定