Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auth-client usage in web worker and nodejs #611

Merged
merged 1 commit into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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