Next.js Discord

Discord Forum

Linking Oauth Accounts

Unanswered
German yellowjacket posted this in #help-forum
Open in Discord
German yellowjacketOP
I am implmenting multiple oauth login ways (and eventually credentials). I tried logging in with 2 different oauth providers using the same email and it gave me an error OAuthAccountNotLinked: Another account already exists with the same e-mail address

So I am just wondering how one would go about linking oauth accounts.
// src/auth.config.ts
import Apple from "next-auth/providers/apple"
import GitHub from "next-auth/providers/github"
import Google from "next-auth/providers/google"
import type { NextAuthConfig } from "next-auth"

export default {
   providers: [
      Apple,
      GitHub,
      Google({
         clientId: process.env.GOOGLE_CLIENT_ID,
         clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      })
   ]
} satisfies NextAuthConfig


// src/auth.ts

import NextAuth from "next-auth";
import authConfig from "./auth.config";
import { MongoDBAdapter } from "@auth/mongodb-adapter";
import clientPromise from "@/lib/mongodb";

// Assuming the database name is stored in an environment variable
const databaseName = process.env.DATABASE_NAME;

export const { handlers, signIn, signOut, auth } = NextAuth({
   adapter: MongoDBAdapter(clientPromise, { databaseName }),
   session: { strategy: "jwt" },
   ...authConfig,
});

0 Replies