Files
sign/packages/lib/server-only/user/create-user.ts

53 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10:00
import { hash } from 'bcrypt';
import { getStripeCustomerByUser } from '@documenso/ee/server-only/stripe/get-customer';
2023-06-09 18:21:18 +10:00
import { prisma } from '@documenso/prisma';
import { IdentityProvider } from '@documenso/prisma/client';
import { SALT_ROUNDS } from '../../constants/auth';
import { getFlag } from '../../universal/get-feature-flag';
2023-06-09 18:21:18 +10:00
export interface CreateUserOptions {
name: string;
email: string;
password: string;
2023-09-01 19:46:44 +10:00
signature?: string | null;
2023-06-09 18:21:18 +10:00
}
2023-09-01 19:46:44 +10:00
export const createUser = async ({ name, email, password, signature }: CreateUserOptions) => {
const isBillingEnabled = await getFlag('app_billing');
2023-06-09 18:21:18 +10:00
const hashedPassword = await hash(password, SALT_ROUNDS);
const userExists = await prisma.user.findFirst({
where: {
email: email.toLowerCase(),
},
});
if (userExists) {
throw new Error('User already exists');
}
let user = await prisma.user.create({
2023-06-09 18:21:18 +10:00
data: {
name,
email: email.toLowerCase(),
password: hashedPassword,
2023-09-01 19:46:44 +10:00
signature,
2023-06-09 18:21:18 +10:00
identityProvider: IdentityProvider.DOCUMENSO,
},
});
if (isBillingEnabled) {
try {
const stripeSession = await getStripeCustomerByUser(user);
user = stripeSession.user;
} catch (e) {
console.error(e);
}
}
return user;
2023-06-09 18:21:18 +10:00
};