T3 stack issues with auth

Getting the following error. I can't find any resources online for it
[next-auth][error][adapter_error_getSessionAndUser]
https://next-auth.js.org/errors#adapter_error_getsessionanduser Cannot read properties of undefined (reading 'findUnique') {
message: "Cannot read properties of undefined (reading 'findUnique')",
stack: "TypeError: Cannot read properties of undefined (reading 'findUnique')\n" +
' at getSessionAndUser (/home/hycord/code/typescript/web/banappeals/node_modules/@next-auth/prisma-adapter/dist/index.js:24:52)\n' +
' at _callee2$ (/home/hycord/code/typescript/web/banappeals/node_modules/next-auth/core/errors.js:365:29)\n' +
' at tryCatch (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:44:17)\n' +
' at Generator.<anonymous> (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:125:22)\n' +
' at Generator.next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:69:21)\n' +
' at asyncGeneratorStep (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)\n' +
' at _next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7\n' +
' at new Promise (<anonymous>)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12',
name: 'TypeError'
}
}
[next-auth][error][adapter_error_getSessionAndUser]
https://next-auth.js.org/errors#adapter_error_getsessionanduser Cannot read properties of undefined (reading 'findUnique') {
message: "Cannot read properties of undefined (reading 'findUnique')",
stack: "TypeError: Cannot read properties of undefined (reading 'findUnique')\n" +
' at getSessionAndUser (/home/hycord/code/typescript/web/banappeals/node_modules/@next-auth/prisma-adapter/dist/index.js:24:52)\n' +
' at _callee2$ (/home/hycord/code/typescript/web/banappeals/node_modules/next-auth/core/errors.js:365:29)\n' +
' at tryCatch (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:44:17)\n' +
' at Generator.<anonymous> (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:125:22)\n' +
' at Generator.next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:69:21)\n' +
' at asyncGeneratorStep (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)\n' +
' at _next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7\n' +
' at new Promise (<anonymous>)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12',
name: 'TypeError'
}
}
17 Replies
Hycord | @ When Replying
[next-auth][error][adapter_error_getSessionAndUser]
https://next-auth.js.org/errors#adapter_error_getsessionanduser Cannot read properties of undefined (reading 'findUnique') {
message: "Cannot read properties of undefined (reading 'findUnique')",
stack: "TypeError: Cannot read properties of undefined (reading 'findUnique')\n" +
' at getSessionAndUser (/home/hycord/code/typescript/web/banappeals/node_modules/@next-auth/prisma-adapter/dist/index.js:24:52)\n' +
' at _callee2$ (/home/hycord/code/typescript/web/banappeals/node_modules/next-auth/core/errors.js:365:29)\n' +
' at tryCatch (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:44:17)\n' +
' at Generator.<anonymous> (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:125:22)\n' +
' at Generator.next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:69:21)\n' +
' at asyncGeneratorStep (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)\n' +
' at _next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7\n' +
' at new Promise (<anonymous>)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12',
name: 'TypeError'
[next-auth][error][adapter_error_getSessionAndUser]
https://next-auth.js.org/errors#adapter_error_getsessionanduser Cannot read properties of undefined (reading 'findUnique') {
message: "Cannot read properties of undefined (reading 'findUnique')",
stack: "TypeError: Cannot read properties of undefined (reading 'findUnique')\n" +
' at getSessionAndUser (/home/hycord/code/typescript/web/banappeals/node_modules/@next-auth/prisma-adapter/dist/index.js:24:52)\n' +
' at _callee2$ (/home/hycord/code/typescript/web/banappeals/node_modules/next-auth/core/errors.js:365:29)\n' +
' at tryCatch (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:44:17)\n' +
' at Generator.<anonymous> (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:125:22)\n' +
' at Generator.next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:69:21)\n' +
' at asyncGeneratorStep (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)\n' +
' at _next (/home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7\n' +
' at new Promise (<anonymous>)\n' +
' at /home/hycord/code/typescript/web/banappeals/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12',
name: 'TypeError'
amanuel
amanuel2y ago
Hey! It'll be easier to help if you provide more context. I'm not sure if anyone will be able to help you with just the stack trace Try posting some code and context of what you are trying to achieve!
Hycord | @ When Replying
That's the base create-t3-app template using discord auth plugged in proper secret and id stuff in env with generated secret switched to mongo with prisma did db pull or whatever the command was and now auth doesn't work
Hycord | @ When Replying
/src/pages/api/auth/[...nextauth].ts
import NextAuth, { type NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db/client";

export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
return session;
},
},
// Configure one or more authentication providers
adapter: PrismaAdapter(prisma),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
// ...add more providers here
],
};

export default NextAuth(authOptions);
import NextAuth, { type NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db/client";

export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
return session;
},
},
// Configure one or more authentication providers
adapter: PrismaAdapter(prisma),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
// ...add more providers here
],
};

export default NextAuth(authOptions);
/prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model appeals {
/// Could not determine type: the field only had null or empty values in the sample set.
id String @id @map("_id")
v Int @map("__v")
all String
/// Could not determine type: the field only had null or empty values in the sample set.
appealData Json?
appealMsgChannelId String
appealMsgId String
appealMsgLink String
appealSubmitted Boolean
banTime Int
guildId String
msgChannelId String
msgId String
msgLink String
type String
userId String
}

model beta_appeals {
id String @id @default(auto()) @map("_id") @db.ObjectId

@@map("beta-appeals")
}

model beta_guilds {
id String @id @default(auto()) @map("_id") @db.ObjectId
v Int @map("__v")
all String
appealChannel String
appealDelay Int
dmOnJoin String
enabled String
guildId String
invite String
questions String[]
supportMode Int?
type String

@@map("beta-guilds")
}

model guilds {
id String @id @default(auto()) @map("_id") @db.ObjectId
v Int @map("__v")
all String
appealChannel String
/// Could not determine type: the field only had null or empty values in the sample set.
appealData Json?
appealDelay Int
dmOnJoin String
enabled String
guildId String
invite String
questions String[]
/// Multiple data types found: Int: 1/3 BigInt: 2/3
supportMode Json?
type String
}

model sessions {
id String @id @default(auto()) @map("_id") @db.ObjectId
}

model settings {
id String @id @default(auto()) @map("_id") @db.ObjectId
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model appeals {
/// Could not determine type: the field only had null or empty values in the sample set.
id String @id @map("_id")
v Int @map("__v")
all String
/// Could not determine type: the field only had null or empty values in the sample set.
appealData Json?
appealMsgChannelId String
appealMsgId String
appealMsgLink String
appealSubmitted Boolean
banTime Int
guildId String
msgChannelId String
msgId String
msgLink String
type String
userId String
}

model beta_appeals {
id String @id @default(auto()) @map("_id") @db.ObjectId

@@map("beta-appeals")
}

model beta_guilds {
id String @id @default(auto()) @map("_id") @db.ObjectId
v Int @map("__v")
all String
appealChannel String
appealDelay Int
dmOnJoin String
enabled String
guildId String
invite String
questions String[]
supportMode Int?
type String

@@map("beta-guilds")
}

model guilds {
id String @id @default(auto()) @map("_id") @db.ObjectId
v Int @map("__v")
all String
appealChannel String
/// Could not determine type: the field only had null or empty values in the sample set.
appealData Json?
appealDelay Int
dmOnJoin String
enabled String
guildId String
invite String
questions String[]
/// Multiple data types found: Int: 1/3 BigInt: 2/3
supportMode Json?
type String
}

model sessions {
id String @id @default(auto()) @map("_id") @db.ObjectId
}

model settings {
id String @id @default(auto()) @map("_id") @db.ObjectId
}
^
amanuel
amanuel2y ago
Great! I hope someone can help you now, I'm at the airport so I'm not able to look at investigate this rn It's definitely something with your db though So make sure that part is setup correctly You need to npx prisma db push The schema And make sure you are connected to the DB locally Your database URL should be a localhost and port
barry
barry2y ago
Instantly suspecting mongodb as the problem
Ambushfall
Ambushfall2y ago
It's not MongoDB, it's the user Setup. @Hycord can you provide an example public repository and let us fork it to test? (I've got mine spun up and working absolutely fine using discord auth)
Ambushfall
Ambushfall2y ago
as you're able to see on https://mongodb-t3.vercel.app/
Create T3 App
Generated by create-t3-app
Ambushfall
Ambushfall2y ago
cheers I've noticed also one ID field being incorrect id String @id @map("_id") needs to be mapped to something @db.ObjectID
needmorewood
needmorewood2y ago
If you look up the prsima integration doc on nextauth you fill find what is most likely the culprit I don't see the user table for instance And based on how that error reads it's likely that's the case Perhaps the session table too I'm not sure
Ambushfall
Ambushfall2y ago
my schema pretty much looks like this, added an example table as well
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model Example {
id String @id @default(auto()) @map("_id") @db.ObjectId
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

// Necessary for Next auth
model Account {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.String
access_token String? @db.String
id_token String? @db.String
expires_at Int?
token_type String?
scope String?
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@unique([provider, providerAccountId])
}

model Session {
id String @id @default(auto()) @map("_id") @db.ObjectId
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}

model VerificationToken {
id String @id @default(auto()) @map("_id") @db.ObjectId
token String @unique
expires DateTime
@@unique([id, token])
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model Example {
id String @id @default(auto()) @map("_id") @db.ObjectId
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

// Necessary for Next auth
model Account {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.String
access_token String? @db.String
id_token String? @db.String
expires_at Int?
token_type String?
scope String?
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@unique([provider, providerAccountId])
}

model Session {
id String @id @default(auto()) @map("_id") @db.ObjectId
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}

model VerificationToken {
id String @id @default(auto()) @map("_id") @db.ObjectId
token String @unique
expires DateTime
@@unique([id, token])
}
And it works fine Op's schema is kinda weird
needmorewood
needmorewood2y ago
Yea it's just missing everything from the t3 setup
Ambushfall
Ambushfall2y ago
Not 100% sure since I'm new with auth as to what's required, but I'm pretty sure I got it running within 20 mins or so. It's straightforward honestly. Guessing OP is hosting an app that has access to banning people from the App/Discord and would like to go that route Not 100% sure You're missing the core requirements to use NextAuth
// Necessary for Next auth
model Account {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.String
access_token String? @db.String
id_token String? @db.String
expires_at Int?
token_type String?
scope String?
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@unique([provider, providerAccountId])
}

model Session {
id String @id @default(auto()) @map("_id") @db.ObjectId
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}
// Necessary for Next auth
model Account {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.String
access_token String? @db.String
id_token String? @db.String
expires_at Int?
token_type String?
scope String?
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@unique([provider, providerAccountId])
}

model Session {
id String @id @default(auto()) @map("_id") @db.ObjectId
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}
Afterwards run
npx prisma db push
npx prisma generate
npx prisma db push
npx prisma generate
add these to your schema and run the commands, discord auth will work. Also, removing a model in your schema results to an actual table being removed in the DB. Auth requires Session User and Account, the rest of the tables can be configured by you. I've managed to reproduce the error by foolishly deleting [User, Account, Session] > which is commented out as "// Necessary for Next auth" Running npx prisma db push and npx prisma generate causing me to fail on dev return these, and you'll be fine. If you have any other questions, open another question topic, cheers! ^^
Ambushfall
Ambushfall2y ago
I've left access to the public repo, however I've removed the mongodb integration from deployment https://github.com/Ambushfall/mongodb-t3
GitHub
GitHub - Ambushfall/mongodb-t3
Contribute to Ambushfall/mongodb-t3 development by creating an account on GitHub.
Hycord | @ When Replying
This does work, thankyou
Ambushfall
Ambushfall2y ago
Quite welcome
Want results from more Discord servers?
Add your server