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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import { http_delete_async, http_get_async, http_post_async } from "$lib/api/_fetch";
import { api_base, CookieNames } from "$lib/configuration";
import { is_known_problem } from "$lib/models/internal/KnownProblem";
import type { CreateAccountPayload, CreateAccountResponse, DeleteAccountResponse, IAccountService, LoginPayload, LoginResponse, Session, UpdateAccountPayload, UpdateAccountResponse } from "./abstractions/IAccountService";
export class AccountService implements IAccountService {
session: Session;
async login_async(payload: LoginPayload): Promise<LoginResponse> {
const response = await http_post_async(api_base("_/account/login"), payload);
if (response.ok) return { isLoggedIn: true };
if (is_known_problem(response)) return {
isLoggedIn: false,
knownProblem: await response.json()
};
return {
isLoggedIn: false
}
}
async logout_async(): Promise<void> {
const response = await http_get_async(api_base("_/account/logout"));
if (!response.ok) {
const deleteCookieResponse = await fetch("/delete-cookie?key=" + CookieNames.session);
if (!deleteCookieResponse.ok) {
throw new Error("Could neither logout nor delete session cookie.");
}
}
return;
}
async create_account_async(payload: CreateAccountPayload): Promise<CreateAccountResponse> {
const response = await http_post_async(api_base("_/account/create"), payload);
if (response.ok) return { isCreated: true };
if (is_known_problem(response)) return {
isCreated: false,
knownProblem: await response.json()
}
return {
isCreated: false
}
}
async delete_current_async(): Promise<DeleteAccountResponse> {
const response = await http_delete_async(api_base("_/account/delete"));
return {
isDeleted: response.ok
}
}
async update_current_async(payload: UpdateAccountPayload): Promise<UpdateAccountResponse> {
const response = await http_post_async(api_base("_/account/update"), payload);
if (response.ok) return { isUpdated: true };
if (is_known_problem(response)) return {
isUpdated: false,
knownProblem: await response.json()
}
return {
isUpdated: false
}
}
}
|