From 4e17eadb7a681d3932a7b340bdcd0fc91b63412d Mon Sep 17 00:00:00 2001 From: Nathaniel Tampus Date: Mon, 6 Mar 2023 11:43:34 +0800 Subject: [PATCH] wrap auth util functions in try blocks --- client/src/lib/auth.ts | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/client/src/lib/auth.ts b/client/src/lib/auth.ts index 74dbfb5..0c99c9c 100644 --- a/client/src/lib/auth.ts +++ b/client/src/lib/auth.ts @@ -2,27 +2,35 @@ import type { User } from "@chessu/types"; import { API_URL } from "@/config"; export const fetchSession = async () => { - const res = await fetch(`${API_URL}/v1/auth`, { - credentials: "include" - }); + try { + const res = await fetch(`${API_URL}/v1/auth`, { + credentials: "include" + }); - if (res && res.status === 200) { - const user: User = await res.json(); - return user; + if (res && res.status === 200) { + const user: User = await res.json(); + return user; + } + } catch (err) { + console.error(err); } }; export const setGuestSession = async (name: string) => { - const res = await fetch(`${API_URL}/v1/auth/guest`, { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ name }) - }); - if (res.status === 201) { - const user: User = await res.json(); - return user; + try { + const res = await fetch(`${API_URL}/v1/auth/guest`, { + method: "POST", + credentials: "include", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ name }) + }); + if (res.status === 201) { + const user: User = await res.json(); + return user; + } + } catch (err) { + console.error(err); } };