Cannot Implement Roles with MongoDB and Google #3158
-
Hi, I'd like to request some clarification on how to extend more data with Google OAuth and MongoDB for session. This is my [...nextauth].ts: import NextAuth from 'next-auth'
import { MongoDBAdapter } from '@next-auth/mongodb-adapter'
import clientPromise from 'lib/mongodb'
import GoogleProvider from 'next-auth/providers/google'
const maxAge = 30 * 24 * 60 * 60 // 30 days
export default async function auth(req, res) {
return await NextAuth(req, res, {
adapter: MongoDBAdapter({ db: (await clientPromise).db('NexusDatabase') }),
//Configure one or more authentication providers
//We will solely use Google, preferably within a single domain
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
profile(profile: { id; name; email; picture }) {
return {
role: 'user',
id: profile.id,
name: profile.name,
email: profile.email,
image: profile.picture,
}
},
}),
],
pages: {
// signIn: '/auth/signin', // Don't need this page as we redirect them directly to Google Auth
signOut: '/pages/index', // Redirect them to home page on sign out
error: '/auth/error',
newUser: '/auth/new-user',
},
})
} This is the error I am getting: https://next-auth.js.org/errors#oauth_parse_profile_error Cannot read properties of undefined (reading 'toString') {
error: {
message: "Cannot read properties of undefined (reading 'toString')",
stack: "TypeError: Cannot read properties of undefined (reading 'toString')\n" +
' at getProfile (webpack-internal:///./node_modules/next-auth/server/lib/oauth/callback.js:149:39)\n' +
' at runMicrotasks (<anonymous>)\n' +
' at processTicksAndRejections (node:internal/process/task_queues:96:5)\n' +
' at async oAuthCallback (webpack-internal:///./node_modules/next-auth/server/lib/oauth/callback.js:115:12)\n' +
' at async Object.callback (webpack-internal:///./node_modules/next-auth/server/routes/callback.js:50:11)\n' +
' at async NextAuthHandler (webpack-internal:///./node_modules/next-auth/server/index.js:171:18)\n' +
' at async auth (webpack-internal:///./pages/api/auth/[...nextauth].ts:18:10)\n' +
' at async Object.apiResolver (/Users/rajbir/Projects/nexus/node_modules/next/dist/server/api-utils.js:101:9)\n' +
' at async DevServer.handleApiRequest (/Users/rajbir/Projects/nexus/node_modules/next/dist/server/next-server.js:770:9)\n' +
' at async Object.fn (/Users/rajbir/Projects/nexus/node_modules/next/dist/server/next-server.js:661:37)',
name: 'TypeError'
},
},
OAuthProfile: {
iss: 'https://accounts.google.com',
...
...
user: null
},
message: "Cannot read properties of undefined (reading 'toString')" It seems that it cannot read my profile correctly because it's reading undefined but asides from the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
yes, Google doesn't return an next-auth/src/providers/google.ts Line 4 in ba39efb |
Beta Was this translation helpful? Give feedback.
-
It still does not work for me, not sure I understand the concept of extending the fields. My code in [...nextauth].js is:
I also added the fields in types:
Now, the whole thing works, but in database and in session there is no trace of the fields I am trying to add. |
Beta Was this translation helpful? Give feedback.
-
Awesome, thanks for all the help
…On Sun, 14 Nov 2021 at 19:26, Rajbir Johar ***@***.***> wrote:
If I understand the docs correctly, yes. You have to set these extra
params when the user is created for the first time. You obviously shouldn't
be manipulating your db directly but have next-auth generate a User object
with all your fields.
Then, you can have some form asking the user to fill those fields out and
then update the fields in your db in your user collection.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3158 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABLZOGQC76HM353VNWI57DUL75NPANCNFSM5HUKLKHA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
yes, Google doesn't return an
id
, usesub
instead.next-auth/src/providers/google.ts
Line 4 in ba39efb