email.tsx (1949B)
1 import PasswordInput from "#http/islands/password_input.tsx"; 2 import { AppState } from "#http/routes/_middleware.ts"; 3 import { Handlers, PageProps } from "$fresh/src/server/types.ts"; 4 import * as V from "$valita"; 5 6 type Props = { 7 invalid: boolean; 8 }; 9 10 export const handler: Handlers<Props, AppState<"/register/email">> = { 11 GET(_req, ctx) { 12 const { forms, formContext } = ctx.state; 13 if (formContext === null) { 14 return forms.redirect({ form: "/connect", context: { back: "/" } }); 15 } 16 const { back } = formContext; 17 if (forms.session !== null) { 18 return forms.redirect(back); 19 } 20 return ctx.render({ invalid: false }); 21 }, 22 23 async POST(req, ctx) { 24 const { app, forms, formContext } = ctx.state; 25 if (formContext === null) { 26 return forms.redirect({ form: "/connect", context: { back: "/" } }); 27 } 28 const { email, back } = formContext; 29 const { authRegister } = app; 30 const { password, passwordConfirmation } = await forms.inputs( 31 req, 32 V.object({ 33 password: V.string(), 34 passwordConfirmation: V.string(), 35 }), 36 ); 37 const result = await authRegister.execute({ 38 email, 39 password, 40 passwordConfirmation, 41 }); 42 if (result.status === "invalid") { 43 return ctx.render({ invalid: true }); 44 } 45 if (result.status === "conflict") { 46 return forms.redirect({ form: "/connect", context: { back } }); 47 } 48 return forms.redirect({ 49 form: "/verify/email", 50 context: { uuid: result.uuid!, back }, 51 }); 52 }, 53 }; 54 55 export default function RegisterPages({ data }: PageProps<Props>) { 56 return ( 57 <article> 58 <header style="text-align: center;"> 59 <b>Email registration</b> 60 </header> 61 <form method="POST"> 62 <PasswordInput error={data.invalid} confirm={true} /> 63 <div role="group"> 64 <button type="submit">Register</button> 65 </div> 66 </form> 67 </article> 68 ); 69 }