Hello guys im a beginner in javascript and i need your help, recently i started a project and in authentification im using json web token and im stuck with the logout code and i need help.
i want the session to stay on untill the user logout from it and thansk in advance.
here is my code :
Hello guys im a beginner in javascript and i need your help, recently i started a project and in authentification im using json web token and im stuck with the logout code and i need help.
i want the session to stay on untill the user logout from it and thansk in advance.
here is my code :
import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { Form, Button, Row, Col } from "react-bootstrap";
import { Link } from "react-router-dom"
import Loading from '../component/loading/loading'
import Error from '../component/errormsg/errormsg'
import MainScreen from '../component/mainscreen/mainscreen'
import './login.css'
function App() {
const navigate = useNavigate()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState(false)
const [loading, setLoading] = useState(false)
async function loginUser(event) {
event.preventDefault()
setLoading(true)
// eslint-disable-next-line no-unused-vars
const response = await fetch('http://localhost:2090/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
password,
}),
})
const data = await response.json()
if(data.user) {
localStorage.setItem('token', data.user)
if(data.status === 'ok') {
navigate('/dashboard')
}
} else {
setError(true)
setLoading(false)
}
}
return (
<MainScreen title="LOGIN">
<div className="loginContainer">
{error && <Error variant="danger">{error}</Error>}
{loading && <Loading />}
<Form onSubmit={loginUser}>
<Form.Group controlId="formBasicEmail">
<Form.Label>Email address</Form.Label>
<Form.Control
type="email"
value={email}
placeholder="Enter email"
onChange={(e) => setEmail(e.target.value)}
/>
</Form.Group>
<Form.Group controlId="formBasicPassword">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
value={password}
placeholder="Password"
onChange={(e) => setPassword(e.target.value)}
/>
</Form.Group>
<Button variant="primary" type="submit">
Submit
</Button>
</Form>
<Row className="py-3">
<Col>
<Link to="/login">Forgot Password or Email ?</Link>
</Col>
</Row>
<Row className="py-3">
<Col>
You dont have an account ? <Link to="/register">Register Here</Link>
</Col>
</Row>
</div>
</MainScreen>
);
}
export default App;