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 表示“可以从提供商那里返回更多信息”,但到目前为止,我还没有运气。 您将不胜感激。 预先感谢。
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,
}
}
})