Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

Commit

Permalink
feat: enable asynchronous store implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
noramass committed Mar 23, 2020
1 parent 9287abe commit 0e0dd3e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 78 deletions.
12 changes: 6 additions & 6 deletions src/auth-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { AppUserRead, JwtBundle } from "@/model";
import { only } from "@/util";

export interface AuthStore {
getData(storageKey: string): { jwtBundle: JwtBundle, user: AppUserRead } | void;
setData(storageKey: string, {}: { jwtBundle: JwtBundle; user: AppUserRead }): void;
removeData(storageKey: string): void;
getData(storageKey: string): Promise<{ jwtBundle: JwtBundle, user: AppUserRead } | void>;
setData(storageKey: string, {}: { jwtBundle: JwtBundle; user: AppUserRead }): Promise<void>;
removeData(storageKey: string): Promise<void>;
}

export class StorageAuthStore implements AuthStore {
public constructor(protected storage: Storage) {}

public getData(storageKey: string) {
public async getData(storageKey: string) {
const serialized = this.storage.getItem(storageKey);
try {
return only(JSON.parse(serialized!), "jwtBundle", "user")
Expand All @@ -19,12 +19,12 @@ export class StorageAuthStore implements AuthStore {
}
}

public setData(storageKey: string, { jwtBundle, user }: { jwtBundle: JwtBundle; user: AppUserRead }) {
public async setData(storageKey: string, { jwtBundle, user }: { jwtBundle: JwtBundle; user: AppUserRead }) {
const serialized = JSON.stringify({ jwtBundle, user });
this.storage.setItem(storageKey, serialized);
}

removeData(storageKey: string): void {
public async removeData(storageKey: string) {
this.storage.removeItem(storageKey);
}
}
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export class SkeletonKey<USER_DATA = object, TOKEN_DATA = object>
}

public async init() {
this.load();
this.bindMethods();
this.installListeners();
await this.load();
if (this.isLoggedIn() && this.initialLoginCheck) await this.refreshInfo();
await this.installInterval();
this.emit("initialized", this);
Expand Down Expand Up @@ -121,15 +121,15 @@ export class SkeletonKey<USER_DATA = object, TOKEN_DATA = object>
this.jwtBundle = jwtTokenBundle as JwtBundle & TOKEN_DATA;
this.user = user as AppUserRead & USER_DATA;
this.emitSync("login", user);
this.persist();
await this.persist();
return user;
}

public async logout() {
this.user = undefined;
this.jwtBundle = undefined;
this.emitSync("logout");
this.persist();
await this.persist();
return true;
}

Expand Down Expand Up @@ -165,7 +165,7 @@ export class SkeletonKey<USER_DATA = object, TOKEN_DATA = object>
try {
this.user = (await this.client.me(this.jwtBundle!.token)) as AppUserRead & USER_DATA;
this.emitSync("refresh", "user", this.user);
this.persist();
await this.persist();
} catch ({ response: { status } }) {
this.handleStatus(status);
}
Expand Down Expand Up @@ -198,15 +198,15 @@ export class SkeletonKey<USER_DATA = object, TOKEN_DATA = object>
return decode(this.jwtBundle!.refreshToken);
}

public persist() {
if (this.isLoggedIn()) this.store.setData(this.storageKey, only(this, "jwtBundle", "user") as any);
else this.store.removeData(this.storageKey);
public async persist() {
if (this.isLoggedIn()) await this.store.setData(this.storageKey, only(this, "jwtBundle", "user") as any);
else await this.store.removeData(this.storageKey);
}

public load() {
public async load() {
if (!this.isLoggedIn()) {
const data = this.store.getData(this.storageKey);
if (!data) return this.persist();
const data = await this.store.getData(this.storageKey);
if (!data) return await this.persist();
Object.assign(this, only(data, "jwtBundle", "user"));
}
}
Expand Down
Loading

0 comments on commit 0e0dd3e

Please sign in to comment.