英文:
Profile id is missing in Google OAuth profile response - NextAuth
问题
I'm following this tutorial on how to add roles in next-auth session. Unfortunately, when I add the profile
property, I get undefined behavior with the profile missing. There are also TypeScript errors. Is this an error on my side, or is it a known bug, since I couldn't find anything on it.
Here's my code so far:
export const authOptions: AuthOptions = {
secret: process.env.NEXT_PUBLIC_SECRET!,
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
// profile: async (profile) => {
// return { ...profile, role: profile.role ?? Role.USER };
// },
}),
],
pages: {
signIn: "/",
},
adapter: PrismaAdapter(prisma),
};
As you can see, it's the same as the one from the tutorial. When I comment out the profile section, I get the expected behavior without the role. Any help would be appreciated!
Version of Next.js: 13.4.1 (app directory)
英文:
I'm following this tutorial on how to add roles in next-auth session.
Unfortunately, when I add profile
property, I get undefined behavior of the profile missing. There are also errors regarding typescript. Is this an error on my side, or is it a known bug, since I couldn't find anything on it.
Here's my code so far:
export const authOptions: AuthOptions = {
secret: process.env.NEXT_PUBLIC_SECRET!,
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
// profile: async (profile) => {
// return { ...profile, role: profile.role ?? Role.USER };
// },
}),
],
pages: {
signIn: "/",
},
adapter: PrismaAdapter(prisma),
};
as you can see it's the same as the one from the tutorial, when I comment out the profile section I get the expected behavior without role. Any help would be appreciated!
Version of Next.js: 13.4.1 (app directory)
答案1
得分: 7
I just added "id: profile.sub" to my GoogleProvider and I have no error since that.
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
authorization: {
params: {
prompt: "consent",
access_type: "offline",
response_type: "code"
}
},
async profile(profile) {
return {
id: profile.sub,
name: profile.name,
firstname: profile.given_name,
lastname: profile.family_name,
email: profile.email,
image: profile.picture,
}
},
}),
英文:
I just added "id: profile.sub" to my GoogleProvider and I have no error since that
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
authorization: {
params: {
prompt: "consent",
access_type: "offline",
response_type: "code"
}
},
async profile(profile) {
return {
id: profile.sub,
name: profile.name,
firstname: profile.given_name,
lastname: profile.family_name,
email: profile.email,
image: profile.picture,
}
},
}),
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论