Rework #6

Merged
monoid merged 38 commits from dev into main 2024-04-17 01:45:37 +09:00
2 changed files with 12 additions and 5 deletions
Showing only changes of commit e0cdf51507 - Show all commits

View File

@ -1,4 +1,7 @@
export const BASE_API_URL = 'http://localhost:8080/';
export async function fetcher(url: string) {
const res = await fetch(url);
const u = new URL(url, BASE_API_URL);
const res = await fetch(u);
return res.json();
}

View File

@ -1,4 +1,5 @@
import { atom, useAtomValue, setAtomValue } from "../lib/atom.ts";
import { BASE_API_URL } from "../hook/fetcher.ts";
type LoginLocalStorage = {
username: string;
@ -22,8 +23,9 @@ function getUserSessions() {
return null;
}
async function refresh() {
const res = await fetch("/user/refresh", {
export async function refresh() {
const u = new URL("/user/refresh", BASE_API_URL);
const res = await fetch(u, {
method: "POST",
});
if (res.status !== 200) throw new Error("Maybe Network Error");
@ -47,7 +49,8 @@ async function refresh() {
}
export const doLogout = async () => {
const req = await fetch("/api/user/logout", {
const u = new URL("/user/refresh", BASE_API_URL);
const req = await fetch(u, {
method: "POST",
});
const setVal = setAtomValue(userLoginStateAtom);
@ -76,7 +79,8 @@ export const doLogin = async (userLoginInfo: {
username: string;
password: string;
}): Promise<string | LoginLocalStorage> => {
const res = await fetch("/api/user/login", {
const u = new URL("/user/refresh", BASE_API_URL);
const res = await fetch(u, {
method: "POST",
body: JSON.stringify(userLoginInfo),
headers: { "content-type": "application/json" },