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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
import {http_delete_async, http_get_async, http_post_async} from "$api/_fetch";
import {api_base, CookieNames, StorageKeys} from "$configuration";
import {is_known_problem} from "$models/internal/KnownProblem";
import {log_debug} from "$help/logger";
import {StoreType, writable_persistent} from "$help/persistent-store";
import {get} from "svelte/store";
import type {Writable} from "svelte/store";
import {Temporal} from "temporal-polyfill";
import type {
CreateAccountPayload,
CreateAccountResponse,
DeleteAccountResponse,
IAccountService,
LoginPayload,
LoginResponse,
Session,
UpdateAccountPayload,
UpdateAccountResponse,
} from "./abstractions/IAccountService";
export class AccountService implements IAccountService {
session: Writable<Session>;
private sessionCooldown = 3600;
constructor() {
this.session = writable_persistent({
name: StorageKeys.session,
initialState: {} as Session,
options: {
store: StoreType.LOCAL,
},
});
this.refresh_session();
}
async refresh_session(forceRefresh: boolean = false): Promise<void> {
const currentValue = get(this.session);
const currentEpoch = Temporal.Now.instant().epochSeconds;
if (currentValue?._lastUpdated + this.sessionCooldown < currentEpoch) {
log_debug("Session is not stale yet", {
currentEpoch,
staleEpoch: currentValue?._lastUpdated + this.sessionCooldown,
});
return;
}
const sessionResponse = await http_get_async(api_base("_/account/session"));
if (sessionResponse.ok) {
}
}
async end_session(callback: Function): Promise<void> {
await this.logout_async();
this.session.set(null);
if (typeof callback === "function") callback();
return;
}
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,
};
}
}
|