1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { fetch } from "https://esm.town/v/std/fetch";
import { API_BASE_URL } from "https://esm.town/v/mangadex/API_BASE_URL";
type LoginSuccess = {
result: "ok";
token: {
session: string;
refresh: string;
};
};
type LoginFailure = {
result: "error";
errors: Array<{
id: string;
status: number;
title: string;
detail: string;
}>;
};
type LoginResponse = LoginSuccess | LoginFailure;
/**
* Creates a session.
* The session token expires after 15 minutes, and the refresh token after 1 month.
*/
export const login = async (
username: string,
password: string,
): Promise<LoginResponse> => {
const response = await fetch(
`${API_BASE_URL}/auth/login`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
},
);
return await response.json();
};