Skip to content

Commit

Permalink
Make stored accounts use new tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
mybearworld committed Jul 23, 2024
1 parent 33aca29 commit f65049c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/lib/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const AUTH_CL_RESPONSE = z.object({
val: AUTH_RESPONSE,
});

const STORED_ACCOUNTS_SCHEMA = z.record(z.string(), z.string());
const STORED_ACCOUNTS_SCHEMA = z.object({
newTokenSystem: z.literal(true),
accounts: z.record(z.string(), z.string()),
});

export const USERNAME_STORAGE = "roarer2:username";
export const TOKEN_STORAGE = "roarer2:token";
Expand All @@ -23,7 +26,7 @@ export const STORED_ACCOUNTS_STORAGE = "roarer2:storedAccounts";
const parseStoredAccounts = (maybeJSON: string) => {
try {
const parsed = STORED_ACCOUNTS_SCHEMA.parse(JSON.parse(maybeJSON));
return { error: false, accounts: parsed } as const;
return { error: false, accounts: parsed.accounts } as const;
} catch {
return { error: true } as const;
}
Expand All @@ -34,7 +37,7 @@ export type AuthSlice = {
username: string;
token: string;
} | null;
storedAccounts: z.infer<typeof STORED_ACCOUNTS_SCHEMA>;
storedAccounts: Record<string, string>;
storeAccount: (username: string, token: string) => void;
removeStoredAccount: (username: string) => void;
finishedAuth: () => Promise<void>;
Expand Down Expand Up @@ -75,6 +78,7 @@ export const createAuthSlice: Slice<AuthSlice> = (set, get) => {
username: parsed.data.val.account._id,
token: parsed.data.val.token,
};
draft.storeAccount(draft.credentials.username, draft.credentials.token);
const home = draft.chatPosts.home;
if (!home || home.error) {
return;
Expand Down

0 comments on commit f65049c

Please sign in to comment.