-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
106 lines (96 loc) · 3.24 KB
/
page.tsx
File metadata and controls
106 lines (96 loc) · 3.24 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
"use client";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { auth } from "@/lib/firebase/firebase";
import {
useAuthState,
useSignInWithEmailAndPassword,
} from "react-firebase-hooks/auth";
import toast from "react-hot-toast";
type Credentials = {
email: string;
password: string;
};
export default function Login() {
const router = useRouter();
const [user, authLoading] = useAuthState(auth);
useEffect(() => {
if (user && !authLoading) {
router.push("/dashboard");
}
}, [user, authLoading, router]);
const [credentials, setCredentials] = useState<Credentials>({
email: "",
password: "",
});
const handleCrendentialsChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setCredentials((prev) => ({ ...prev, [name]: value }));
};
const [signInWithEmailAndPassword, _, loading, error] =
useSignInWithEmailAndPassword(auth);
const handleLogin = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!credentials.email || !credentials.password) {
return toast.error("Missing Email or Password");
}
const result = await signInWithEmailAndPassword(
credentials.email,
credentials.password,
);
if (result) {
router.push("/dashboard");
toast.success("Successfully Logged In");
} else {
toast.error("Invalid password or email.");
}
};
return (
<>
<div className="fixed inset-0 z-0 bg-[#080d14]" />
<div className="relative z-10 mx-auto flex max-w-2xl flex-col items-center justify-center pt-40 text-[#eaeaea] sm:pt-56">
<div className="px-6 text-center sm:px-10">
<h1 className="text-2xl font-medium sm:text-3xl">Admin Login</h1>
<h2 className="mt-3">Use the admin email and password to login.</h2>
</div>
<form
action=""
className="mt-6 flex w-full flex-col gap-5 px-6 sm:px-10 "
onSubmit={handleLogin}
>
<div>
<label htmlFor="email" className="text-sm sm:text-base">Email:</label>
<input
id="email"
type="email"
name="email"
className="mt-2 block w-full rounded-md border border-gray-700 bg-[#303742] px-4 py-1.5 sm:py-2 outline-none"
placeholder="Email"
onChange={handleCrendentialsChange}
/>
</div>
<div>
<label htmlFor="password" className="text-sm sm:text-base">Password:</label>
<input
type="password"
id="password"
name="password"
className="mt-2 block w-full rounded-md border border-gray-700 bg-[#303742] px-4 py-1.5 sm:py-2 outline-none"
placeholder="Password"
onChange={handleCrendentialsChange}
/>
</div>
<input
type="submit"
value="Login"
className="cursor-pointer rounded-md bg-blue py-1.5 sm:py-2 text-sm text-black font-medium"
/>
</form>
<Link href="/" className="mt-6 text-sm underline">
Lost? Click to return to safety
</Link>
</div>
</>
);
}