|
import { React, useState } from "react"; |
|
import "./signin.css"; |
|
import Logo from "../imgs/logo2.png"; |
|
import BG1 from "../imgs/login-BG.png"; |
|
import BG2 from "../imgs/login-BG2.png"; |
|
import google from "../imgs/google.png"; |
|
import { app } from "../Firebase"; |
|
import { Link, useNavigate } from "react-router-dom"; |
|
import { |
|
getAuth, |
|
createUserWithEmailAndPassword, |
|
GoogleAuthProvider, |
|
signInWithPopup, |
|
updateProfile, |
|
} from "firebase/auth"; |
|
import swal from "sweetalert"; |
|
import { ToastContainer, toast } from "react-toastify"; |
|
import "react-toastify/dist/ReactToastify.css"; |
|
|
|
const auth = getAuth(app); |
|
const provider = new GoogleAuthProvider(); |
|
|
|
function Signup() { |
|
const [email, setEmail] = useState(""); |
|
const [name, setName] = useState(""); |
|
const [password, setPassword] = useState(""); |
|
const [emailError, setEmailError] = useState(""); |
|
const [bgLoaded, setBgLoaded] = useState(false); |
|
const [PasswordError, setPasswordError] = useState(""); |
|
const [NameError, setNameError] = useState(""); |
|
|
|
document.title = "Amazon" |
|
|
|
const notify1 = () => |
|
toast.error("Please fill-up all the credentials properly!", { |
|
position: "top-center", |
|
autoClose: 1200, |
|
hideProgressBar: false, |
|
closeOnClick: true, |
|
pauseOnHover: true, |
|
draggable: true, |
|
progress: undefined, |
|
theme: "colored", |
|
}); |
|
|
|
const navigate = useNavigate(); |
|
const handleEmailChange = (event) => { |
|
setEmail(event.target.value); |
|
}; |
|
|
|
const handleNameChange = (event) => { |
|
setName(event.target.value); |
|
}; |
|
|
|
const handlePasswordChange = (event) => { |
|
setPassword(event.target.value); |
|
}; |
|
|
|
const handleEmailBlur = (event) => { |
|
if ( |
|
event.target.value === "" || |
|
!event.target.value.includes("@") || |
|
!event.target.value.includes(".com") |
|
) { |
|
setEmailError("Please enter a valid email address."); |
|
} else { |
|
setEmailError(""); |
|
} |
|
}; |
|
|
|
const handleNameBlur = (event) => { |
|
if (event.target.value === "") { |
|
setNameError("Please enter your name."); |
|
} else { |
|
setNameError(""); |
|
} |
|
}; |
|
|
|
const handlePasswordBlur = (event) => { |
|
if (event.target.value === "") { |
|
setPasswordError("Please enter your password."); |
|
} else if (event.target.value.length < 4) { |
|
setPasswordError("Password is too small."); |
|
} else { |
|
setPasswordError(""); |
|
} |
|
}; |
|
|
|
const CreateUser = async () => { |
|
try { |
|
const { user } = await createUserWithEmailAndPassword( |
|
auth, |
|
email, |
|
password |
|
); |
|
await updateProfile(user, { |
|
displayName: name, |
|
}); |
|
navigate("/home"); |
|
} catch (error) { |
|
swal({ |
|
title: "Error!", |
|
text: error.message, |
|
icon: "error", |
|
buttons: "Ok", |
|
}); |
|
} |
|
}; |
|
|
|
const GoogleAuth = async () => { |
|
signInWithPopup(auth, provider) |
|
.then((result) => { |
|
navigate("/home"); |
|
}) |
|
.catch((error) => { |
|
swal({ |
|
title: "Error!", |
|
text: error.message, |
|
icon: "error", |
|
buttons: "Ok", |
|
}); |
|
}); |
|
}; |
|
|
|
const handleBgLoad = () => { |
|
setBgLoaded(true); |
|
}; |
|
|
|
return ( |
|
<> |
|
<ToastContainer /> |
|
<div className="signin-page"> |
|
<div className="login-navbar"> |
|
<div className="main-logo"> |
|
<img src={Logo} className="amazon-logo" /> |
|
</div> |
|
<div className="signup"> |
|
<Link to="/"> |
|
<button className="signup-btn">Sign in</button> |
|
</Link> |
|
</div> |
|
</div> |
|
<div className="background"> |
|
<img src={BG1} className="BG1" onLoad={handleBgLoad} /> |
|
<img src={BG2} className="BG2" onLoad={handleBgLoad} /> |
|
</div> |
|
{bgLoaded && ( |
|
<div className="main-form2"> |
|
<div className="login-form"> |
|
<div className="some-text"> |
|
<p className="user">User Registration</p> |
|
<p className="user-desc"> |
|
Hey, Enter your details to create a new account |
|
</p> |
|
</div> |
|
<div className="user-details"> |
|
<input |
|
type="text" |
|
placeholder="Name" |
|
className="name" |
|
value={name} |
|
onBlur={handleNameBlur} |
|
onChange={handleNameChange} |
|
required |
|
/> |
|
{NameError && <div className="error-message">{NameError}</div>} |
|
<input |
|
type="email" |
|
placeholder="Enter Email" |
|
className="email" |
|
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" |
|
value={email} |
|
onChange={handleEmailChange} |
|
onBlur={handleEmailBlur} |
|
required |
|
/> |
|
{emailError && ( |
|
<div className="error-message">{emailError}</div> |
|
)} |
|
<input |
|
type="password" |
|
placeholder="Passcode" |
|
className="password" |
|
value={password} |
|
onChange={handlePasswordChange} |
|
onBlur={handlePasswordBlur} |
|
required |
|
/> |
|
{PasswordError && ( |
|
<div className="error-message">{PasswordError}</div> |
|
)} |
|
<button |
|
onClick={() => { |
|
if (name === "" || email === "" || password === "") { |
|
notify1(); |
|
} else { |
|
CreateUser(); |
|
} |
|
}} |
|
className="signin-btn" |
|
> |
|
Sign up |
|
</button> |
|
<div className="extra-buttons"> |
|
<p className="or">― Or ―</p> |
|
<button onClick={GoogleAuth} className="google"> |
|
<p>Sign up with</p> |
|
<img src={google} className="google-img" /> |
|
</button> |
|
</div> |
|
</div> |
|
</div> |
|
</div> |
|
)} |
|
</div> |
|
</> |
|
); |
|
} |
|
|
|
export default Signup; |
|
|