-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(prisma-adaper): update PrismaAdapter
options to accept optional custom model names
#12401
Open
ogroppo
wants to merge
3
commits into
nextauthjs:main
Choose a base branch
from
ogroppo:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,114 +15,130 @@ | |
* | ||
* @module @auth/prisma-adapter | ||
*/ | ||
import type { PrismaClient, Prisma } from "@prisma/client" | ||
import type { PrismaClient, Prisma } from "@prisma/client"; | ||
import type { | ||
Adapter, | ||
AdapterAccount, | ||
AdapterSession, | ||
AdapterUser, | ||
} from "@auth/core/adapters" | ||
} from "@auth/core/adapters"; | ||
|
||
export function PrismaAdapter( | ||
prisma: PrismaClient | ReturnType<PrismaClient["$extends"]> | ||
prisma: PrismaClient | ReturnType<PrismaClient["$extends"]>, | ||
options?: { | ||
accountModel?: string; | ||
authenticatorModel?: string; | ||
sessionModel?: string; | ||
userModel?: string; | ||
verificationTokenModel?: string; | ||
} | ||
): Adapter { | ||
const p = prisma as PrismaClient | ||
const p = prisma as PrismaClient; | ||
const accountModel = p[options?.accountModel ?? "account"]; | ||
const authenticatorModel = p[options?.authenticatorModel ?? "authenticator"]; | ||
const sessionModel = p[options?.sessionModel ?? "session"]; | ||
const userModel = p[options?.userModel ?? "user"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there's some TS issues here. See: https://github.com/nextauthjs/next-auth/actions/runs/12449775424/job/34755845781?pr=12401 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const verificationTokenModel = | ||
p[options?.verificationTokenModel ?? "verificationToken"]; | ||
return { | ||
// We need to let Prisma generate the ID because our default UUID is incompatible with MongoDB | ||
createUser: ({ id, ...data }) => p.user.create(stripUndefined(data)), | ||
getUser: (id) => p.user.findUnique({ where: { id } }), | ||
getUserByEmail: (email) => p.user.findUnique({ where: { email } }), | ||
createUser: ({ id, ...data }) => userModel.create(stripUndefined(data)), | ||
getUser: (id) => userModel.findUnique({ where: { id } }), | ||
getUserByEmail: (email) => userModel.findUnique({ where: { email } }), | ||
async getUserByAccount(provider_providerAccountId) { | ||
const account = await p.account.findUnique({ | ||
const account = await accountModel.findUnique({ | ||
where: { provider_providerAccountId }, | ||
select: { user: true }, | ||
}) | ||
return (account?.user as AdapterUser) ?? null | ||
}); | ||
return (account?.user as AdapterUser) ?? null; | ||
}, | ||
updateUser: ({ id, ...data }) => | ||
p.user.update({ | ||
userModel.update({ | ||
where: { id }, | ||
...stripUndefined(data), | ||
}) as Promise<AdapterUser>, | ||
deleteUser: (id) => | ||
p.user.delete({ where: { id } }) as Promise<AdapterUser>, | ||
userModel.delete({ where: { id } }) as Promise<AdapterUser>, | ||
linkAccount: (data) => | ||
p.account.create({ data }) as unknown as AdapterAccount, | ||
accountModel.create({ data }) as unknown as AdapterAccount, | ||
unlinkAccount: (provider_providerAccountId) => | ||
p.account.delete({ | ||
accountModel.delete({ | ||
where: { provider_providerAccountId }, | ||
}) as unknown as AdapterAccount, | ||
async getSessionAndUser(sessionToken) { | ||
const userAndSession = await p.session.findUnique({ | ||
const userAndSession = await sessionModel.findUnique({ | ||
where: { sessionToken }, | ||
include: { user: true }, | ||
}) | ||
if (!userAndSession) return null | ||
const { user, ...session } = userAndSession | ||
return { user, session } as { user: AdapterUser; session: AdapterSession } | ||
}); | ||
if (!userAndSession) return null; | ||
const { user, ...session } = userAndSession; | ||
return { user, session } as { | ||
user: AdapterUser; | ||
session: AdapterSession; | ||
}; | ||
}, | ||
createSession: (data) => p.session.create(stripUndefined(data)), | ||
createSession: (data) => sessionModel.create(stripUndefined(data)), | ||
updateSession: (data) => | ||
p.session.update({ | ||
sessionModel.update({ | ||
where: { sessionToken: data.sessionToken }, | ||
...stripUndefined(data), | ||
}), | ||
deleteSession: (sessionToken) => | ||
p.session.delete({ where: { sessionToken } }), | ||
sessionModel.delete({ where: { sessionToken } }), | ||
async createVerificationToken(data) { | ||
const verificationToken = await p.verificationToken.create( | ||
const verificationToken = await verificationTokenModel.create( | ||
stripUndefined(data) | ||
) | ||
); | ||
// @ts-expect-errors // MongoDB needs an ID, but we don't | ||
if (verificationToken.id) delete verificationToken.id | ||
return verificationToken | ||
if (verificationToken.id) delete verificationToken.id; | ||
return verificationToken; | ||
}, | ||
async useVerificationToken(identifier_token) { | ||
try { | ||
const verificationToken = await p.verificationToken.delete({ | ||
const verificationToken = await verificationTokenModel.delete({ | ||
where: { identifier_token }, | ||
}) | ||
}); | ||
// @ts-expect-errors // MongoDB needs an ID, but we don't | ||
if (verificationToken.id) delete verificationToken.id | ||
return verificationToken | ||
if (verificationToken.id) delete verificationToken.id; | ||
return verificationToken; | ||
} catch (error) { | ||
// If token already used/deleted, just return null | ||
// https://www.prisma.io/docs/reference/api-reference/error-reference#p2025 | ||
if ((error as Prisma.PrismaClientKnownRequestError).code === "P2025") | ||
return null | ||
throw error | ||
return null; | ||
throw error; | ||
} | ||
}, | ||
async getAccount(providerAccountId, provider) { | ||
return p.account.findFirst({ | ||
return accountModel.findFirst({ | ||
where: { providerAccountId, provider }, | ||
}) as Promise<AdapterAccount | null> | ||
}) as Promise<AdapterAccount | null>; | ||
}, | ||
async createAuthenticator(data) { | ||
return p.authenticator.create(stripUndefined(data)) | ||
return authenticatorModel.create(stripUndefined(data)); | ||
}, | ||
async getAuthenticator(credentialID) { | ||
return p.authenticator.findUnique({ | ||
return authenticatorModel.findUnique({ | ||
where: { credentialID }, | ||
}) | ||
}); | ||
}, | ||
async listAuthenticatorsByUserId(userId) { | ||
return p.authenticator.findMany({ | ||
return authenticatorModel.findMany({ | ||
where: { userId }, | ||
}) | ||
}); | ||
}, | ||
async updateAuthenticatorCounter(credentialID, counter) { | ||
return p.authenticator.update({ | ||
return authenticatorModel.update({ | ||
where: { credentialID }, | ||
data: { counter }, | ||
}) | ||
}); | ||
}, | ||
} | ||
}; | ||
} | ||
|
||
/** @see https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/null-and-undefined */ | ||
function stripUndefined<T>(obj: T) { | ||
const data = {} as T | ||
for (const key in obj) if (obj[key] !== undefined) data[key] = obj[key] | ||
return { data } | ||
const data = {} as T; | ||
for (const key in obj) if (obj[key] !== undefined) data[key] = obj[key]; | ||
return { data }; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just allow users to override the model name