shellphone.app/app/auth/mutations/signup.ts
2021-09-25 21:15:16 +08:00

37 lines
992 B
TypeScript

import { resolver, SecurePassword } from "blitz";
import db, { GlobalRole, MembershipRole } from "../../../db";
import { Signup } from "../validations";
import { computeEncryptionKey } from "../../../db/_encryption";
export default resolver.pipe(resolver.zod(Signup), async ({ email, password, name }, ctx) => {
const hashedPassword = await SecurePassword.hash(password.trim());
const encryptionKey = computeEncryptionKey(email.toLowerCase().trim()).toString("hex");
const user = await db.user.create({
data: {
name: name.trim(),
email: email.toLowerCase().trim(),
hashedPassword,
role: GlobalRole.CUSTOMER,
memberships: {
create: {
role: MembershipRole.OWNER,
organization: {
create: {
encryptionKey,
},
},
},
},
},
include: { memberships: true },
});
await ctx.session.$create({
userId: user.id,
roles: [user.role, user.memberships[0]!.role],
orgId: user.memberships[0]!.organizationId,
});
return user;
});