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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
"use client";
import { postLogin, postRegister } from "@/actions/login";
import { cn } from "@/lib/utils";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
Card,
CardHeader,
CardDescription,
CardContent,
CardFooter,
CardTitle,
} from "@/components/ui/card";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
import { useActionState } from "react";
const FormSchema = z.object({
username: z.string().min(2, {
message: "Username must be at least 2 characters.",
}),
password: z.string().min(2, {
message: "Password must be at least 2 characters.",
}),
});
export default function AuthScreen() {
return <OOldform />;
}
function OOldform() {
const [state, formAction, isPending] = useActionState<
{ msg: string },
FormData
>(postLogin, { msg: "init" });
return (
<form action={formAction}>
{state.msg}
<label>
Username
<input type="text" placeholder="shadcn" name="username" />
</label>
<label className="flex justify-between">
<span>Password</span>
<a
href="#"
className="ml-auto text-sm underline-offset-4 hover:underline"
>
Forgot your password?
</a>
<input type="password" placeholder="..." name="password" />
</label>
<button type="submit" className="w-full">
Login
</button>
<div className="text-center text-sm">
Don't have an account?{" "}
<a href="#" className="underline underline-offset-4">
Sign up
</a>
</div>
</form>
);
}
function Oldform() {
const [state, formAction, isPending] = useActionState<
{ msg: string },
FormData
>(postLogin, { msg: "init" });
return (
<form action={formAction}>
{state.msg}
<div className="flex flex-col gap-6">
<Card>
<CardHeader className="text-center">
<CardTitle className="text-xl">Welcome back</CardTitle>
<CardDescription>Login to Sorlang</CardDescription>
<p>{state.msg}</p>
</CardHeader>
<CardContent>
<div className="grid gap-6">
<div className="grid gap-6">
<Label>
Username
<Input placeholder="shadcn" name="username" />
</Label>
<Label className="flex justify-between">
<span>Password</span>
<a
href="#"
className="ml-auto text-sm underline-offset-4 hover:underline"
>
Forgot your password?
</a>
<Input type="password" placeholder="..." name="password" />
</Label>
</div>
<Button type="submit" className="w-full">
Login
</Button>
<div className="text-center text-sm">
Don't have an account?{" "}
<a href="#" className="underline underline-offset-4">
Sign up
</a>
</div>
</div>
</CardContent>
</Card>
<div className="text-balance text-center text-xs text-muted-foreground [&_a]:underline [&_a]:underline-offset-4 [&_a]:hover:text-primary ">
By clicking continue, you agree to our{" "}
<a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>.
</div>
</div>
</form>
);
}
function Register({ setRegister }: { setRegister: (b: boolean) => void }) {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
username: "",
password: "",
},
});
async function onSubmit(data: z.infer<typeof FormSchema>) {
const body = JSON.stringify({
name: data.username,
creds: data.password,
});
const opts = {
method: "POST",
headers: { "Content-type": "application/json" },
body,
};
const res = await fetch("/api/db/user/new", opts);
const j = await res.json();
console.log(j);
if ("error" in j) toast(`Error: ${j.error}`);
else {
toast("Register successful");
}
}
return (
<Card>
<CardTitle>Register</CardTitle>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="w-2/3 space-y-6"
>
<CardContent>
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormDescription>
This is your public display name.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input type="password" placeholder="..." {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</CardContent>
<CardFooter>
<Button type="submit">Submit</Button>
</CardFooter>
</form>
</Form>
</Card>
);
}
function LoginForm({
className,
...props
}: React.ComponentPropsWithoutRef<"div">) {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
username: "",
password: "",
},
});
async function onSubmit(data: z.infer<typeof FormSchema>) {
console.log("oh hai");
const body = JSON.stringify({
name: data.username,
creds: data.password,
});
const opts = {
method: "POST",
headers: { "Content-type": "application/json" },
body,
};
const res = await fetch("/api/login", opts);
const j = await res.json();
console.log({ j });
if ("error" in j) toast(`Error! ${j.error}`);
else {
toast("Login successful");
}
}
return (
<div className={cn("flex flex-col gap-6", className)} {...props}>
<Card>
<CardHeader className="text-center">
<CardTitle className="text-xl">Welcome back</CardTitle>
<CardDescription>Login to Sorlang</CardDescription>
</CardHeader>
<CardContent>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<div className="grid gap-6">
<div className="grid gap-6">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel className="flex justify-between">
<span>Password</span>
<a
href="#"
className="ml-auto text-sm underline-offset-4 hover:underline"
>
Forgot your password?
</a>
</FormLabel>
<FormControl>
<Input type="password" placeholder="..." {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<Button type="submit" className="w-full">
Login
</Button>
<div className="text-center text-sm">
Don't have an account?{" "}
<a href="#" className="underline underline-offset-4">
Sign up
</a>
</div>
</div>
</form>
</Form>
</CardContent>
</Card>
<div className="text-balance text-center text-xs text-muted-foreground [&_a]:underline [&_a]:underline-offset-4 [&_a]:hover:text-primary ">
By clicking continue, you agree to our <a href="#">Terms of Service</a>{" "}
and <a href="#">Privacy Policy</a>.
</div>
</div>
);
}
|