Skip to content

Commit

Permalink
fix: auth-client usage in web worker and nodejs (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Aug 23, 2022
1 parent 1cafb38 commit 04ce9d1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/auth-client/src/db.ts
@@ -1,5 +1,5 @@
import { openDB, IDBPDatabase } from 'idb';
import { KEY_STORAGE_DELEGATION, KEY_STORAGE_KEY } from './storage';
import { isBrowser, KEY_STORAGE_DELEGATION, KEY_STORAGE_KEY } from './storage';

type Database = IDBPDatabase<unknown>;
type IDBValidKey = string | number | Date | BufferSource | IDBValidKey[];
Expand All @@ -12,7 +12,7 @@ const _openDbStore = async (
version: number,
) => {
// Clear legacy stored delegations
if (localStorage && localStorage.getItem(KEY_STORAGE_DELEGATION)) {
if (isBrowser && localStorage?.getItem(KEY_STORAGE_DELEGATION)) {
localStorage.removeItem(KEY_STORAGE_DELEGATION);
localStorage.removeItem(KEY_STORAGE_KEY);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/auth-client/src/index.ts
Expand Up @@ -18,6 +18,7 @@ import { IdleManager, IdleManagerOptions } from './idleManager';
import {
AuthClientStorage,
IdbStorage,
isBrowser,
KEY_STORAGE_DELEGATION,
KEY_STORAGE_KEY,
KEY_VECTOR,
Expand Down Expand Up @@ -195,7 +196,7 @@ export class AuthClient {
key = options.identity;
} else {
let maybeIdentityStorage = await storage.get(KEY_STORAGE_KEY);
if (!maybeIdentityStorage) {
if (!maybeIdentityStorage && isBrowser) {
// Attempt to migrate from localstorage
try {
const fallbackLocalStorage = new LocalStorage();
Expand Down
2 changes: 2 additions & 0 deletions packages/auth-client/src/storage.ts
Expand Up @@ -6,6 +6,8 @@ export const KEY_VECTOR = 'iv';
// Increment if any fields are modified
export const DB_VERSION = 1;

export const isBrowser = typeof window !== 'undefined';

/**
* Interface for persisting user authentication data
*/
Expand Down

0 comments on commit 04ce9d1

Please sign in to comment.