mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 01:01:49 +10:00
fix: force users to have a Stripe customer on sign in
This commit is contained in:
@ -2,6 +2,8 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
|
|
||||||
import NextAuth from 'next-auth';
|
import NextAuth from 'next-auth';
|
||||||
|
|
||||||
|
import { getStripeCustomerByUser } from '@documenso/ee/server-only/stripe/get-customer';
|
||||||
|
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
|
||||||
import { NEXT_AUTH_OPTIONS } from '@documenso/lib/next-auth/auth-options';
|
import { NEXT_AUTH_OPTIONS } from '@documenso/lib/next-auth/auth-options';
|
||||||
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
@ -18,15 +20,27 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
error: '/signin',
|
error: '/signin',
|
||||||
},
|
},
|
||||||
events: {
|
events: {
|
||||||
signIn: async ({ user }) => {
|
signIn: async ({ user: { id: userId } }) => {
|
||||||
await prisma.userSecurityAuditLog.create({
|
const [user] = await Promise.all([
|
||||||
data: {
|
await prisma.user.findFirstOrThrow({
|
||||||
userId: user.id,
|
where: {
|
||||||
ipAddress,
|
id: userId,
|
||||||
userAgent,
|
},
|
||||||
type: UserSecurityAuditLogType.SIGN_IN,
|
}),
|
||||||
},
|
await prisma.userSecurityAuditLog.create({
|
||||||
});
|
data: {
|
||||||
|
userId: userId,
|
||||||
|
ipAddress,
|
||||||
|
userAgent,
|
||||||
|
type: UserSecurityAuditLogType.SIGN_IN,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Create the Stripe customer and attach it to the user if it doesn't exist.
|
||||||
|
if (user.customerId === null && IS_BILLING_ENABLED()) {
|
||||||
|
await getStripeCustomerByUser(user);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
signOut: async ({ token }) => {
|
signOut: async ({ token }) => {
|
||||||
const userId = typeof token.id === 'string' ? parseInt(token.id) : token.id;
|
const userId = typeof token.id === 'string' ? parseInt(token.id) : token.id;
|
||||||
|
|||||||
Reference in New Issue
Block a user