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";
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);
}
};