Add logout logic
This commit is contained in:
parent
34c5dcd863
commit
2a9c6cf6a1
2 changed files with 6 additions and 6 deletions
|
@ -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} />;
|
||||
|
|
|
@ -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>
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue