File size: 10,978 Bytes
f5071ca |
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 |
import React from "react";
import { useState, useContext } from "react";
import { Link, useNavigate } from "react-router-dom";
import { Fade } from "react-reveal";
import { ClipLoader } from "react-spinners";
import {
getAuth,
signInWithEmailAndPassword,
GoogleAuthProvider,
signInWithPopup,
} from "firebase/auth";
import { setDoc, doc, getDoc } from "firebase/firestore";
import { db } from "../Firebase/FirebaseConfig";
import { AuthContext } from "../Context/UserContext";
import GoogleLogo from "../images/GoogleLogo.png";
import WelcomePageBanner from "../images/WelcomePageBanner.jpg";
function SignIn() {
const { User, setUser } = useContext(AuthContext);
const navigate = useNavigate();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [ErrorMessage, setErrorMessage] = useState("");
const [loader, setLoader] = useState(false);
const handleSubmit = (e) => {
e.preventDefault();
setLoader(true);
const auth = getAuth();
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
console.log(user);
if (user != null) {
navigate("/");
}
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
setErrorMessage(error.message);
setLoader(false);
console.log(errorCode);
console.log(errorMessage);
});
};
const loginWithGoogle = (e) => {
e.preventDefault();
const auth = getAuth();
const provider = new GoogleAuthProvider();
signInWithPopup(auth, provider)
.then((result) => {
const user = result.user;
console.log(user);
const EmptyArray = [];
setDoc(
doc(db, "Users", user.uid),
{
email: user.email,
Uid: user.uid,
},
{ merge: true }
).then(() => {
getDoc(doc(db, "MyList", user.uid)).then((result) => {
if (result.exists()) {
// Data exist in MyList section for this user
} else {
// Creating a new MyList, WatchedMovies List, LikedMovies List for the user in the database
setDoc(
doc(db, "MyList", user.uid),
{
movies: EmptyArray,
},
{ merge: true }
);
setDoc(
doc(db, "WatchedMovies", user.uid),
{
movies: EmptyArray,
},
{ merge: true }
);
setDoc(
doc(db, "LikedMovies", user.uid),
{
movies: EmptyArray,
},
{ merge: true }
).then(() => {
navigate("/");
});
}
});
});
if (user != null) {
navigate("/");
}
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
setErrorMessage(error.message);
setLoader(false);
// The email of the user's account used.
const email = error.customData.email;
// The AuthCredential type that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
});
};
return (
<section
className="h-[100vh] bg-gray-50 dark:bg-gray-900"
style={{
background: `linear-gradient(0deg, hsl(0deg 0% 0% / 73%) 0%, hsl(0deg 0% 0% / 73%) 35%),url(${WelcomePageBanner})`,
}}
>
<div className="h-[100vh] flex flex-col items-center justify-center px-6 py-8 mx-auto md:h-screen lg:py-0">
<div className="w-full bg-[#000000a2] rounded-lg shadow sm:my-0 md:mt-0 sm:max-w-lg xl:p-0 border-2 border-stone-800 lg:border-0">
<Fade>
<div>
<div className="p-6 space-y-4 md:space-y-6 sm:p-12">
<h1 className="text-xl font-bold leading-tight tracking-tight text-white md:text-2xl dark:text-white">
Sign in to your account
</h1>
<h1 className="text-white text-2xl p-3 text-center border-2 border-red-700 rounded-sm">
Not Real Netflix
</h1>
<form
onSubmit={handleSubmit}
className="space-y-4 md:space-y-6"
action="#"
>
<div>
<label
for="email"
className="block mb-2 text-sm font-medium text-white dark:text-white"
>
Your email
</label>
<input
type="email"
name="email"
id="email"
className={
ErrorMessage
? "bg-stone-700 text-white sm:text-sm rounded-sm focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 border-2 border-red-700 dark:placeholder-white dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 placeholder:text-white"
: "bg-stone-700 text-white sm:text-sm rounded-sm focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:placeholder-white dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 placeholder:text-white"
}
placeholder="name@email.com"
required=""
onChange={(e) => setEmail(e.target.value)}
></input>
</div>
<div>
<label
for="password"
className="block mb-2 text-sm font-medium text-white dark:text-white"
>
Password
</label>
<input
type="password"
name="password"
id="password"
placeholder="••••••••"
className={
ErrorMessage
? "bg-stone-700 text-white sm:text-sm rounded-sm focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 border-2 border-red-700 dark:bg-gray-700 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 placeholder:text-white"
: "bg-stone-700 text-white sm:text-sm rounded-sm focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 placeholder:text-white"
}
required=""
onChange={(e) => setPassword(e.target.value)}
></input>
</div>
<div>
{ErrorMessage && (
<h1 className="flex text-white font-bold p-4 bg-red-700 rounded text-center">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6 mr-1"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
/>
</svg>
{ErrorMessage}
</h1>
)}
</div>
<div className="flex items-center justify-between">
<div className="flex items-start">
<div className="flex items-center h-5">
<input
id="remember"
aria-describedby="remember"
type="checkbox"
className="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-primary-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-primary-600 dark:ring-offset-gray-800"
required=""
></input>
</div>
<div className="ml-3 text-sm">
<label
for="remember"
className="text-gray-500 dark:text-gray-300"
>
Remember me
</label>
</div>
</div>
</div>
<button
type="submit"
className={`w-full text-white ${
loader
? `bg-stone-700`
: `bg-red-700 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-primary-300`
} transition ease-in-out font-medium rounded-sm text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800`}
>
{loader ? <ClipLoader color="#ff0000" /> : `Sign in`}
</button>
<button
onClick={loginWithGoogle}
className={`flex justify-center items-center w-full text-white ${
loader
? `bg-stone-700`
: `bg-blue-600 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-primary-300`
} transition ease-in-out font-medium rounded-sm text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:focus:ring-primary-800`}
>
{loader ? (
<ClipLoader color="#ff0000" />
) : (
<>
<img className="w-8" src={GoogleLogo}></img>{" "}
<p className="ml-1">Sign in with Google</p>
</>
)}
</button>
<p className="text-sm font-light text-gray-500 dark:text-gray-400">
Don’t have an account yet?{" "}
<Link
className="font-medium text-white hover:underline dark:text-primary-500"
to={"/signup"}
>
Sign up
</Link>
</p>
</form>
</div>
</div>
</Fade>
</div>
</div>
</section>
);
}
export default SignIn;
|