Add login timeout

This commit is contained in:
Zoe Roux 2023-07-19 10:12:52 +09:00
parent 2c49848dd7
commit 45baa804e6
3 changed files with 8 additions and 2 deletions

View File

@ -46,7 +46,7 @@ export const useAccounts = () => {
setVerified({status: "loading"}); setVerified({status: "loading"});
const selAcc = accounts![selected!]; const selAcc = accounts![selected!];
setApiUrl(selAcc.apiUrl); setApiUrl(selAcc.apiUrl);
const verif = await loginFunc("refresh", selAcc.refresh_token); const verif = await loginFunc("refresh", selAcc.refresh_token, undefined, 5_000);
setVerified(verif.ok ? { status: "ok" } : { status: "error", error: verif.error }); setVerified(verif.ok ? { status: "ok" } : { status: "error", error: verif.error });
} }

View File

@ -60,8 +60,11 @@ export const loginFunc = async (
action: "register" | "login" | "refresh", action: "register" | "login" | "refresh",
body: { username: string; password: string; email?: string } | string, body: { username: string; password: string; email?: string } | string,
apiUrl?: string, apiUrl?: string,
timeout?: number,
): Promise<Result<Token, string>> => { ): Promise<Result<Token, string>> => {
try { try {
const controller = timeout !== undefined ? new AbortController() : undefined;
if (controller) setTimeout(() => controller.abort(), timeout);
const token = await queryFn( const token = await queryFn(
{ {
path: ["auth", action, typeof body === "string" && `?token=${body}`], path: ["auth", action, typeof body === "string" && `?token=${body}`],
@ -69,6 +72,7 @@ export const loginFunc = async (
body: typeof body === "object" ? body : undefined, body: typeof body === "object" ? body : undefined,
authenticated: false, authenticated: false,
apiUrl, apiUrl,
abortSignal: controller?.signal,
}, },
TokenP, TokenP,
); );

View File

@ -54,7 +54,8 @@ export const queryFn = async <Data,>(
body?: object; body?: object;
method: "GET" | "POST" | "DELETE"; method: "GET" | "POST" | "DELETE";
authenticated?: boolean; authenticated?: boolean;
apiUrl?: string apiUrl?: string;
abortSignal?: AbortSignal;
}, },
type?: z.ZodType<Data>, type?: z.ZodType<Data>,
token?: string | null, token?: string | null,
@ -87,6 +88,7 @@ export const queryFn = async <Data,>(
...(token ? { Authorization: token } : {}), ...(token ? { Authorization: token } : {}),
...("body" in context ? { "Content-Type": "application/json" } : {}), ...("body" in context ? { "Content-Type": "application/json" } : {}),
}, },
signal: "abortSignal" in context ? context.abortSignal : undefined,
}); });
} catch (e) { } catch (e) {
console.log("Fetch error", e); console.log("Fetch error", e);