Some checks failed
Build & Push Docker Image to Gitea Registry / build-and-push (push) Failing after 6s
130 lines
3.8 KiB
TypeScript
130 lines
3.8 KiB
TypeScript
import { API_URL } from "@/config";
|
|
import type { User } from "@michess/types";
|
|
|
|
const readErrorMessage = async (res: Response, fallback: string) => {
|
|
try {
|
|
const body = await res.json();
|
|
if (typeof body?.message === "string") {
|
|
return body.message;
|
|
}
|
|
} catch (err) {
|
|
// Response did not include a JSON error body.
|
|
}
|
|
|
|
return fallback;
|
|
};
|
|
|
|
export const fetchSession = async () => {
|
|
try {
|
|
const res = await fetch(`${API_URL}/v1/auth`, {
|
|
credentials: "include"
|
|
});
|
|
|
|
if (res && res.status === 200) {
|
|
const user: User = await res.json();
|
|
return user;
|
|
}
|
|
} catch (err) {
|
|
// do nothing
|
|
}
|
|
};
|
|
|
|
export const setGuestSession = async (name: string) => {
|
|
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;
|
|
}
|
|
return await readErrorMessage(res, "Could not start guest session.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
return "Could not reach the server. Please try again.";
|
|
}
|
|
};
|
|
|
|
export const register = async (name: string, password: string, email?: string) => {
|
|
try {
|
|
const res = await fetch(`${API_URL}/v1/auth/register`, {
|
|
method: "POST",
|
|
credentials: "include",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({ name, password, email })
|
|
});
|
|
if (res.status === 201) {
|
|
const user: User = await res.json();
|
|
return user;
|
|
}
|
|
return await readErrorMessage(res, "Registration failed. Please try again.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
return "Could not reach the server. Please try again.";
|
|
}
|
|
};
|
|
|
|
export const login = async (name: string, password: string) => {
|
|
try {
|
|
const res = await fetch(`${API_URL}/v1/auth/login`, {
|
|
method: "POST",
|
|
credentials: "include",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({ name, password })
|
|
});
|
|
if (res.status === 200) {
|
|
const user: User = await res.json();
|
|
return user;
|
|
}
|
|
return await readErrorMessage(res, "Login failed. Please try again.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
return "Could not reach the server. Please try again.";
|
|
}
|
|
};
|
|
|
|
export const logout = async () => {
|
|
try {
|
|
const res = await fetch(`${API_URL}/v1/auth/logout`, {
|
|
method: "POST",
|
|
credentials: "include"
|
|
});
|
|
if (res.status === 204) {
|
|
return true;
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
};
|
|
|
|
export const updateUser = async (name?: string, email?: string, password?: string) => {
|
|
try {
|
|
if (!name && !email && !password) return;
|
|
const res = await fetch(`${API_URL}/v1/auth/`, {
|
|
method: "PATCH",
|
|
credentials: "include",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({ name, email, password })
|
|
});
|
|
if (res.status === 200) {
|
|
const user: User = await res.json();
|
|
return user;
|
|
}
|
|
return await readErrorMessage(res, "Could not update settings. Please try again.");
|
|
} catch (err) {
|
|
console.error(err);
|
|
return "Could not reach the server. Please try again.";
|
|
}
|
|
};
|