From c42ecd659170c29a6f851ddb3b66f81d5599c6f6 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Wed, 18 Oct 2023 16:47:49 -0400 Subject: [PATCH] Updated auth types --- ui/src/api/auth.ts | 19 +++++++++++++++---- ui/src/components/Topbar/index.tsx | 14 +++++--------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/ui/src/api/auth.ts b/ui/src/api/auth.ts index 359f603..18745b2 100644 --- a/ui/src/api/auth.ts +++ b/ui/src/api/auth.ts @@ -1,13 +1,24 @@ import { getRequest, postRequest } from '.'; +import { ResponseUser } from './auth.types'; -export async function login(email: string, password: string) { - return await postRequest('auth/login', { email, password }, { withCredentials: true }); +export async function login(email: string, password: string): Promise { + 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() { return await postRequest('auth/logout', {}, { withCredentials: true }); } -export async function me() { - return await getRequest('auth/me', { withCredentials: true }); +export async function me(): Promise { + const response = await getRequest('auth/me', { withCredentials: true }); + if (response?.status === 200) { + return response.data; + } else { + return undefined; + } } diff --git a/ui/src/components/Topbar/index.tsx b/ui/src/components/Topbar/index.tsx index d8d135d..ded0833 100644 --- a/ui/src/components/Topbar/index.tsx +++ b/ui/src/components/Topbar/index.tsx @@ -74,8 +74,8 @@ export default function Topbar() { useEffect(() => { if (Cookies.get('logged_in')) { me().then((response) => { - if (response?.status == 200) { - setUser(response.data.user); + if (response) { + setUser(response.user); } }); } else { @@ -176,13 +176,9 @@ function LoginModal({
{ const response = await login(values.email, values.password); - if (response?.status == 200) { - me().then((response) => { - if (response?.status == 200) { - setUser(response.data); - } - onClose(); - }); + if (response) { + setUser(response.user); + onClose(); } })} >