2021-07-31 15:57:43 +00:00
|
|
|
import { resolver, SecurePassword } from "blitz";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
import db, { GlobalRole, MembershipRole } from "../../../db";
|
2021-07-31 15:57:43 +00:00
|
|
|
import { Signup } from "../validations";
|
|
|
|
import { computeEncryptionKey } from "../../../db/_encryption";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-09-25 14:16:31 +00:00
|
|
|
export default resolver.pipe(resolver.zod(Signup), async ({ email, password, fullName }, ctx) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const hashedPassword = await SecurePassword.hash(password.trim());
|
2021-08-05 17:07:15 +00:00
|
|
|
const encryptionKey = computeEncryptionKey(email.toLowerCase().trim()).toString("hex");
|
2021-07-31 14:33:18 +00:00
|
|
|
const user = await db.user.create({
|
2021-08-05 17:07:15 +00:00
|
|
|
data: {
|
2021-09-25 14:16:31 +00:00
|
|
|
fullName: fullName.trim(),
|
2021-08-05 17:07:15 +00:00
|
|
|
email: email.toLowerCase().trim(),
|
|
|
|
hashedPassword,
|
|
|
|
role: GlobalRole.CUSTOMER,
|
|
|
|
memberships: {
|
|
|
|
create: {
|
|
|
|
role: MembershipRole.OWNER,
|
|
|
|
organization: {
|
|
|
|
create: {
|
|
|
|
encryptionKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: { memberships: true },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
await ctx.session.$create({
|
|
|
|
userId: user.id,
|
|
|
|
roles: [user.role, user.memberships[0]!.role],
|
|
|
|
orgId: user.memberships[0]!.organizationId,
|
2021-10-15 22:24:28 +00:00
|
|
|
shouldShowWelcomeMessage: true,
|
2021-08-05 17:07:15 +00:00
|
|
|
});
|
2021-07-31 15:57:43 +00:00
|
|
|
return user;
|
|
|
|
});
|