aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/services/account-service.ts
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-12-11 20:46:58 +0100
committerivarlovlie <git@ivarlovlie.no>2022-12-11 20:47:06 +0100
commit6561771c435f9d9bed1589b5ed13d17aee0b7873 (patch)
tree2c47e60fa4aaaa5bf1e151838ac197a61f4377cc /code/app/src/services/account-service.ts
parent8da37c77cae0c7f712a775e3996afd9d84b0f9af (diff)
downloadgreatoffice-6561771c435f9d9bed1589b5ed13d17aee0b7873.tar.xz
greatoffice-6561771c435f9d9bed1589b5ed13d17aee0b7873.zip
feat: Add frontpage
Diffstat (limited to 'code/app/src/services/account-service.ts')
-rw-r--r--code/app/src/services/account-service.ts35
1 files changed, 20 insertions, 15 deletions
diff --git a/code/app/src/services/account-service.ts b/code/app/src/services/account-service.ts
index 9d45950..92c6126 100644
--- a/code/app/src/services/account-service.ts
+++ b/code/app/src/services/account-service.ts
@@ -1,4 +1,5 @@
import {http_delete_async, http_get_async, http_post_async} from "$api/_fetch";
+import {browser} from "$app/environment";
import {api_base, CookieNames, StorageKeys} from "$configuration";
import {is_known_problem} from "$models/internal/KnownProblem";
import {log_debug} from "$help/logger";
@@ -19,24 +20,29 @@ import type {
} from "./abstractions/IAccountService";
export class AccountService implements IAccountService {
- session: Writable<Session>;
+ session: Writable<Session> | undefined;
private sessionCooldown = 3600;
constructor() {
- this.session = writable_persistent({
- name: StorageKeys.session,
- initialState: {} as Session,
- options: {
- store: StoreType.LOCAL,
- },
- });
- this.refresh_session();
+ if (browser) {
+ this.session = writable_persistent({
+ name: StorageKeys.session,
+ initialState: {} as Session,
+ options: {
+ store: StoreType.LOCAL,
+ },
+ });
+ this.refresh_session();
+ } else {
+ this.session = undefined;
+ }
}
async refresh_session(forceRefresh: boolean = false): Promise<void> {
+ if (!this.session) return;
const currentValue = get(this.session);
const currentEpoch = Temporal.Now.instant().epochSeconds;
- if (currentValue?._lastUpdated + this.sessionCooldown < currentEpoch) {
+ if (!forceRefresh && ((currentValue?._lastUpdated ?? 0) + this.sessionCooldown) > currentEpoch) {
log_debug("Session is not stale yet", {
currentEpoch,
staleEpoch: currentValue?._lastUpdated + this.sessionCooldown,
@@ -45,11 +51,14 @@ export class AccountService implements IAccountService {
}
const sessionResponse = await http_get_async(api_base("_/account/session"));
if (sessionResponse.ok) {
-
+ this.session.set(await sessionResponse.json());
+ } else {
+ this.session.set(null);
}
}
async end_session(callback: Function): Promise<void> {
+ if (!this.session) return;
await this.logout_async();
this.session.set(null);
if (typeof callback === "function") callback();
@@ -70,14 +79,12 @@ export class AccountService implements IAccountService {
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;
}
@@ -88,7 +95,6 @@ export class AccountService implements IAccountService {
isCreated: false,
knownProblem: await response.json(),
};
-
return {
isCreated: false,
};
@@ -108,7 +114,6 @@ export class AccountService implements IAccountService {
isUpdated: false,
knownProblem: await response.json(),
};
-
return {
isUpdated: false,
};