English 中文(简体)
我如何在下个小队使用下个接听器,在近海中使用13个信塔服务器部件
原标题:How do I use next auth getServerSession in next js 13 beta server component in app directory

I m使用下 au诉4,下 j13个床子,配备服务器部件,所有物品都进行罚款。 但是,我有这样一种情况,即我需要知道贴上门的用户,因为我用下 au,能够参加本届会议,我可以使用Session(Session),但我需要把该部件当作客户,因此,我想在服务器上使用该部件,因为我可以使用 API器,因为我可以使用 re子;反之,但在下游乐器上,我可以这样做。 请让我知道,你是否知道如何解决这个问题。 谢谢。

import { getServerSession } from "next-auth";
import { authOptions } from "@/pages/api/auth/[...nextauth]";

const Test = async () => {
    const user_id = 1; // How do I get user id from session, user_id is available in session

    // I don t have access req & res object in server component.
    const data = await getServerSession(request, response, authOptions);

    console.log(data);

});
    return (
        <></>
    );
};

export default Test;

没有足够的信息

最佳回答

我在接下来的13个小节中找到了答案,你赢得了使用请求和安局的需要。 反应标语,只是使用制裁,它将工作。

import { getServerSession } from "next-auth";
import { authOptions } from "@/pages/api/auth/[...nextauth]";

const Test = async () => {

    const data = await getServerSession(authOptions);

    console.log(data);

});
    return (
        <></>
    );
};

export default Test;
问题回答

If you are trying to do the same this with the app router, its the same flow except the authOptions will be added to app/api/auth/[...nextauth]/route.(ts/js).

如果你在接下来的13岁时重新使用该名录,并像现在这样从公路操作者那里开始下游:

src/app/api/auth/[......nextauth]/route.js

import NextAuth from  next-auth 
import GoogleProvider from  next-auth/providers/google 

const handler = NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET
    })
  ]
})

export { handler as GET, handler as POST }

then you can just using getServerSession in server components without passing anything

src/app/layout.js

import { getServerSession } from  next-auth 

...

export default async function Layout ({ children }) {
  const data = await getServerSession()

  return (
    <h5>Data: {JSON.stringify(data)}</h5>
  )
}





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签