aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'code/app/src/lib')
-rw-r--r--code/app/src/lib/components/button.svelte4
-rw-r--r--code/app/src/lib/services/abstractions/IAccountService.ts53
-rw-r--r--code/app/src/lib/services/account-service.ts54
3 files changed, 108 insertions, 3 deletions
diff --git a/code/app/src/lib/components/button.svelte b/code/app/src/lib/components/button.svelte
index abe62ae..49a9354 100644
--- a/code/app/src/lib/components/button.svelte
+++ b/code/app/src/lib/components/button.svelte
@@ -5,7 +5,6 @@
<script lang="ts">
import pwKey from "$actions/pwKey";
-
import { SpinnerIcon } from "./icons";
export let kind = "primary" as ButtonKind;
@@ -93,7 +92,7 @@
use:pwKey={_pwKey}
{...shared_props}
on:click
- class="{sizeClasses} {kindClasses} {$$restProps.class ?? ''}
+ class="btn {sizeClasses} {kindClasses} {$$restProps.class ?? ''}
{fullWidth
? 'w-full justify-center'
: ''} inline-flex items-center border font-medium rounded shadow-sm focus:outline-none focus:ring-2"
@@ -109,7 +108,6 @@
.reset {
border: 0px;
outline: none;
-
}
.reset:focus {
outline: none;
diff --git a/code/app/src/lib/services/abstractions/IAccountService.ts b/code/app/src/lib/services/abstractions/IAccountService.ts
new file mode 100644
index 0000000..736c3ae
--- /dev/null
+++ b/code/app/src/lib/services/abstractions/IAccountService.ts
@@ -0,0 +1,53 @@
+import type { KnownProblem } from "$lib/models/internal/KnownProblem"
+
+export interface IAccountService {
+ session: Session,
+ login_async(payload: LoginPayload): Promise<LoginResponse>,
+ logout_async(): Promise<void>,
+ create_account_async(payload: CreateAccountPayload): Promise<CreateAccountResponse>,
+ delete_current_async(): Promise<DeleteAccountResponse>,
+ update_current_async(payload: UpdateAccountPayload): Promise<UpdateAccountResponse>,
+}
+
+export type Session = {
+ profile: {
+ username: string,
+ displayName: string,
+ id: string,
+ },
+ lastChecked: number,
+}
+
+export type LoginPayload = {
+ username: string,
+ password: string,
+ persist: boolean
+}
+
+export type LoginResponse = {
+ isLoggedIn: boolean
+}
+
+export type CreateAccountPayload = {
+ username: string,
+ password: string,
+}
+
+export type CreateAccountResponse = {
+ isCreated: boolean,
+ knownProblem?: KnownProblem
+}
+
+export type DeleteAccountResponse = {
+ isDeleted: boolean
+}
+
+export type UpdateAccountPayload = {
+ username: string,
+ password: string
+}
+
+export type UpdateAccountResponse = {
+ isUpdated: boolean,
+ knownProblem?: KnownProblem
+} \ No newline at end of file
diff --git a/code/app/src/lib/services/account-service.ts b/code/app/src/lib/services/account-service.ts
new file mode 100644
index 0000000..90af163
--- /dev/null
+++ b/code/app/src/lib/services/account-service.ts
@@ -0,0 +1,54 @@
+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);
+ return { isLoggedIn: response.ok };
+ }
+ 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
+ }
+ }
+} \ No newline at end of file