Profile id is missing in Google OAuth profile response – NextAuth

huangapple go评论92阅读模式
英文:

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:

  1. export const authOptions: AuthOptions = {
  2. secret: process.env.NEXT_PUBLIC_SECRET!,
  3. providers: [
  4. GoogleProvider({
  5. clientId: process.env.GOOGLE_CLIENT_ID!,
  6. clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
  7. // profile: async (profile) => {
  8. // return { ...profile, role: profile.role ?? Role.USER };
  9. // },
  10. }),
  11. ],
  12. pages: {
  13. signIn: "/",
  14. },
  15. adapter: PrismaAdapter(prisma),
  16. };

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:

  1. export const authOptions: AuthOptions = {
  2. secret: process.env.NEXT_PUBLIC_SECRET!,
  3. providers: [
  4. GoogleProvider({
  5. clientId: process.env.GOOGLE_CLIENT_ID!,
  6. clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
  7. // profile: async (profile) => {
  8. // return { ...profile, role: profile.role ?? Role.USER };
  9. // },
  10. }),
  11. ],
  12. pages: {
  13. signIn: "/",
  14. },
  15. adapter: PrismaAdapter(prisma),
  16. };

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 -->

  1. GoogleProvider({
  2. clientId: process.env.GOOGLE_ID,
  3. clientSecret: process.env.GOOGLE_SECRET,
  4. authorization: {
  5. params: {
  6. prompt: &quot;consent&quot;,
  7. access_type: &quot;offline&quot;,
  8. response_type: &quot;code&quot;
  9. }
  10. },
  11. async profile(profile) {
  12. return {
  13. id: profile.sub,
  14. name: profile.name,
  15. firstname: profile.given_name,
  16. lastname: profile.family_name,
  17. email: profile.email,
  18. image: profile.picture,
  19. }
  20. },
  21. }),

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月14日 02:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定