Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

fix reset password

AykutSarac79c28b0ee956
diff --git a/src/pages/forgot-password.tsx b/src/pages/forgot-password.tsx
index cf635766..7ddaee73 100644
--- a/src/pages/forgot-password.tsx
+++ b/src/pages/forgot-password.tsx
@@ -1,26 +1,94 @@
import React from "react";
import Head from "next/head";
import Link from "next/link";
-import { Button, Group, Paper, Stack, TextInput, Text, Anchor } from "@mantine/core";
+import { useRouter } from "next/router";
+import { Button, Group, Paper, Stack, TextInput, Text, Anchor, PasswordInput } from "@mantine/core";
import { toast } from "react-hot-toast";
import Layout from "src/layout/Layout";
import { supabase } from "src/lib/api/supabase";
+function ResetPassword() {
+ const [loading, setLoading] = React.useState(false);
+ const [password, setPassword] = React.useState("");
+ const [password2, setPassword2] = React.useState("");
+
+ const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
+ try {
+ e.preventDefault();
+ setLoading(true);
+
+ if (password !== password2) throw new Error("Passwords do not match");
+
+ const { error } = await supabase.auth.updateUser({ password });
+ if (error) throw new Error(error.message);
+
+ toast.success("Successfully updated password!");
+ setTimeout(() => window.location.assign("/sign-in"), 2000);
+ } catch (error) {
+ if (error instanceof Error) toast.error(error.message);
+ } finally {
+ setLoading(false);
+ }
+ };
+
+ return (
+ <Paper mx="auto" mt={70} maw={400} p="lg" withBorder>
+ <Text size="lg" weight={500} mb="lg">
+ Create New Password
+ </Text>
+
+ <form onSubmit={onSubmit}>
+ <Stack>
+ <PasswordInput
+ value={password}
+ onChange={e => setPassword(e.target.value)}
+ required
+ label="New Password"
+ radius="sm"
+ placeholder="∗∗∗∗∗∗∗∗∗∗∗"
+ />
+ <PasswordInput
+ value={password2}
+ onChange={e => setPassword2(e.target.value)}
+ required
+ label="Validate Password"
+ radius="sm"
+ placeholder="∗∗∗∗∗∗∗∗∗∗∗"
+ />
+ </Stack>
+
+ <Group position="apart" mt="xl">
+ <Button color="dark" type="submit" radius="sm" loading={loading} fullWidth>
+ Reset Password
+ </Button>
+ </Group>
+ </form>
+ </Paper>
+ );
+}
+
const ForgotPassword = () => {
+ const { query } = useRouter();
const [loading, setLoading] = React.useState(false);
const [email, setEmail] = React.useState("");
const [success, setSuccess] = React.useState(false);
+ const isPasswordReset = query?.type === "recovery" && !query?.error;
+
+ const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
+ try {
+ e.preventDefault();
+ setLoading(true);
+
+ const { error } = await supabase.auth.resetPasswordForEmail(email);
+ if (error) throw new Error(error.message);
- const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
- e.preventDefault();
- setLoading(true);
- supabase.auth
- .resetPasswordForEmail(email)
- .then(({ error }) => {
- if (error) return toast.error(error.message);
- setSuccess(true);
- })
- .finally(() => setLoading(false));
+ setSuccess(true);
+ } catch (error) {
+ if (error instanceof Error) toast.error(error.message);
+ console.error(error);
+ } finally {
+ setLoading(false);
+ }
};
return (
@@ -29,40 +97,45 @@ const ForgotPassword = () => {
<title>Reset Password - JSON Crack</title>
<meta name="robots" content="noindex,nofollow" />
</Head>
- <Paper mx="auto" mt={70} maw={400} p="lg" withBorder>
- <Text size="lg" weight={500}>
- Reset Password
- </Text>
- <Paper pt="lg">
- {success ? (
- <Text>We&apos;ve sent an email to you, please check your inbox.</Text>
- ) : (
- <form onSubmit={onSubmit}>
- <Stack>
- <TextInput
- value={email}
- onChange={e => setEmail(e.target.value)}
- required
- label="Email"
- placeholder="[email protected]"
- radius="sm"
- />
- </Stack>
+ {isPasswordReset ? (
+ <ResetPassword />
+ ) : (
+ <Paper mx="auto" mt={70} maw={400} p="lg" withBorder>
+ <Text size="lg" weight={500}>
+ Reset Password
+ </Text>
+ <Paper pt="lg">
+ {success ? (
+ <Text>We&apos;ve sent an email to you, please check your inbox.</Text>
+ ) : (
+ <form onSubmit={onSubmit}>
+ <Stack>
+ <TextInput
+ type="email"
+ value={email}
+ onChange={e => setEmail(e.target.value)}
+ required
+ label="Email"
+ placeholder="[email protected]"
+ radius="sm"
+ />
+ </Stack>
- <Group position="apart" mt="xl">
- <Button color="dark" type="submit" radius="sm" loading={loading} fullWidth>
- Reset Password
- </Button>
- </Group>
- <Stack mt="lg" align="center">
- <Anchor component={Link} prefetch={false} href="/sign-in" color="dark" size="xs">
- Don&apos;t have an account? Sign Up
- </Anchor>
- </Stack>
- </form>
- )}
+ <Group position="apart" mt="xl">
+ <Button color="dark" type="submit" radius="sm" loading={loading} fullWidth>
+ Reset Password
+ </Button>
+ </Group>
+ <Stack mt="lg" align="center">
+ <Anchor component={Link} prefetch={false} href="/sign-in" color="dark" size="xs">
+ Don&apos;t have an account? Sign Up
+ </Anchor>
+ </Stack>
+ </form>
+ )}
+ </Paper>
</Paper>
- </Paper>
+ )}
</Layout>
);
};
diff --git a/src/pages/sign-in.tsx b/src/pages/sign-in.tsx
index 140113fb..fa2fe40a 100644
--- a/src/pages/sign-in.tsx
+++ b/src/pages/sign-in.tsx
@@ -5,9 +5,7 @@ import { useRouter } from "next/router";
import {
TextInput,
PasswordInput,
- Text,
Paper,
- Group,
PaperProps,
Button,
Divider,
@@ -66,7 +64,7 @@ export function AuthenticationForm(props: PaperProps) {
placeholder="[email protected]"
value={userData.email}
onChange={event => setUserData(d => ({ ...d, email: event.target.value }))}
- radius="md"
+ radius="sm"
/>
<PasswordInput
@@ -76,10 +74,10 @@ export function AuthenticationForm(props: PaperProps) {
placeholder="∗∗∗∗∗∗∗∗∗∗∗"
value={userData.password}
onChange={event => setUserData(d => ({ ...d, password: event.target.value }))}
- radius="md"
+ radius="sm"
/>
- <Button color="dark" type="submit" radius="md" loading={loading}>
+ <Button color="dark" type="submit" radius="sm" loading={loading}>
Sign in
</Button>
@@ -101,7 +99,7 @@ export function AuthenticationForm(props: PaperProps) {
<Stack mb="md" mt="md">
<Button
- radius="md"
+ radius="sm"
leftIcon={<AiOutlineGoogle size="20" />}
onClick={() => handleLoginClick("google")}
color="red"
@@ -110,7 +108,7 @@ export function AuthenticationForm(props: PaperProps) {
Sign in with Google
</Button>
<Button
- radius="md"
+ radius="sm"
leftIcon={<AiOutlineGithub size="20" />}
onClick={() => handleLoginClick("github")}
color="dark"
@@ -123,63 +121,6 @@ export function AuthenticationForm(props: PaperProps) {
);
}
-function ResetPassword(props: PaperProps) {
- const { query } = useRouter();
- const [loading, setLoading] = React.useState(false);
- const [password, setPassword] = React.useState("");
- const [password2, setPassword2] = React.useState("");
-
- const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
- e.preventDefault();
- if (query?.token && typeof query?.token === "string") {
- setLoading(true);
- supabase.auth
- .updateUser({
- password,
- })
- .then(({ error }) => {
- if (error) return toast.error(error.message);
- toast.success("Successfully updated password!");
- setTimeout(() => window.location.assign("/sign-in"), 2000);
- })
- .finally(() => setLoading(false));
- }
- };
-
- return (
- <Paper radius="md" {...props}>
- <Text size="lg" weight={500}>
- Reset Password
- </Text>
-
- <form onSubmit={onSubmit}>
- <Stack>
- <PasswordInput
- value={password}
- onChange={e => setPassword(e.target.value)}
- required
- label="Password"
- radius="md"
- />
- <PasswordInput
- value={password2}
- onChange={e => setPassword2(e.target.value)}
- required
- label="Validate Password"
- radius="md"
- />
- </Stack>
-
- <Group position="apart" mt="xl">
- <Button type="submit" radius="xl" loading={loading}>
- Reset Password
- </Button>
- </Group>
- </form>
- </Paper>
- );
-}
-
const SignIn = () => {
const { isReady, push, query } = useRouter();
const session = useSession();
@@ -196,7 +137,7 @@ const SignIn = () => {
<title>Sign In - JSON Crack</title>
</Head>
<Paper mt={50} shadow="xs" mx="auto" maw={400} p="lg" withBorder>
- {isPasswordReset ? <ResetPassword /> : <AuthenticationForm />}
+ <AuthenticationForm />
</Paper>
<Center my="xl">
<Anchor component={Link} prefetch={false} href="/sign-up" color="dark" fw="bold">
diff --git a/src/pages/sign-up.tsx b/src/pages/sign-up.tsx
index 72b61dd3..19b3be68 100644
--- a/src/pages/sign-up.tsx
+++ b/src/pages/sign-up.tsx
@@ -67,7 +67,7 @@ const SignUp = () => {
Please check your inbox for email confirmation.
</Text>
<Anchor component={Link} href="/sign-in" underline={false}>
- <Button color="dark" radius="md" mt="lg" fullWidth>
+ <Button color="dark" radius="sm" mt="lg" fullWidth>
Back to login
</Button>
</Anchor>
@@ -82,7 +82,7 @@ const SignUp = () => {
required
label="Name"
placeholder="John Doe"
- radius="md"
+ radius="sm"
/>
<TextInput
@@ -91,7 +91,7 @@ const SignUp = () => {
required
label="Email"
placeholder="[email protected]"
- radius="md"
+ radius="sm"
/>
<PasswordInput
@@ -100,7 +100,7 @@ const SignUp = () => {
required
label="Password"
placeholder="∗∗∗∗∗∗∗∗∗∗"
- radius="md"
+ radius="sm"
/>
<Button color="dark" type="submit" loading={loading}>
@@ -111,7 +111,7 @@ const SignUp = () => {
<Flex gap="sm">
<Button
- radius="md"
+ radius="sm"
fullWidth
leftIcon={<AiOutlineGoogle size="20" />}
onClick={() => handleLoginClick("google")}
@@ -121,7 +121,7 @@ const SignUp = () => {
Google
</Button>
<Button
- radius="md"
+ radius="sm"
leftIcon={<AiOutlineGithub size="20" />}
onClick={() => handleLoginClick("github")}
color="dark"