🔨 Patch OAuth providers returning unexpected fields (#972)

This commit is contained in:
Luke Vella 2024-01-08 09:04:25 +07:00 committed by GitHub
parent 48421616ea
commit 5898755bbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View file

@ -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",

View file

@ -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;
},
};
}