diff --git a/apps/web/src/utils/auth.ts b/apps/web/src/utils/auth.ts index 172086752..54ba095d2 100644 --- a/apps/web/src/utils/auth.ts +++ b/apps/web/src/utils/auth.ts @@ -1,4 +1,3 @@ -import { PrismaAdapter } from "@auth/prisma-adapter"; import { RegistrationTokenPayload } from "@rallly/backend"; import { decryptToken } from "@rallly/backend/session"; import { generateOtp, randomid } from "@rallly/backend/utils/nanoid"; @@ -19,6 +18,7 @@ import EmailProvider from "next-auth/providers/email"; import { Provider } from "next-auth/providers/index"; import { absoluteUrl } from "@/utils/absolute-url"; +import { CustomPrismaAdapter } from "@/utils/auth/custom-prisma-adapter"; import { mergeGuestsIntoUser } from "@/utils/auth/merge-user"; import { isOIDCEnabled, oidcName } from "@/utils/constants"; import { emailClient } from "@/utils/emails"; @@ -133,7 +133,7 @@ if (isOIDCEnabled) { const getAuthOptions = (...args: GetServerSessionParams) => ({ - adapter: PrismaAdapter(prisma), + adapter: CustomPrismaAdapter(prisma), secret: process.env.SECRET_PASSWORD, session: { strategy: "jwt", diff --git a/apps/web/src/utils/auth/custom-prisma-adapter.ts b/apps/web/src/utils/auth/custom-prisma-adapter.ts new file mode 100644 index 000000000..7d9b19af7 --- /dev/null +++ b/apps/web/src/utils/auth/custom-prisma-adapter.ts @@ -0,0 +1,37 @@ +/** + * This is a modified version of the default Prisma adapter that + * ignores fields returned by the OAuth provider that are not + * defined in the database schema when creating an account. + * + * This resolves issues where some OAuth providers return unexpected + * fields in their response which cause the Prisma adapter to throw + * an error. + * + * See: https://github.com/lukevella/rallly/issues/949 + */ +import { PrismaAdapter } from "@auth/prisma-adapter"; +import { PrismaClient } from "@rallly/database"; +import { Adapter, AdapterAccount } from "next-auth/adapters"; + +export function CustomPrismaAdapter(client: PrismaClient): Adapter { + return { + ...PrismaAdapter(client), + linkAccount: (data) => { + return client.account.create({ + data: { + userId: data.userId, + type: data.type, + provider: data.provider, + providerAccountId: data.providerAccountId, + access_token: data.access_token as string, + expires_at: data.expires_at as number, + id_token: data.id_token as string, + token_type: data.token_type as string, + refresh_token: data.refresh_token as string, + scope: data.scope as string, + session_state: data.session_state as string, + }, + }) as unknown as AdapterAccount; + }, + }; +}