Skip to content

Commit

Permalink
Merge branch 'main' into feat/better-signup-error-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGuilding committed Sep 5, 2024
2 parents ac5105d + d79cec7 commit cf99087
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 47 deletions.
41 changes: 29 additions & 12 deletions packages/backend/src/types.ts
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>;
68 changes: 34 additions & 34 deletions packages/interface/src/features/signup/components/RegisterEmail.tsx
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">
Expand All @@ -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
Expand All @@ -87,7 +87,7 @@ const RegisterEmail = ({
</FormSection>
</Form>
</div>
);
};
)
}

export default RegisterEmail;
export default RegisterEmail
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const VerifyOtp = ({ emailField }: IVerifyOtpProps): JSX.Element => {
<FormControl
required
valueAsNumber
hint="Check your 'pse.dev' inbox for the OTP"
hint="Check your 'pse.dev' or 'ethereum.org' inbox for the OTP"
label="OTP"
name="otp"
>
Expand Down

0 comments on commit cf99087

Please sign in to comment.