-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginForm.tsx
More file actions
198 lines (180 loc) · 5.8 KB
/
LoginForm.tsx
File metadata and controls
198 lines (180 loc) · 5.8 KB
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
191
192
193
194
195
196
197
198
"use client";
import { loginUser } from "@/api";
import { VaultItem } from "@/app/page";
import { decryptVault, generateVaultKey, hashPassword } from "@/crypto";
import { zodResolver } from "@hookform/resolvers/zod";
import { Dispatch, SetStateAction, useState } from "react";
import { useForm } from "react-hook-form";
import { useMutation } from "react-query";
import { toast } from "sonner";
import { z } from "zod";
import { Button } from "./ui/button";
import { Form, FormControl, FormField, FormItem, FormLabel } from "./ui/form";
import { Input } from "./ui/input";
import { useRouter } from "next/navigation";
import { Card, CardContent } from "./ui/card";
import { Label } from "./ui/label";
const FormSchema = z.object({
email: z.string().email().min(4, {
message: "Email must be at least 2 characters.",
}),
password: z.string().min(8, {
message: "Password must be at least 8 characters.",
}),
});
function LoginForm({
setVault,
setVaultKey,
setStep,
}: {
setVault: Dispatch<SetStateAction<VaultItem[]>>;
setVaultKey: Dispatch<SetStateAction<string>>;
setStep: Dispatch<SetStateAction<"login" | "register" | "vault">>;
}) {
const {
handleSubmit,
register,
getValues,
setValue,
formState: { errors },
} = useForm<{ email: string; password: string; hashedPassword: string }>();
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const router = useRouter();
const mutation = useMutation(loginUser, {
onSuccess: ({ salt, vault }) => {
const hashedPassword = getValues("hashedPassword");
const email = getValues("email");
const vaultKey = generateVaultKey({
hashedPassword,
email,
salt,
});
window.sessionStorage.setItem("vk", vaultKey);
const decryptedVault = decryptVault({ vault, vaultKey });
setVaultKey(vaultKey);
setVault(decryptedVault);
window.sessionStorage.setItem("vault", JSON.stringify(decryptedVault));
setStep("vault");
router.push("/");
},
onError: (error: any) => {
const errorMessage =
error.response?.data?.message ||
"An error occurred. Please try again later.";
setErrorMessage(errorMessage);
},
});
const handleRegistrationClick = () => {
setStep("register");
};
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
email: "",
password: "",
},
});
function onSubmit(data: z.infer<typeof FormSchema>) {
toast("Logging you in...");
const email = data.email;
const hashedPassword = hashPassword(data.password);
setValue("hashedPassword", hashedPassword);
mutation.mutate({
email,
hashedPassword,
});
}
return (
<div className="bg-black flex min-h-screen items-center justify-center">
<Card className="w-full max-w-md">
<div className="flex justify-center py-6">
<MountainIcon className="h-8 w-8" />
</div>
<CardContent className="space-y-4">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<h1 className="font-extrabold text-center text-7xl py-10 bg-gradient-to-r from-sky-500 via-purple-300 to-purple-500 text-transparent bg-clip-text">
Login
</h1>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<Label htmlFor="email">Email</Label>
<FormControl>
<Input
id="email"
placeholder="Email"
{...field}
aria-invalid={errors.email ? "true" : "false"}
aria-describedby={errors.email ? "email-error" : ""}
/>
</FormControl>
{errors.email && (
<div id="email-error" role="alert">
{errors.email.message}
</div>
)}
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<Label htmlFor="password">Password</Label>
<FormControl>
<Input
id="password"
type="password"
placeholder="Password"
{...field}
aria-invalid={errors.password ? "true" : "false"}
aria-describedby={
errors.password ? "password-error" : ""
}
/>
</FormControl>
{errors.password && (
<div id="password-error" role="alert">
{errors.password.message}
</div>
)}
</FormItem>
)}
/>
<Button type="submit" className="mt-10 w-full">
Login
</Button>
</form>
</Form>
</CardContent>
</Card>
</div>
);
}
interface MountainIconProps {
className?: string;
style?: React.CSSProperties;
}
function MountainIcon(props: MountainIconProps) {
return (
<svg
{...props}
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m8 3 4 8 5-5 5 15H2L8 3z" />
</svg>
);
}
export default LoginForm;