Skip to content

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

add login with pwd

AykutSarac817226765d4c
diff --git a/package.json b/package.json
index 2b58ef55..56f1da68 100644
--- a/package.json
+++ b/package.json
@@ -16,6 +16,7 @@
"@emotion/react": "^11.11.0",
"@emotion/server": "^11.11.0",
"@mantine/core": "^6.0.11",
+ "@mantine/form": "^6.0.11",
"@mantine/hooks": "^6.0.11",
"@mantine/next": "^6.0.11",
"@mantine/prism": "^6.0.11",
diff --git a/src/pages/reset-password.tsx b/src/pages/reset-password.tsx
new file mode 100644
index 00000000..90713012
--- /dev/null
+++ b/src/pages/reset-password.tsx
@@ -0,0 +1,110 @@
+import React from "react";
+import Head from "next/head";
+import { useRouter } from "next/router";
+import styled from "styled-components";
+import { PaperProps, Center, Button, Group, Paper, Stack, TextInput, Text } from "@mantine/core";
+import { toast } from "react-hot-toast";
+import { altogic } from "src/api/altogic";
+import { Footer } from "src/layout/Footer";
+import { JSONCrackLogo } from "src/layout/JsonCrackLogo";
+import { Navbar } from "src/layout/Navbar";
+import useUser from "src/store/useUser";
+
+const StyledPageWrapper = styled.div`
+ height: calc(100vh - 27px);
+ width: 100%;
+
+ @media only screen and (max-width: 768px) {
+ position: fixed;
+ height: -webkit-fill-available;
+ flex-direction: column;
+ }
+`;
+
+export function AuthenticationForm(props: PaperProps) {
+ const [loading, setLoading] = React.useState(false);
+ const [email, setEmail] = React.useState("");
+ const [success, setSuccess] = React.useState(false);
+
+ const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
+ e.preventDefault();
+ setLoading(true);
+ altogic.auth
+ .sendResetPwdEmail(email)
+ .then(res => {
+ if (res.errors) {
+ toast.error(res.errors.items[0].message);
+ } else setSuccess(true);
+ })
+ .finally(() => setLoading(false));
+ };
+
+ return (
+ <Paper radius="md" p="xl" withBorder w={300} {...props}>
+ <Text size="lg" weight={500}>
+ Reset Password
+ </Text>
+
+ {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="md"
+ />
+ </Stack>
+
+ <Group position="apart" mt="xl">
+ <Button type="submit" radius="xl" loading={loading}>
+ Reset Password
+ </Button>
+ </Group>
+ </form>
+ )}
+ </Paper>
+ );
+}
+
+const StyledHeroSection = styled.section`
+ display: flex;
+ justify-content: center;
+ flex-direction: column;
+ align-items: center;
+`;
+
+const SignIn = () => {
+ const { isReady, replace } = useRouter();
+ const checkSession = useUser(state => state.checkSession);
+ const isAuthenticated = useUser(state => state.isAuthenticated);
+
+ React.useEffect(() => {
+ if (!isReady) checkSession();
+ if (isAuthenticated) replace("/");
+ }, [isReady, isAuthenticated, replace, checkSession]);
+
+ return (
+ <div>
+ <Head>
+ <title>Reset Password | Herowand Editor</title>
+ </Head>
+ <Navbar />
+ <StyledPageWrapper className="repeating-grid">
+ <StyledHeroSection>
+ <JSONCrackLogo />
+ </StyledHeroSection>
+ <Center pt={60}>
+ <AuthenticationForm />
+ </Center>
+ </StyledPageWrapper>
+ <Footer />
+ </div>
+ );
+};
+
+export default SignIn;
diff --git a/src/pages/sign-in.tsx b/src/pages/sign-in.tsx
index d635cf58..9f435110 100644
--- a/src/pages/sign-in.tsx
+++ b/src/pages/sign-in.tsx
@@ -3,25 +3,35 @@ import Head from "next/head";
import Link from "next/link";
import { useRouter } from "next/router";
import styled from "styled-components";
-import { Button, Center, Container, Stack } from "@mantine/core";
+import {
+ TextInput,
+ PasswordInput,
+ Text,
+ Paper,
+ Group,
+ PaperProps,
+ Button,
+ Divider,
+ Checkbox,
+ Anchor,
+ Stack,
+ Center,
+} from "@mantine/core";
+import { useForm } from "@mantine/form";
+import { useToggle, upperFirst } from "@mantine/hooks";
+import { toast } from "react-hot-toast";
import { AiOutlineGithub, AiOutlineGoogle } from "react-icons/ai";
import { altogic } from "src/api/altogic";
import { Footer } from "src/layout/Footer";
+import { JSONCrackLogo } from "src/layout/JsonCrackLogo";
import { Navbar } from "src/layout/Navbar";
import useUser from "src/store/useUser";
-const StyledPageWrapper = styled.div`
- padding: 5%;
-`;
-
-const StyledHeroSection = styled.section`
- display: flex;
- justify-content: center;
- flex-direction: column;
- align-items: center;
-`;
+export function AuthenticationForm(props: PaperProps) {
+ const login = useUser(state => state.login);
+ const [loading, setLoading] = React.useState(false);
+ const [type, toggle] = useToggle<"login" | "register">(["login", "register"]);
-const SignIn = () => {
const { isReady, replace, query } = useRouter();
const checkSession = useUser(state => state.checkSession);
const isAuthenticated = useUser(state => state.isAuthenticated);
@@ -35,65 +45,268 @@ const SignIn = () => {
if (isAuthenticated) replace("/editor");
}, [isReady, isAuthenticated, replace]);
+ const form = useForm({
+ initialValues: {
+ email: "",
+ name: "",
+ password: "",
+ passwordAgain: "",
+ terms: true,
+ },
+
+ validate: {
+ email: val => (/^\S+@\S+$/.test(val) ? null : "Invalid email"),
+ password: val => (val.length <= 6 ? "Password should include at least 6 characters" : null),
+ passwordAgain: (val, { password }) =>
+ type === "register" && val !== password ? "Passwords doesn't match" : null,
+ },
+ });
+
+ const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
+ e.preventDefault();
+ const validate = form.validate();
+ if (validate.hasErrors) return;
+
+ setLoading(true);
+ if (type === "login") {
+ altogic.auth
+ .signInWithEmail(form.values.email, form.values.password)
+ .then(({ user, errors }) => {
+ if (errors?.items.length || !user) return toast.error("Incorrect email or password!");
+ login(user as any);
+ })
+ .finally(() => setLoading(false));
+ } else {
+ altogic.auth
+ .signUpWithEmail(form.values.email, form.values.password, form.values.name)
+ .finally(() => setLoading(false));
+ }
+ };
+
const handleLoginClick = (provider: "github" | "google") => {
altogic.auth.signInWithProvider(provider);
};
return (
- <>
+ <Paper radius="md" p="xl" withBorder {...props}>
+ <Text size="lg" weight={500}>
+ Welcome to JSON Crack, {type} with
+ </Text>
+
+ {isAuthenticating ? (
+ <Stack my={60} w={250} spacing="xl">
+ <Button size="lg" color="orange" onClick={checkSession}>
+ JSON Crack
+ </Button>
+ <Button
+ component="a"
+ href={window.location.href.replace(window.location.host, "editor.herowand.com")}
+ size="lg"
+ color="teal"
+ >
+ Herowand Editor
+ </Button>
+ </Stack>
+ ) : (
+ <>
+ <Group grow mb="md" mt="md">
+ <Button
+ radius="xl"
+ leftIcon={<AiOutlineGoogle />}
+ onClick={() => handleLoginClick("google")}
+ color="red"
+ >
+ Google
+ </Button>
+ <Button
+ radius="xl"
+ leftIcon={<AiOutlineGithub />}
+ onClick={() => handleLoginClick("github")}
+ color="dark"
+ >
+ GitHub
+ </Button>
+ </Group>
+
+ <Divider label="Or continue with email" labelPosition="center" my="lg" />
+
+ <form onSubmit={onSubmit}>
+ <Stack>
+ {type === "register" && (
+ <TextInput
+ required
+ label="Name"
+ placeholder="John Doe"
+ value={form.values.name}
+ onChange={event => form.setFieldValue("name", event.currentTarget.value)}
+ error={form.errors.name && "This field cannot be left blank"}
+ radius="md"
+ />
+ )}
+
+ <TextInput
+ required
+ label="Email"
+ placeholder="[email protected]"
+ value={form.values.email}
+ onChange={event => form.setFieldValue("email", event.currentTarget.value)}
+ error={form.errors.email && "Invalid email"}
+ radius="md"
+ />
+
+ <PasswordInput
+ required
+ label="Password"
+ placeholder="Your password"
+ value={form.values.password}
+ onChange={event => form.setFieldValue("password", event.currentTarget.value)}
+ error={form.errors.password && "Password should include at least 6 characters"}
+ radius="md"
+ />
+ {type === "login" && (
+ <Link href="/reset-password">
+ <Text size="xs">Forgot password?</Text>
+ </Link>
+ )}
+
+ {type === "register" && (
+ <PasswordInput
+ required
+ label="Validate Password"
+ placeholder="Your password"
+ value={form.values.passwordAgain}
+ onChange={event => form.setFieldValue("passwordAgain", event.currentTarget.value)}
+ error={form.errors.passwordAgain && "Passwords doesn't match"}
+ radius="md"
+ />
+ )}
+
+ {type === "register" && (
+ <Checkbox
+ label="I accept terms and conditions"
+ checked={form.values.terms}
+ onChange={event => form.setFieldValue("terms", event.currentTarget.checked)}
+ />
+ )}
+ </Stack>
+
+ <Group position="apart" mt="xl">
+ <Anchor
+ component="button"
+ type="button"
+ color="dimmed"
+ onClick={() => toggle()}
+ size="xs"
+ >
+ {type === "register"
+ ? "Already have an account? Login"
+ : "Don't have an account? Register"}
+ </Anchor>
+ <Button type="submit" radius="xl" loading={loading} disabled={!form.values.terms}>
+ {upperFirst(type)}
+ </Button>
+ </Group>
+ </form>
+ </>
+ )}
+ </Paper>
+ );
+}
+
+const StyledPageWrapper = styled.div`
+ padding: 5%;
+`;
+
+const StyledHeroSection = styled.section`
+ display: flex;
+ justify-content: center;
+ flex-direction: column;
+ align-items: center;
+`;
+
+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?.access_token && typeof query?.access_token === "string") {
+ setLoading(true);
+ altogic.auth
+ .resetPwdWithToken(query?.access_token, password)
+ .then(({ errors }) => {
+ if (errors) {
+ toast.error(errors.items[0].message);
+ } else {
+ toast.success("Successfully updated password!");
+ setTimeout(() => window.location.assign("/sign-in"), 2000);
+ }
+ })
+ .finally(() => setLoading(false));
+ }
+ };
+
+ return (
+ <Paper radius="md" p="xl" withBorder w={300} {...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, replace, query } = useRouter();
+ const checkSession = useUser(state => state.checkSession);
+ const isAuthenticated = useUser(state => state.isAuthenticated);
+ const isPasswordReset = query?.action === "reset-pwd" && !query?.error;
+
+ React.useEffect(() => {
+ if (!isReady) checkSession();
+ if (isAuthenticated) replace("/");
+ }, [isReady, isAuthenticated, replace, checkSession]);
+
+ return (
+ <div>
<Head>
- <title>Sign In | JSON Crack</title>
+ <title>Sign In | Herowand Editor</title>
</Head>
<Navbar />
- <StyledPageWrapper>
+ <StyledPageWrapper className="repeating-grid">
<StyledHeroSection>
- <Link href="/">
- <img src="assets/icon.png" alt="json crack" width="400" />
- </Link>
- <h1>{isAuthenticating ? "Continue With" : "Sign In"}</h1>
+ <JSONCrackLogo />
</StyledHeroSection>
- <Container>
- <Center>
- {isAuthenticating ? (
- <Stack my={60} w={250} spacing="xl">
- <Button size="lg" color="orange" onClick={checkSession}>
- JSON Crack
- </Button>
- <Button
- component="a"
- href={window.location.href.replace(window.location.host, "editor.herowand.com")}
- size="lg"
- color="teal"
- >
- Herowand Editor
- </Button>
- </Stack>
- ) : (
- <Stack my={60} w={250} spacing="xl">
- <Button
- size="lg"
- color="red"
- onClick={() => handleLoginClick("google")}
- leftIcon={<AiOutlineGoogle size={24} />}
- >
- Sign In with Google
- </Button>
- <Button
- size="lg"
- variant="white"
- color="gray"
- onClick={() => handleLoginClick("github")}
- leftIcon={<AiOutlineGithub size={24} />}
- >
- Sign In with GitHub
- </Button>
- </Stack>
- )}
- </Center>
- </Container>
+ <Center pt={60}>{isPasswordReset ? <ResetPassword /> : <AuthenticationForm />}</Center>
</StyledPageWrapper>
<Footer />
- </>
+ </div>
);
};
diff --git a/src/store/useUser.ts b/src/store/useUser.ts
index 43553541..e0a5a3e0 100644
--- a/src/store/useUser.ts
+++ b/src/store/useUser.ts
@@ -7,7 +7,7 @@ import useModal from "./useModal";
const isDevelopment = process.env.NODE_ENV === "development";
interface UserActions {
- login: (response: AltogicAuth) => void;
+ login: (response: User | AltogicAuth) => void;
logout: () => void;
setUser: (key: keyof typeof initialStates, value: any) => void;
checkSession: () => void;
diff --git a/yarn.lock b/yarn.lock
index 581918ae..114134de 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -445,6 +445,14 @@
react-remove-scroll "^2.5.5"
react-textarea-autosize "8.3.4"
+"@mantine/form@^6.0.11":
+ version "6.0.11"
+ resolved "https://registry.yarnpkg.com/@mantine/form/-/form-6.0.11.tgz#241dba070117461ea6b48b834bc546b0e1c5e699"
+ integrity sha512-fUkUF91bHbj0rVyMbLMsUc4/ieHrmsjVSevgfDbVtR9OAA+BL8B4huqsBpiE0NI2clc2zSf/ttbVcHON8PC4qQ==
+ dependencies:
+ fast-deep-equal "^3.1.3"
+ klona "^2.0.5"
+
"@mantine/hooks@^6.0.11":
version "6.0.11"
resolved "https://registry.yarnpkg.com/@mantine/hooks/-/hooks-6.0.11.tgz#1f31cc04aaaf1c24509452a263984647a2b494cf"
@@ -3089,6 +3097,11 @@ kld-polynomial@^0.3.0:
resolved "https://registry.yarnpkg.com/kld-polynomial/-/kld-polynomial-0.3.0.tgz#cb7f3f37c679925d895f4cd737ed713a3b379921"
integrity sha512-PEfxjQ6tsxL9DHBIhM2UZsSes0GI+OIMjbE0kj60jr80Biq/xXl1eGfnyzmfoackAMdKZtw2060L09HdjkPP5w==
+klona@^2.0.5:
+ version "2.0.6"
+ resolved "https://registry.yarnpkg.com/klona/-/klona-2.0.6.tgz#85bffbf819c03b2f53270412420a4555ef882e22"
+ integrity sha512-dhG34DXATL5hSxJbIexCft8FChFXtmskoZYnoPWjXQuebWYCNkVeV3KkGegCK9CP1oswI/vQibS2GY7Em/sJJA==
+
language-subtag-registry@~0.3.2:
version "0.3.22"
resolved "https://registry.yarnpkg.com/language-subtag-registry/-/language-subtag-registry-0.3.22.tgz#2e1500861b2e457eba7e7ae86877cbd08fa1fd1d"