Commit bb17c073 authored by Jithesh K's avatar Jithesh K

initial changes

parent af90ece0
...@@ -29,7 +29,7 @@ function CreateItem() { ...@@ -29,7 +29,7 @@ function CreateItem() {
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
<div> <div>
<label htmlFor="title">Title</label> <label htmlFor="title">Title</label>
<input type="text" placeholder='Enter Title' name='title' className='form-control' onChange={handleChange}/> <input type="text" placeholder='Enter Title' required name='title' className='form-control' onChange={handleChange}/>
</div> </div>
<div> <div>
<label htmlFor="description">Description</label> <label htmlFor="description">Description</label>
......
...@@ -6,13 +6,24 @@ import axios from 'axios' ...@@ -6,13 +6,24 @@ import axios from 'axios'
function Login() { function Login() {
const [username, setUsername] = useState('') const [username, setUsername] = useState('')
const [password, setPassword] = useState('') const [password, setPassword] = useState('')
const [errorMessage, setErrorMessage] = useState('')
const navigate = useNavigate() const navigate = useNavigate()
axios.defaults.withCredentials = true; axios.defaults.withCredentials = true;
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault() e.preventDefault()
if (!username || !password) {
setErrorMessage("Both fields are required.")
return
}
setErrorMessage("")
axios.post('http://localhost:3001/auth/login', {username, password}) axios.post('http://localhost:3001/auth/login', {username, password})
.then(result => { .then(result => {
if(result.data.message == "Invalid Credentials"){
setErrorMessage("Invalid Credentials")
return
}
window.localStorage.setItem("id", result.data.id) window.localStorage.setItem("id", result.data.id)
navigate('/') navigate('/')
console.log(result) console.log(result)
...@@ -24,6 +35,11 @@ function Login() { ...@@ -24,6 +35,11 @@ function Login() {
<div className='p-3 border border-1 w-25'> <div className='p-3 border border-1 w-25'>
<h3>Login</h3> <h3>Login</h3>
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
{errorMessage && (
<div className="alert alert-danger">
{errorMessage}
</div>
)}
<div> <div>
<label htmlFor="username">Username</label> <label htmlFor="username">Username</label>
<input type="text" placeholder='Enter Username' className='form-control' onChange={(e) => setUsername(e.target.value)}/> <input type="text" placeholder='Enter Username' className='form-control' onChange={(e) => setUsername(e.target.value)}/>
......
...@@ -7,7 +7,7 @@ function Nav() { ...@@ -7,7 +7,7 @@ function Nav() {
const handleLogout = () => { const handleLogout = () => {
window.localStorage.clear() window.localStorage.clear()
axios.get('http://localhost:3001/auth/logout') axios.get('http://localhost:3001/auth/logout')
.then(result => navigate("/")) .then(result => navigate("/auth/login"))
.catch(err => console.log(err)) .catch(err => console.log(err))
} }
return ( return (
...@@ -19,11 +19,20 @@ function Nav() { ...@@ -19,11 +19,20 @@ function Nav() {
ToDo-Mern ToDo-Mern
</Link> </Link>
<ul className="navbar-nav ms-2 me-auto mb-2 mb-lg-0"> <ul className="navbar-nav ms-2 me-auto mb-2 mb-lg-0">
{
window.localStorage.length ?
<li className="nav-item"> <li className="nav-item">
<Link className="nav-link text-white" aria-current="page" to="/item/create-item"> <Link className="nav-link text-white" aria-current="page" to="/item/create-item">
Create Item Create Item
</Link> </Link>
</li> </li>
:
<li className="nav-item">
<Link className="nav-link text-white disabled-link" aria-current="page" to="#">
Create Item
</Link>
</li>
}
<li className="nav-item"> <li className="nav-item">
<Link className="nav-link text-white" to="/item/completed-item"> <Link className="nav-link text-white" to="/item/completed-item">
Completed-Items Completed-Items
......
...@@ -6,10 +6,16 @@ import { Link, useNavigate } from 'react-router-dom' ...@@ -6,10 +6,16 @@ import { Link, useNavigate } from 'react-router-dom'
function Registration() { function Registration() {
const [username, setUsername] = useState('') const [username, setUsername] = useState('')
const [password, setPassword] = useState('') const [password, setPassword] = useState('')
const [errorMessage, setErrorMessage] = useState('')
const navigate = useNavigate() const navigate = useNavigate()
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault() e.preventDefault()
if (!username || !password) {
setErrorMessage("Both fields are required.")
return
}
axios.post('http://localhost:3001/auth/register', {username, password}) axios.post('http://localhost:3001/auth/register', {username, password})
.then(result => { .then(result => {
navigate('/auth/login') navigate('/auth/login')
...@@ -22,6 +28,11 @@ function Registration() { ...@@ -22,6 +28,11 @@ function Registration() {
<div className='p-3 border border-1 w-25'> <div className='p-3 border border-1 w-25'>
<h3>Registration</h3> <h3>Registration</h3>
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
{errorMessage && (
<div className="alert alert-danger">
{errorMessage}
</div>
)}
<div> <div>
<label htmlFor="username">Username</label> <label htmlFor="username">Username</label>
<input type="text" placeholder='Enter Username' className='form-control' onChange={(e) => setUsername(e.target.value)}/> <input type="text" placeholder='Enter Username' className='form-control' onChange={(e) => setUsername(e.target.value)}/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment