({
ctx,
input,
}: InferProcedureOpts<typeof baseProcedure>)
| 27 | export const registerProcedure = once(() => baseProcedure.mutation(register)); |
| 28 | |
| 29 | export async function register({ |
| 30 | ctx, |
| 31 | input, |
| 32 | }: InferProcedureOpts<typeof baseProcedure>) { |
| 33 | let emailVerificationCode: string | null; |
| 34 | |
| 35 | await ctx.dataAbstraction.transaction(async (dtrx) => { |
| 36 | // Get user |
| 37 | |
| 38 | let user = await UserModel.query() |
| 39 | .where('email_hash', Buffer.from(hashUserEmail(input.email))) |
| 40 | .where((builder) => |
| 41 | builder |
| 42 | .where('email_verified', true) |
| 43 | .orWhere('email_verification_expiration_date', '>', new Date()), |
| 44 | ) |
| 45 | .select('email_verified', 'email_verification_code') |
| 46 | .first(); |
| 47 | |
| 48 | // Check if user already exists |
| 49 | |
| 50 | if (user != null) { |
| 51 | // User already exists, check if email is verified |
| 52 | |
| 53 | if (user.email_verified) { |
| 54 | throw new TRPCError({ |
| 55 | message: 'Email already registered.', |
| 56 | code: 'CONFLICT', |
| 57 | }); |
| 58 | } else { |
| 59 | await sendRegistrationEmail({ |
| 60 | email: input.email, |
| 61 | emailVerificationCode: user.email_verification_code, |
| 62 | }); |
| 63 | |
| 64 | throw new TRPCError({ |
| 65 | message: 'Email awaiting verification. New email sent.', |
| 66 | code: 'UNAUTHORIZED', |
| 67 | }); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Create user |
| 72 | |
| 73 | user = await registerUser({ |
| 74 | ...input, |
| 75 | |
| 76 | ip: ctx.req.ip, |
| 77 | userAgent: ctx.req.headers['user-agent'] ?? '', |
| 78 | |
| 79 | passwordValues: derivePasswordValues({ password: input.loginHash }), |
| 80 | |
| 81 | dtrx, |
| 82 | }); |
| 83 | |
| 84 | // Send email |
| 85 | |
| 86 | if (process.env.SEND_EMAILS !== 'false') { |
nothing calls this directly
no test coverage detected