Updated auth types

This commit is contained in:
Benjamin Sherriff
2023-10-18 16:47:49 -04:00
parent 939f8c2b90
commit c42ecd6591
2 changed files with 20 additions and 13 deletions

View File

@@ -1,13 +1,24 @@
import { getRequest, postRequest } from '.'; import { getRequest, postRequest } from '.';
import { ResponseUser } from './auth.types';
export async function login(email: string, password: string) { export async function login(email: string, password: string): Promise<ResponseUser | undefined> {
return await postRequest('auth/login', { email, password }, { withCredentials: true }); const response = await postRequest('auth/login', { email, password }, { withCredentials: true });
if (response?.status === 200) {
return response.data as ResponseUser;
} else {
return undefined;
}
} }
export async function logout() { export async function logout() {
return await postRequest('auth/logout', {}, { withCredentials: true }); return await postRequest('auth/logout', {}, { withCredentials: true });
} }
export async function me() { export async function me(): Promise<ResponseUser | undefined> {
return await getRequest('auth/me', { withCredentials: true }); const response = await getRequest('auth/me', { withCredentials: true });
if (response?.status === 200) {
return response.data;
} else {
return undefined;
}
} }

View File

@@ -74,8 +74,8 @@ export default function Topbar() {
useEffect(() => { useEffect(() => {
if (Cookies.get('logged_in')) { if (Cookies.get('logged_in')) {
me().then((response) => { me().then((response) => {
if (response?.status == 200) { if (response) {
setUser(response.data.user); setUser(response.user);
} }
}); });
} else { } else {
@@ -176,13 +176,9 @@ function LoginModal({
<form <form
onSubmit={form.onSubmit(async (values) => { onSubmit={form.onSubmit(async (values) => {
const response = await login(values.email, values.password); const response = await login(values.email, values.password);
if (response?.status == 200) { if (response) {
me().then((response) => { setUser(response.user);
if (response?.status == 200) { onClose();
setUser(response.data);
}
onClose();
});
} }
})} })}
> >