wrap auth util functions in try blocks

This commit is contained in:
Nathaniel Tampus
2023-03-06 11:43:34 +08:00
parent dd1166dc42
commit 4e17eadb7a

View File

@@ -2,27 +2,35 @@ import type { User } from "@chessu/types";
import { API_URL } from "@/config"; import { API_URL } from "@/config";
export const fetchSession = async () => { export const fetchSession = async () => {
const res = await fetch(`${API_URL}/v1/auth`, { try {
credentials: "include" const res = await fetch(`${API_URL}/v1/auth`, {
}); credentials: "include"
});
if (res && res.status === 200) { if (res && res.status === 200) {
const user: User = await res.json(); const user: User = await res.json();
return user; return user;
}
} catch (err) {
console.error(err);
} }
}; };
export const setGuestSession = async (name: string) => { export const setGuestSession = async (name: string) => {
const res = await fetch(`${API_URL}/v1/auth/guest`, { try {
method: "POST", const res = await fetch(`${API_URL}/v1/auth/guest`, {
credentials: "include", method: "POST",
headers: { credentials: "include",
"Content-Type": "application/json" headers: {
}, "Content-Type": "application/json"
body: JSON.stringify({ name }) },
}); body: JSON.stringify({ name })
if (res.status === 201) { });
const user: User = await res.json(); if (res.status === 201) {
return user; const user: User = await res.json();
return user;
}
} catch (err) {
console.error(err);
} }
}; };