session context & auth util functions

This commit is contained in:
Nathaniel Tampus
2023-03-04 18:38:07 +08:00
parent 6dc9f1a18a
commit 6f7a9fac14
3 changed files with 58 additions and 0 deletions

28
client/src/lib/auth.ts Normal file
View File

@@ -0,0 +1,28 @@
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"
});
if (res && res.status === 200) {
const user: User = await res.json();
return user;
}
};
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;
}
};