LocChat/client/components/home/_home.jsx
2021-11-23 14:04:12 -07:00

37 lines
848 B
JavaScript

import { useContext, useEffect, useState } from 'react';
import { ApiContext } from '../../utils/api_context';
import { AuthContext } from '../../utils/auth_context';
export const Home = () => {
const [, setAuthToken] = useContext(AuthContext);
const api = useContext(ApiContext);
const [loading, setLoading] = useState(true);
const [user, setUser] = useState(null);
useEffect(async () => {
const res = await api.get('/users/me');
setUser(res.user);
setLoading(false);
}, []);
const logout = async () => {
const res = await api.del('/sessions');
if (res.success) {
setAuthToken(null);
}
};
if (loading) {
return <div>Loading...</div>;
}
return (
<div>
<h1>Welcome {user.name}</h1>
<button type="button" onClick={logout}>
Logout
</button>
</div>
);
};