Add logout logic

This commit is contained in:
Leonardo Murça 2022-06-14 11:39:43 -03:00
parent 34c5dcd863
commit 2a9c6cf6a1
2 changed files with 6 additions and 6 deletions

View file

@ -29,12 +29,11 @@ function AuthProvider(props) {
async function bootstrapUser() {
const user = window.localStorage.getItem('$USER');
if (user) {
console.log(user);
setState({ status: 'success', user: JSON.parse(user), error: null });
}
}
bootstrapUser();
}, [setState]);
}, []);
const login = (email, password) => {
setState({ ...state, status: 'pending' });
@ -51,10 +50,8 @@ function AuthProvider(props) {
};
const logout = () => {
setState({ ...state, status: 'pending' });
return getUser().then(() =>
setState({ status: 'success', user: null, error: null })
);
setState({ status: 'success', user: null, error: null });
window.localStorage.clear();
};
return <AuthContext.Provider value={{ state, login, logout }} {...props} />;

View file

@ -1,12 +1,15 @@
import { useAuthState } from '../../context/auth';
import { useUser } from '../../context/user';
function Home() {
const { isPending, classrooms } = useUser();
const { logout } = useAuthState();
return (
<div>
<h2>Página inicial</h2>
<button onClick={classrooms}>Get classrooms</button>
<button onClick={logout}>Logout</button>
{isPending && <h1>Loading...</h1>}
</div>
);