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() {
|
async function bootstrapUser() {
|
||||||
const user = window.localStorage.getItem('$USER');
|
const user = window.localStorage.getItem('$USER');
|
||||||
if (user) {
|
if (user) {
|
||||||
console.log(user);
|
|
||||||
setState({ status: 'success', user: JSON.parse(user), error: null });
|
setState({ status: 'success', user: JSON.parse(user), error: null });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bootstrapUser();
|
bootstrapUser();
|
||||||
}, [setState]);
|
}, []);
|
||||||
|
|
||||||
const login = (email, password) => {
|
const login = (email, password) => {
|
||||||
setState({ ...state, status: 'pending' });
|
setState({ ...state, status: 'pending' });
|
||||||
|
@ -51,10 +50,8 @@ function AuthProvider(props) {
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
setState({ ...state, status: 'pending' });
|
setState({ status: 'success', user: null, error: null });
|
||||||
return getUser().then(() =>
|
window.localStorage.clear();
|
||||||
setState({ status: 'success', user: null, error: null })
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return <AuthContext.Provider value={{ state, login, logout }} {...props} />;
|
return <AuthContext.Provider value={{ state, login, logout }} {...props} />;
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
|
import { useAuthState } from '../../context/auth';
|
||||||
import { useUser } from '../../context/user';
|
import { useUser } from '../../context/user';
|
||||||
|
|
||||||
function Home() {
|
function Home() {
|
||||||
const { isPending, classrooms } = useUser();
|
const { isPending, classrooms } = useUser();
|
||||||
|
const { logout } = useAuthState();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h2>Página inicial</h2>
|
<h2>Página inicial</h2>
|
||||||
<button onClick={classrooms}>Get classrooms</button>
|
<button onClick={classrooms}>Get classrooms</button>
|
||||||
|
<button onClick={logout}>Logout</button>
|
||||||
{isPending && <h1>Loading...</h1>}
|
{isPending && <h1>Loading...</h1>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue