diff --git a/client/src/pages/Home.js b/client/src/pages/Home.js new file mode 100644 index 0000000..6691f2e --- /dev/null +++ b/client/src/pages/Home.js @@ -0,0 +1,49 @@ +import React, { useEffect, useState } from "react"; +import { useNavigate } from "react-router-dom"; +import { useCookies } from "react-cookie"; +import axios from "axios"; +import { ToastContainer, toast } from "react-toastify"; + +const HomePage = () => { + const navigate = useNavigate(); + const [cookies, removeCookie] = useCookies([]); + const [username, setUsername] = useState(""); + useEffect(() => { + const verifyCookie = async () => { + if (!cookies.token) { + navigate("/login"); + } + const { data } = await axios.post( + "http://localhost:6000", + {}, + { withCredentials: true } + ); + const { status, user } = data; + setUsername(user); + return status + ? toast(`Hello ${user}`, { + position: "top-right", + }) + : (removeCookie("token"), navigate("/login")); + }; + verifyCookie(); + }, [cookies, navigate, removeCookie]); + const Logout = () => { + removeCookie("token"); + navigate("/signup"); + }; + return ( +
+
+

+ {" "} + Welcome {username} +

+ +
+ +
+ ); +}; + +export default HomePage; diff --git a/client/src/pages/Login.js b/client/src/pages/Login.js new file mode 100644 index 0000000..3a99811 --- /dev/null +++ b/client/src/pages/Login.js @@ -0,0 +1,95 @@ +import React, { useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; +import axios from "axios"; +import { ToastContainer, toast } from "react-toastify"; + +const Loginup = () => { + const navigate = useNavigate(); + const [inputValue, setInputValue] = useState({ + email: "", + password: "", + }); + const { email, password } = inputValue; + const handleOnChange = (e) => { + const { name, value } = e.target; + setInputValue({ + ...inputValue, + [name]: value, + }); + }; + + const handleError = (err) => + toast.error(err, { + position: "bottom-left", + }); + const handleSuccess = (msg) => + toast.success(msg, { + position: "bottom-left", + }); + + const handleSubmit = async (e) => { + e.preventDefault(); + try { + const { data } = await axios.post( + "http://localhost:4000/login", + { + ...inputValue, + }, + { withCredentials: true } + ); + console.log(data); + const { success, message } = data; + if (success) { + handleSuccess(message); + setTimeout(() => { + navigate("/"); + }, 1000); + } else { + handleError(message); + } + } catch (error) { + console.log(error); + } + setInputValue({ + ...inputValue, + email: "", + password: "", + }); + }; + return ( +
+
+

Login Account

+
+
+ + +
+
+ + +
+ + + Already have an account? Signup + +
+ +
+
+ ); +}; + +export default Loginup; diff --git a/client/src/pages/Signup.js b/client/src/pages/Signup.js new file mode 100644 index 0000000..482b3b8 --- /dev/null +++ b/client/src/pages/Signup.js @@ -0,0 +1,119 @@ +import React, { useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; +import axios from "axios"; +import { ToastContainer, toast } from "react-toastify"; + +const SignUp = () => { + const navigate = useNavigate(); + const [inputValue, setInputValue] = useState({ + firstname: "", + email: "", + password: "", + username: "", + }); + + const { firstname, email, password, username } = inputValue; + const handleOnChange = (e) => { + const { name, value } = e.target; + setInputValue({ + ...inputValue, + [name]: value, + }); + }; + + const handleError = (err) => + toast.error(err, { + position: "bottom-left", + }); + const handleSuccess = (msg) => + toast.success(msg, { + position: "bottom-right", + }); + + const handleSubmit = async (e) => { + e.preventDefault(); + try { + const { data } = await axios.post( + "http://localhost:4040/signup", + { + ...inputValue, + }, + { withCredentials: true } + ); + const { success, message } = data; + if (success) { + handleSuccess(message); + setTimeout(() => { + navigate("/"); + }, 1000); + } else { + handleError(message); + } + } catch (error) { + console.log(error); + } + setInputValue({ + ...inputValue, + email: "", + password: "", + username: "", + }); + }; + + return ( +
+
+

Signup Account

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + + Already have an account? Login + +
+ +
+
+ ); +}; + +export default SignUp; diff --git a/client/src/pages/index.js b/client/src/pages/index.js new file mode 100644 index 0000000..4cf905b --- /dev/null +++ b/client/src/pages/index.js @@ -0,0 +1,3 @@ +// export {default as Login} from "./Login" +// export {default as Home} from "./Home" +// // export {default as SignUp} from "./Signup"