-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01caab7
commit 236a071
Showing
4 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<main> | ||
<div className="home_page"> | ||
<h4> | ||
{" "} | ||
Welcome <span>{username}</span> | ||
</h4> | ||
<button onClick={Logout}>LOGOUT</button> | ||
</div> | ||
<ToastContainer /> | ||
</main> | ||
); | ||
}; | ||
|
||
export default HomePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<main> | ||
<div className="form_container"> | ||
<h2>Login Account</h2> | ||
<form onSubmit={handleSubmit}> | ||
<div> | ||
<label htmlFor="email">Email</label> | ||
<input | ||
type="email" | ||
name="email" | ||
value={email} | ||
placeholder="Enter your email" | ||
onChange={handleOnChange} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="password">Password</label> | ||
<input | ||
type="password" | ||
name="password" | ||
value={password} | ||
placeholder="Enter your password" | ||
onChange={handleOnChange} | ||
/> | ||
</div> | ||
<button type="submit">Submit</button> | ||
<span> | ||
Already have an account? <Link to={"/signup"}>Signup</Link> | ||
</span> | ||
</form> | ||
<ToastContainer /> | ||
</div> | ||
</main> | ||
); | ||
}; | ||
|
||
export default Loginup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<main> | ||
<div className="form_container"> | ||
<h2>Signup Account</h2> | ||
<form onSubmit={handleSubmit}> | ||
<div> | ||
<label htmlFor="email">Firstname</label> | ||
<input | ||
type="text" | ||
name="firstname" | ||
value={firstname} | ||
placeholder="Enter your firstname" | ||
onChange={handleOnChange} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="email">Email</label> | ||
<input | ||
type="email" | ||
name="email" | ||
value={email} | ||
placeholder="Enter your email" | ||
onChange={handleOnChange} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="email">Username</label> | ||
<input | ||
type="text" | ||
name="username" | ||
value={username} | ||
placeholder="Enter your username" | ||
onChange={handleOnChange} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="password">Password</label> | ||
<input | ||
type="password" | ||
name="password" | ||
value={password} | ||
placeholder="Enter your password" | ||
onChange={handleOnChange} | ||
/> | ||
</div> | ||
<button type="submit">Submit</button> | ||
<span> | ||
Already have an account? <Link to={"/login"}>Login</Link> | ||
</span> | ||
</form> | ||
<ToastContainer /> | ||
</div> | ||
</main> | ||
); | ||
}; | ||
|
||
export default SignUp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// export {default as Login} from "./Login" | ||
// export {default as Home} from "./Home" | ||
// // export {default as SignUp} from "./Signup" |