2021-07-31 15:57:43 +00:00
|
|
|
import { resolver, SecurePassword } from "blitz";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import db, { Role } from "../../../db";
|
|
|
|
import { Signup } from "../validations";
|
|
|
|
import { computeEncryptionKey } from "../../../db/_encryption";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
export default resolver.pipe(resolver.zod(Signup), async ({ email, password }, ctx) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const hashedPassword = await SecurePassword.hash(password.trim());
|
2021-07-31 14:33:18 +00:00
|
|
|
const user = await db.user.create({
|
|
|
|
data: { email: email.toLowerCase().trim(), hashedPassword, role: Role.USER },
|
|
|
|
select: { id: true, name: true, email: true, role: true },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
|
|
|
const encryptionKey = computeEncryptionKey(user.id).toString("hex");
|
|
|
|
await db.customer.create({ data: { id: user.id, encryptionKey } });
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
await ctx.session.$create({ userId: user.id, role: user.role });
|
|
|
|
return user;
|
|
|
|
});
|