Skip to content

Commit

Permalink
should work
Browse files Browse the repository at this point in the history
  • Loading branch information
yofukashino committed Dec 8, 2023
1 parent 85a7608 commit d109be1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
57 changes: 19 additions & 38 deletions src/lib/stores/AuthorizationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,25 @@ interface AuthorizationState {
isAuthorized: () => boolean;
}

const indexedDBStorage: StateStorage = {
async getItem(name: string): Promise<string | null> {
return (await authorizationToken).get(name).then((v) => v ?? null);
export const useAuthorizationStore = {
token: authorizationToken.get("token", null),
tokens: authorizationToken.get("tokens", {
[users.getCurrentUser().id]: authorizationToken.get("token", null),
}),
init: () => {},
setToken: (token: string) => {
authorizationToken.set("token", token);
authorizationToken.set("tokens", {
...authorizationToken.get("tokens", {}),
[users.getCurrentUser().id]: token,
});
},
async setItem(name: string, value: string): Promise<void> {
(await authorizationToken).set(name, value);
},
async removeItem(name: string): Promise<void> {
(await authorizationToken).set(name, null);
remove: (id: string) => {
const tokens = authorizationToken.get("tokens", {});

delete tokens[id];
authorizationToken.set("tokens", tokens);
},
authorize: () => void showAuthorizationModal(),
isAuthorized: () => Boolean(authorizationToken.get("token", null)),
};

export const useAuthorizationStore = create<AuthorizationState>(
persist(
(set, get) => ({
token: null,
tokens: {},
init: () => {
set({ token: get().tokens[users.getCurrentUser().id] ?? null });
},
setToken: (token: string) =>
set({ token, tokens: { ...get().tokens, [users.getCurrentUser().id]: token } }),
remove: (id: string) => {
const { tokens, init } = get();
const newTokens = { ...tokens };
delete newTokens[id];
set({ tokens: newTokens });

init();
},
authorize: () => void showAuthorizationModal(),
isAuthorized: () => !!get().token,
}),
{
name: "decor-auth",
getStorage: () => indexedDBStorage,
partialize: (state) => ({ tokens: state.tokens }),
onRehydrateStorage: () => (state) => state?.init(),
},
),
);
6 changes: 3 additions & 3 deletions src/lib/utils/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { settings } from "replugged"
import { settings } from "replugged";

export const defaultSettings = {}
export const authorizationToken = settings.init("decor.auth", defaultSettings)
export const defaultSettings = {};
export const authorizationToken = await settings.init("decor.auth", defaultSettings);

0 comments on commit d109be1

Please sign in to comment.