首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Next-8月-自定义OAuth 2.0提供程序集成

Next-8月-自定义OAuth 2.0提供程序集成
EN

Stack Overflow用户
提问于 2021-07-02 12:16:29
回答 1查看 4.2K关注 0票数 2

我们有自己的OAuth 2.0实现,我正在使用next-auth在我的Next.js应用程序中连接和获取jwt。

输出:目前,我可以登录。但是,我在客户端获得了null作为令牌。

我已经实施了以下步骤:

  • 在顶层添加了next-auth提供程序组件.

代码语言:javascript
复制
    function MyApp({ Component, pageProps }) {
      return (
        <Provider
          options={{
            clientMaxAge: 0,
            keepAlive: 0,
          }}
          session={pageProps.session}>
          <Component {...pageProps} />
        </Provider>
      );
    }

文件:pages/api/auth/[...nextauth].js

代码语言:javascript
复制
export default NextAuth({
  providers: [
    {
      id: "quot-test",
      name: "quot-test",
      type: "oauth",
      version: "2.0",
      scope: "openid email",
      state: true,
      protection: "state",
      params: { grant_type: "authorization_code" },
      idToken: true,
      authorizationUrl:
        "http://localhost:9000/oauth2/authorize?response_type=code",
      accessTokenUrl: "http://localhost:9000/oauth2/token",
      requestTokenUrl: "http://localhost:9000/oauth2/jwks",
      clientId: process.env.QUOT_ID,
      clientSecret: process.env.QUOT_SECRET,
    },
  ],
  secret: process.env.SECRET,
  debug: true,
  session: {
    jwt: true,
    maxAge: 60 * 5,
  },
  jwt: {
    secret: process.env.SECRET,
    encryption: false,
  },
  callbacks: {
    async signin(user, account, profile) {
      console.log("user", user, account, profile);
      return true;
    },
    async jwt(token, user, account, profile, isNewUser) {
      console.log(token);
      console.log(user);
      console.log(account);
      console.log(profile);
      console.log(isNewUser);
      if (account.accessToken) {
        token.accessToken = account.accessToken;
      }
      return Promise.resolve(token);
    },
    async session(session, token) {
      console.log(session);
      console.log(token);
      return session;
    },
  },
});

这是第一个问题:不触发回调,终端上没有打印console.log。我上面的配置出现了什么问题吗?

  • 添加了signIn/ signOut,在主页上注册了auth服务。

代码语言:javascript
复制
import { signIn, signOut, useSession, getSession } from "next-auth/client";

export default function Home(){
  ...
  const [session, loading]  = useSession();
  console.log(session); // prints null after signin
  return(
  <button onClick={() =>signIn(null, { callbackUrl: "http://localhost:3000/happy" })}>
    Sign 
  </button>
  ...

这就是下一个问题所在:尽管signin函数中的callbackUrl指向页面‘/高兴’,但我仍然被带到主页!

  • I在根目录中有以下.env.local文件:

代码语言:javascript
复制
NEXTAUTH_URL=http://localhost:3000/

QUOTECH_ID=quot-test
QUOTECH_SECRET=[secret-from-authenticator-goes-here]
SECRET=[random-string-goes-here]

我错过了什么?为什么一旦我被客户端应用程序重定向,我就会得到null。在上面的[...nextauth].js文件中,有些选项被忽略了吗?

EN

回答 1

Stack Overflow用户

发布于 2021-08-05 15:46:14

配置文件值在自定义提供程序上是必需的,如果您更新了它们,那么它们就会正常工作。

代码语言:javascript
复制
{
  ...,
  profileUrl: string,
  profile: function,
  ...
}

在文档中,您可以看到这些区域是必需的字段。

profileUrl应该是获取配置文件信息的配置文件链接,而配置文件是您操作此配置文件url返回的函数,以适应下一个配置文件模式或您的自定义配置文件模式。

榜样;

代码语言:javascript
复制
{
 id: "quot-test",
 name: "quot-test",
 type: "oauth",
 version: "2.0",
 scope: "openid email",
 state: true,
 protection: "state",
 params: { grant_type: "authorization_code" },
 idToken: true,
 authorizationUrl: "http://localhost:9000/oauth2/authorize?response_type=code",
 accessTokenUrl: "http://localhost:9000/oauth2/token",
 requestTokenUrl: "http://localhost:9000/oauth2/jwks",
 profileUrl: "[ProfileURL]",
 profile(profile, tokens) {
  return {
          id: profile.id,
          name: profile.name,
          email: profile.email
         };
 },
 clientId: process.env.QUOT_ID,
 clientSecret: process.env.QUOT_SECRET,
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68225050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档