connect.tsx (2171B)
1 import EmailInput from "#http/islands/email_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<"/connect">> = { 11 GET(_req, ctx) { 12 const { forms, formContext } = ctx.state; 13 if (formContext === null) { 14 return forms.redirect({ 15 form: "/connect", 16 context: { back: "/" }, 17 }); 18 } 19 const { back } = formContext; 20 if (forms.session !== null) { 21 return forms.redirect(back); 22 } 23 return ctx.render({ invalid: false }); 24 }, 25 26 async POST(req, ctx) { 27 const { app, forms, formContext } = ctx.state; 28 const { authExists, customerInfo } = app; 29 if (formContext === null) { 30 return forms.redirect({ 31 form: "/connect", 32 context: { back: "/" }, 33 }); 34 } 35 36 const { back } = formContext; 37 if (forms.session !== null) { 38 return forms.redirect(back) 39 } 40 41 const { email } = await forms.inputs(req, V.object({ email: V.string() })); 42 43 const existsResult = await authExists.execute({ email }); 44 if (existsResult.status === "invalid") { 45 return ctx.render({ invalid: true }); 46 } 47 if (existsResult.status === "unknown") { 48 return forms.redirect({ 49 form: "/register/email", 50 context: { email, back }, 51 }); 52 } 53 54 const result = await customerInfo.execute({ uuid: existsResult.uuid! }); 55 if (!result.emailVerified) { 56 return forms.redirect({ 57 form: "/verify/email", 58 context: { uuid: result.uuid!, back }, 59 }); 60 } 61 62 return forms.redirect({ 63 form: "/login", 64 context: { uuid: result.uuid!, back }, 65 }); 66 }, 67 }; 68 69 export default function ConnectPage({ data }: PageProps<Props>) { 70 return ( 71 <article> 72 <header style="text-align: center;"> 73 <b>Connection</b> 74 </header> 75 <form method="POST"> 76 <EmailInput error={data.invalid} /> 77 <div role="group"> 78 <button type="submit">Login or register</button> 79 </div> 80 </form> 81 </article> 82 ); 83 }