I am building an application with ReactJS, which has a login module and is going to have a role system. At this moment, I am with the login part, but I have an error that does not let me continue.
I am storing the username that is being logged into the localStorage with the name 'authenticatedUser', but it is never saved. I have noticed, that if I get the history.push..... the localStorage.setItem()
doesn't work. Does anyone know why this happens or how can I solve it? I leave my code:
Login:
import React, { useState } from 'react';
import { Form, Button } from 'react-bootstrap'
import { useHistory, Link } from "react-router-dom";
import { AuthenticationService } from '../services/AuthenticationService.js'
import '../assets/css/Login.css'
export const Login = () => {
const history = useHistory();
const initialValues = {
userName: '', password: ''
};
const [values, setValues] = useState(initialValues);
function loginClicked(e) {
if (values.userName === 'a@a.a' && values.password === 'a') {
AuthenticationService.registerSuccessfullLogin(values.userName);
history.push({
pathname: '/welcome',
state: { name: values.userName }
});
} else {
e.preventDefault();
alert("Parametros incorrectos");
}
}
return (
<div className="login">
<Form onSubmit={(e) => loginClicked(e)}>
<Form.Group controlId="formBasicEmail">
<Form.Label>Email</Form.Label>
<Form.Control
type="email"
placeholder="Enter email"
value={values.userName}
onChange={({ target: { value } }) => setValues({ ...values, userName: value })}
/>
</Form.Group>
<Form.Group controlId="formBasicPassword">
<Form.Label>Password</Form.Label>
<Form.Control
type="password"
placeholder="Password"
value={values.password}
onChange={({ target: { value } }) => setValues({ ...values, password: value })}
/>
</Form.Group>
<Form.Group controlId="formBasicCheckbox">
<Form.Check type="checkbox" label="Recordar usuario" />
</Form.Group>
<div className="boton">
<Button variant="primary" type="submit">
Ingresar
</Button>
</div>
<div>
<Link to="/registry" className="financionacion-link">
No estas registrado? Registrate!
</Link>
</div>
</Form>
</div>
);
}
AuthenticationService (where I make the setItem):
export const AuthenticationService = {
registerSuccessfullLogin(userName) {
localStorage.setItem('authenticatedUser', userName);
},
logout(userName) {
localStorage.removeItem('authenticatedUser');
},
isUserLoggedIn() {
let user = localStorage.getItem('authenticatedUser')
if (user === null) return false
return true
}
}
When I stop the program immediately after setting the name, I can see that the variable arrives fine and if I look at the storage at that moment, the variable is saved. But when the history.push is done, the storage is erased.
Image 1:
Image 2:
I found the problem, it was my mistake, I had a nav link, which was the logout, and it executed the action.
Before:
<Nav>
<Nav.Link href="/" onClick={AuthenticationService.logout()}>Logout</Nav.Link>
</Nav>
Now:
<Nav>
<Nav.Link href="/" onClick={() => AuthenticationService.logout()}>Logout</Nav.Link>
</Nav>
Thank you all very much, greetings.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.