/
sign-in.tsx
190 lines (168 loc) · 5.23 KB
/
sign-in.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React from "react";
import Head from "next/head";
import Link from "next/link";
import { useRouter } from "next/router";
import type { PaperProps } from "@mantine/core";
import {
TextInput,
PasswordInput,
Paper,
Button,
Divider,
Anchor,
Stack,
Center,
Text,
} from "@mantine/core";
import { toast } from "react-hot-toast";
import { AiOutlineGithub, AiOutlineGoogle } from "react-icons/ai";
import Layout from "src/layout/Layout";
import { supabase } from "src/lib/api/supabase";
import { isIframe } from "src/lib/utils/widget";
import useUser from "src/store/useUser";
export function AuthenticationForm(props: PaperProps) {
const { push } = useRouter();
const setSession = useUser(state => state.setSession);
const isAuthenticated = useUser(state => state.isAuthenticated);
const [sessionLoading, setSessionLoading] = React.useState(false);
const [userData, setUserData] = React.useState({
name: "",
email: "",
password: "",
});
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setSessionLoading(true);
const { data, error } = await supabase.auth.signInWithPassword({
email: userData.email,
password: userData.password,
});
if (error) {
setSessionLoading(false);
return toast.error(error.message);
}
await setSession(data.session);
push("/editor");
setSessionLoading(false);
};
const handleLoginClick = async (provider: "github" | "google") => {
setSessionLoading(true);
await supabase.auth.signInWithOAuth({
provider,
options: { redirectTo: `${window.location.origin}/editor` },
});
setSessionLoading(false);
};
if (isAuthenticated) {
return (
<Paper p="lg" maw={400} style={{ textAlign: "center" }}>
<Text fz="sm" c="dark">
You are already signed in. Click the button below to go to the editor.
</Text>
<Link href="/editor">
<Button mt="lg" color="dark" size="lg">
GO TO EDITOR
</Button>
</Link>
</Paper>
);
}
return (
<Paper {...props} style={{ textAlign: "left" }}>
<form onSubmit={onSubmit}>
<Stack>
<TextInput
name="email"
required
label="Email"
placeholder="hello@jsoncrack.com"
value={userData.email}
onChange={event => setUserData(d => ({ ...d, email: event.target.value }))}
radius="sm"
style={{ color: "black" }}
/>
<PasswordInput
name="password"
required
label="Password"
placeholder="∗∗∗∗∗∗∗∗∗∗∗"
value={userData.password}
onChange={event => setUserData(d => ({ ...d, password: event.target.value }))}
radius="sm"
style={{ color: "black" }}
/>
<Button color="dark" type="submit" radius="sm" loading={sessionLoading}>
Sign in
</Button>
<Stack gap="sm" mx="auto" align="center">
<Anchor component={Link} prefetch={false} href="/forgot-password" c="dark" size="xs">
Forgot your password?
</Anchor>
</Stack>
</Stack>
</form>
<Divider my="lg" />
<Stack mb="md" mt="md">
<Button
radius="sm"
leftSection={<AiOutlineGoogle size="20" />}
onClick={() => handleLoginClick("google")}
color="red"
variant="outline"
>
Sign in with Google
</Button>
<Button
radius="sm"
leftSection={<AiOutlineGithub size="20" />}
onClick={() => handleLoginClick("github")}
color="dark"
variant="outline"
>
Sign in with GitHub
</Button>
</Stack>
</Paper>
);
}
const SignIn = () => {
const { isReady, push, query } = useRouter();
const hasSession = useUser(state => !!state.user);
const setSession = useUser(state => state.setSession);
const isPasswordReset = query?.type === "recovery" && !query?.error;
React.useEffect(() => {
if (isIframe()) {
push("/");
return;
}
if (!isReady) return;
if (query?.access_token && query?.refresh_token) {
(async () => {
const refresh_token = query.refresh_token as string;
const access_token = query.access_token as string;
const { data, error } = await supabase.auth.setSession({ refresh_token, access_token });
if (error) return toast.error(error.message);
if (data.session) setSession(data.session);
})();
}
if (hasSession && !isPasswordReset) push("/editor");
}, [isReady, hasSession, push, isPasswordReset, query, setSession]);
if (!isReady) return null;
return (
<Layout>
<Head>
<title>Sign In - JSON Crack</title>
<link rel="canonical" href="https://app.jsoncrack.com/sign-in" />
</Head>
<Paper mt={100} mx="auto" maw={400} p="lg" withBorder>
<AuthenticationForm />
</Paper>
<Center my="xl">
<Anchor component={Link} prefetch={false} href="/sign-up" c="gray.5" fw="bold">
Don't have an account?
</Anchor>
</Center>
</Layout>
);
};
export default SignIn;