Skip to content

Commit

Permalink
Merge pull request #1 from IITII/optional-email-confirmation
Browse files Browse the repository at this point in the history
optional email confirmation feature
  • Loading branch information
IITII authored Jan 29, 2024
2 parents 7f8acd7 + 0dc2289 commit b5677d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
11 changes: 5 additions & 6 deletions client/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const LoginPage = () => {
const { isAuthenticated } = useStoreState((s) => s.auth);
const login = useStoreActions((s) => s.auth.login);
const [error, setError] = useState("");
const [verifying, setVerifying] = useState(false);
const [message, setMessage] = useState("");
const [loading, setLoading] = useState({ login: false, signup: false });
const [formState, { email, password, label }] = useFormState<{
email: string;
Expand Down Expand Up @@ -79,8 +79,8 @@ const LoginPage = () => {
if (type === "signup" && !DISALLOW_REGISTRATION) {
setLoading((s) => ({ ...s, signup: true }));
try {
await axios.post(APIv2.AuthSignup, { email, password });
setVerifying(true);
const res = await axios.post(APIv2.AuthSignup, { email, password });
setMessage(res.data.message);
} catch (error) {
setError(error.response.data.error);
}
Expand All @@ -97,10 +97,9 @@ const LoginPage = () => {
return (
<AppWrapper>
<ColCenterV maxWidth="100%" px={3} flex="0 0 auto" mt={4}>
{verifying ? (
{message ? (
<H2 textAlign="center" light>
A verification email has been sent to{" "}
<Email>{formState.values.email}</Email>.
{message}
</H2>
) : (
<LoginForm id="login-form" onSubmit={onSubmit("login")}>
Expand Down
9 changes: 8 additions & 1 deletion server/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,16 @@ export const signup: Handler = async (req, res) => {
req.user
);

if (!process.env.MAIL_HOST) {
return res
.status(201)
.send({ message: "Your account has been created successfully." });
}
await mail.verification(user);

return res.status(201).send({ message: "Verification email has been sent." });
return res
.status(201)
.send({ message: `Verification email has been sent to ${user.email}.` });
};

export const token: Handler = async (req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion server/queries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const add = async (params: Add, user?: User) => {
email: params.email,
password: params.password,
verification_token: uuid(),
verification_expires: addMinutes(new Date(), 60).toISOString()
verification_expires: addMinutes(new Date(), 60).toISOString(),
verified: process.env.MAIL_HOST ? false : true
};

if (user) {
Expand Down

0 comments on commit b5677d4

Please sign in to comment.