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

Conversation

peterpeterparker
Copy link
Member

Description

auth-client cannot be used in web worker or in NodeJS context because window.localStorage is not available.

e.g. stacktrace while trying to use v0.13.1 in a jest test:

ReferenceError: localStorage is not defined

  18 |  */
  19 | export const createAuthClient = (): Promise<AuthClient> =>
> 20 |   AuthClient.create({
     |              ^
  21 |     idleOptions: {
  22 |       disableIdle: true,
  23 |       disableDefaultIdleCallback: true,

  at _openDbStore (node_modules/@dfinity/auth-client/src/db.ts:15:3)
  at Function.create (node_modules/@dfinity/auth-client/src/db.ts:74:22)
  at node_modules/@dfinity/auth-client/src/storage.ts:74:17
  at IdbStorage.get _db [as _db] (node_modules/@dfinity/auth-client/src/storage.ts:72:12)
  at IdbStorage.get (node_modules/@dfinity/auth-client/src/storage.ts:82:27)
  at Function.create (node_modules/@dfinity/auth-client/src/index.ts:197:48)
  at createAuthClient (src/lib/utils/auth.utils.ts:20:14)
  at Object.signOut (src/lib/stores/auth.store.ts:69:60)
  at Object.<anonymous> (src/tests/lib/stores/auth.store.spec.ts:10:21)

Fixes # (issue)

NNS-dapp PR #1247

How Has This Been Tested?

Manually in the browser (window and web worker) and with a jest test.

@krpeacock
Copy link
Contributor

This works! Auth-client was never particularly intended for a Nodejs context, but web workers are definitely a valid use case. Thanks for the improvement!

@krpeacock krpeacock merged commit 04ce9d1 into main Aug 23, 2022
@krpeacock krpeacock deleted the fix/nodejs-usage branch August 23, 2022 15:46
peterpeterparker added a commit to dfinity/nns-dapp that referenced this pull request Aug 24, 2022
# Motivation

agent-js `v0.13.x` introduces a breaking change. It uses indexeddb instead of local storage to save the session delegation and identity.

Indexddb is awesome and it's the most convenient way to access these information from web workers which we are already using for the cron that checks the delegation and which we might use for more features in the future.

# ⚠️ Important Note ⚠️ 

- on logout, local storage is not going cleared anymore but idb instead
- we don't explicitly clear idb but defer the work to agent-js which do so
- instead of syncing the auth on local storage changes, the identity will be checked in the auth cron

#  Dependencies

fake-indexeddb (https://github.com/dumbmatter/fakeIndexedDB) has been added to mock idb for jest tests happening in the nodejs context

# PRs required

- [X] ic-js PR dfinity/ic-js#177
- [x] agent-js PR dfinity/agent-js#611
- [x] agent-js PR dfinity/agent-js#612

# Changes

- in web worker, read delegation from idb instead of localstorage to check the validity of the delegation
- in web worker, also check the auth is authenticated
- on logout, do not clear local storage
- mock idb for test with new dev dependency `fake-indexeddb`
- remove auth sync on local storage changes
- bump agent-js `v0.13.2`
- bump most recent nns-js, sns-js and utils-js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants