-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/better-signup-error-handling
- Loading branch information
Showing
3 changed files
with
64 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,42 @@ | ||
import { isAddress } from "viem"; | ||
import z from "zod"; | ||
import { isAddress } from 'viem'; | ||
import z from 'zod'; | ||
|
||
export const SendOtpSchema = z.object({ | ||
email: z.string().email().endsWith("@pse.dev").refine(email => !email.includes('+'), { | ||
message: "Email must not contain '+'" | ||
}), | ||
email: z | ||
.string() | ||
.email() | ||
.refine( | ||
(email) => { | ||
return ( | ||
(email.endsWith('@pse.dev') || email.endsWith('@ethereum.org')) && !email.includes('+') | ||
); | ||
}, | ||
{ | ||
message: "Email must be from '@pse.dev' or '@ethereum.org' and must not contain '+'" | ||
} | ||
) | ||
}); | ||
|
||
export type SendOtp = z.infer<typeof SendOtpSchema>; | ||
|
||
export const VerifyOtpSchema = z.object({ | ||
email: z.string() | ||
email: z | ||
.string() | ||
.email() | ||
.endsWith("@pse.dev") | ||
.refine(email => !email.includes('+'), { | ||
message: "Email must not contain '+'" | ||
}), | ||
.refine( | ||
(email) => { | ||
return ( | ||
(email.endsWith('@pse.dev') || email.endsWith('@ethereum.org')) && !email.includes('+') | ||
); | ||
}, | ||
{ | ||
message: "Email must be from '@pse.dev' or '@ethereum.org' and must not contain '+'" | ||
} | ||
), | ||
otp: z.number().int().gte(100000).lte(999999), | ||
address: z.string().refine(isAddress, { | ||
message: "Invalid address", | ||
}), | ||
message: 'Invalid address' | ||
}) | ||
}); | ||
|
||
export type VerifyOtp = z.infer<typeof VerifyOtpSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
import { Dispatch, SetStateAction, useState } from "react"; | ||
import { toast } from "sonner"; | ||
import { Dispatch, SetStateAction, useState } from "react" | ||
import { toast } from "sonner" | ||
|
||
import { config } from "~/config"; | ||
import { Form, FormControl, FormSection } from "~/components/ui/Form"; | ||
import { Input } from "~/components/ui/Input"; | ||
import { EmailFieldSchema, EmailField } from "../types"; | ||
import { Button } from "~/components/ui/Button"; | ||
import { Spinner } from "~/components/ui/Spinner"; | ||
import { config } from "~/config" | ||
import { Form, FormControl, FormSection } from "~/components/ui/Form" | ||
import { Input } from "~/components/ui/Input" | ||
import { EmailFieldSchema, EmailField } from "../types" | ||
import { Button } from "~/components/ui/Button" | ||
import { Spinner } from "~/components/ui/Spinner" | ||
|
||
interface IRegisterEmailProps { | ||
emailField: | ||
| { | ||
email: string; | ||
} | ||
| undefined; | ||
| { | ||
email: string | ||
} | ||
| undefined | ||
setEmail: Dispatch< | ||
SetStateAction< | ||
| { | ||
email: string; | ||
} | ||
email: string | ||
} | ||
| undefined | ||
> | ||
>; | ||
> | ||
} | ||
|
||
const RegisterEmail = ({ | ||
emailField, | ||
setEmail, | ||
}: IRegisterEmailProps): JSX.Element => { | ||
const [registering, setRegistering] = useState(false); | ||
const [registering, setRegistering] = useState(false) | ||
|
||
const registerEmail = async (emailField: EmailField) => { | ||
try { | ||
setRegistering(true); | ||
setRegistering(true) | ||
const response = await fetch(`${config.backendUrl}/send-otp`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(emailField), | ||
}); | ||
const json = await response.json(); | ||
}) | ||
const json = await response.json() | ||
|
||
if (!response.ok) { | ||
console.log(response.status); | ||
console.error(json); | ||
toast.error((json.errors && json.errors[0]) ?? json.message); | ||
console.log(response.status) | ||
console.error(json) | ||
toast.error((json.errors && json.errors[0]) ?? json.message) | ||
} else { | ||
setEmail(emailField); | ||
toast.success(`OTP has been sent to ${emailField.email}`); | ||
setEmail(emailField) | ||
toast.success(`OTP has been sent to ${emailField.email}`) | ||
} | ||
} catch (error: any) { | ||
console.error(error); | ||
toast.error("An unexpected error occured registering your email"); | ||
console.error(error) | ||
toast.error("An unexpected error occured registering your email") | ||
} finally { | ||
setRegistering(false); | ||
setRegistering(false) | ||
} | ||
}; | ||
} | ||
|
||
return ( | ||
<div className="w-72 sm:w-96"> | ||
|
@@ -65,16 +65,16 @@ const RegisterEmail = ({ | |
onSubmit={(email) => registerEmail(email)} | ||
> | ||
<FormSection | ||
description="Please register with your 'pse.dev' email." | ||
description="Please register with your 'pse.dev' or 'ethereum.org' email." | ||
title="Register" | ||
> | ||
<FormControl | ||
required | ||
hint="This is your 'pse.dev' email address" | ||
hint="This is your 'pse.dev' or 'ethereum.org' email address" | ||
label="Email address" | ||
name="email" | ||
> | ||
<Input placeholder="[email protected]" /> | ||
<Input placeholder="[email protected] | [email protected]" /> | ||
</FormControl> | ||
<Button | ||
suppressHydrationWarning | ||
|
@@ -87,7 +87,7 @@ const RegisterEmail = ({ | |
</FormSection> | ||
</Form> | ||
</div> | ||
); | ||
}; | ||
) | ||
} | ||
|
||
export default RegisterEmail; | ||
export default RegisterEmail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters