如何在 Next Auth Google 提供商配置文件中添加自定义字段?

问题描述 投票:0回答:1
import Credentials from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
import { LoginSchema } from "@/schemas";
import type { NextAuthConfig } from "next-auth";
import { getUserbyEmail } from "@/data/user";
import Github from "next-auth/providers/github";
import Google from "next-auth/providers/google";

export default {
  providers: [
    Google({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
    Github({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
    }),
    Credentials({
      async authorize(credentials) {
        const validdatedFields = LoginSchema.safeParse(credentials);

        if (validdatedFields.success) {
          const { email, password } = validdatedFields.data;

          const user = await getUserbyEmail(email);
          if (!user || !user.password) return null;

          const passwordsMatch = await bcrypt.compare(password, user.password);

          if (passwordsMatch) return user;
        }

        return null;
      },
    }),
  ],
} satisfies NextAuthConfig;

我正在构建一个使用 Next AuthV5 进行身份验证的应用程序,我想构建一个自定义 Google 提供程序,它返回的字段不仅仅是 ID、名称、图像和电子邮件(这些是默认字段)。在我的用例中,我有一个用户名字段,我想将其添加到 Google 提供商。 Next Auth 表示“可以从提供商那里返回更多信息”,但到目前为止,我还没有运气。 您将不胜感激。 预先感谢。

next.js google-oauth next-auth
1个回答
0
投票
profile

。对于我的

GitHubProvider
,我有以下
export const githubProvider = GitHubProvider({
    id: "github"
    clientId: process.env.GITHUB_ID || "",
    clientSecret: process.env.GITHUB_SECRET || "",
    profile(profile: GithubProfile) {
        return {
            id: profile.id.toString(),
            name: profile.name || profile.login, // if user has not registered name
            roles: [], // custom stuff
            email: profile.email,
            image: profile.avatar_url,
        }
    }
})

	
© www.soinside.com 2019 - 2024. All rights reserved.