First off did a lot of research and testing through these Q&As:
- NextAuth – OAuthAccountNotLinked – Imported data from another website – Autolinking
- OAuthAccountNotLinked error in next-auth with Nextjs 13
- Next js – Next Auth – Keep having error=OAuthCreateAccount google provider
- How to handle OAuthAccountNotLinked error in a Next.js app with NextAuth, Prisma, and MongoDB?
- NextAuth – Secondth time signing in with the same google account, OAuthAccountNotLinked error
- Next-auth does not detect linked account after first sign in OAuthAccountNotLinked
- How to handle OAuthAccountNotLinked error in next-auth with Nextjs 13
- next-auth google signin issue OAuthAccountNotLinked caus of MongoDBAdapterOptions.collections
- Next js – Next Auth – Keep having error=OAuthCreateAccount google provider
However, when I run my Next.js application and try to log in I keep getting the error message of:
Try signing in with a different account.
route url in Chrome browser: http://localhost:3000/api/auth/signin?error=OAuthAccountNotLinked
route.js
filepath: app/api/auth/[...nextauth]/route.ts
contents:
import NextAuth from 'next-auth'
import GoogleProvider from 'next-auth/providers/google'
import type { NextAuthOptions } from 'next-auth'
import { PrismaAdapter } from '@next-auth/prisma-adapter'
import prisma from '@/prisma/client'
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
allowDangerousEmailAccountLinking: true,
}),
],
session: {
strategy: 'jwt',
},
}
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
prisma schema
path: prisma/schema.prisma
pulled from here, file contents:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
name String?
email String @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Account {
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@id([provider, providerAccountId])
}
model Session {
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model VerificationToken {
identifier String
token String
expires DateTime
@@id([identifier, token])
}
prisma client
path: prisma/client.ts
contents:
import { PrismaClient } from '@prisma/client'
const prismaClientSingleton = () => {
return new PrismaClient()
}
declare global {
var prismaGlobal: undefined | ReturnType<typeof prismaClientSingleton>
}
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()
export default prisma
if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma
package.json dependencies
"dependencies": {
"@auth/prisma-adapter": "^2.0.0",
"@next-auth/prisma-adapter": "^1.0.7",
"@prisma/client": "^5.13.0",
"@types/node": "20.12.7",
"@types/react": "18.2.79",
"@types/react-dom": "18.2.25",
"autoprefixer": "10.4.19",
"eslint": "8.57.0",
"eslint-config-next": "14.2.2",
"fast-sort": "^3.4.0",
"next": "14.2.2",
"next-auth": "^4.24.7",
"next-cloudinary": "^6.5.1",
"postcss": "8.4.38",
"prisma": "^5.13.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwindcss": "3.4.3",
"typescript": "5.4.5",
"zod": "^3.23.4"
},
"devDependencies": {
"daisyui": "^4.10.2"
}
I’ve tried with both @auth/prisma-adapter
and @next-auth/prisma-adapter
but nothing is logged to the console. Don’t see anything in Chrome app dashboard either.
Why can I not sign in with Google using Next Auth?