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: resolves window.open issue in safari due to async storage call #620

Merged
merged 2 commits into from Aug 31, 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: 4 additions & 0 deletions docs/generated/changelog.html
Expand Up @@ -21,6 +21,10 @@ <h2>Version 1.0.0</h2>
</li>
</ul>
</ul>
<h3>Version 0.13.3</h3>
<ul>
<li>Auth Client moves key fallback generation to the create method instead of login and makes the _key non-nullable. This fixes a regression with async window.open behavior in Safari</li>
</ul>
<h2>Version 0.13.2</h2>
<ul>
<li>auth-client avoids localstorage global and can be used in a web worker or nodejs</li>
Expand Down
19 changes: 4 additions & 15 deletions packages/auth-client/src/index.test.ts
Expand Up @@ -594,7 +594,8 @@ describe('Migration from localstorage', () => {

await AuthClient.create({ storage });

expect(storage.set as jest.Mock).toBeCalledTimes(0);
// Key is stored during creation when none is provided
expect(storage.set as jest.Mock).toBeCalledTimes(1);
});
it('should not attempt to migrate if a delegation is already stored', async () => {
const storage: AuthClientStorage = {
Expand All @@ -609,7 +610,7 @@ describe('Migration from localstorage', () => {

await AuthClient.create({ storage });

expect(storage.set as jest.Mock).toBeCalledTimes(0);
expect(storage.set as jest.Mock).toBeCalledTimes(1);
});
it('should migrate storage from localstorage', async () => {
const localStorage = new LocalStorage();
Expand All @@ -624,18 +625,6 @@ describe('Migration from localstorage', () => {

await AuthClient.create({ storage });

expect(storage.set as jest.Mock).toBeCalledTimes(2);
expect((storage.set as jest.Mock).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"delegation",
"test",
],
Array [
"identity",
"key",
],
]
`);
expect(storage.set as jest.Mock).toBeCalledTimes(3);
});
});
17 changes: 7 additions & 10 deletions packages/auth-client/src/index.ts
Expand Up @@ -256,12 +256,18 @@ export class AuthClient {
? undefined
: IdleManager.create(options.idleOptions);

if (!key) {
// Create a new key (whether or not one was in storage).
key = Ed25519KeyIdentity.generate();
await storage.set(KEY_STORAGE_KEY, JSON.stringify(key));
}

return new this(identity, key, chain, storage, idleManager, options);
}

protected constructor(
private _identity: Identity,
private _key: SignIdentity | null,
private _key: SignIdentity,
private _chain: DelegationChain | null,
private _storage: AuthClientStorage,
public readonly idleManager: IdleManager | undefined,
Expand Down Expand Up @@ -378,14 +384,6 @@ export class AuthClient {
*/
onError?: ((error?: string) => void) | ((error?: string) => Promise<void>);
}): Promise<void> {
let key = this._key;
if (!key) {
// Create a new key (whether or not one was in storage).
key = Ed25519KeyIdentity.generate();
this._key = key;
await this._storage.set(KEY_STORAGE_KEY, JSON.stringify(key));
}

// Set default maxTimeToLive to 8 hours
const defaultTimeToLive = /* hours */ BigInt(8) * /* nanoseconds */ BigInt(3_600_000_000_000);

Expand Down Expand Up @@ -493,7 +491,6 @@ export class AuthClient {

// Reset this auth client to a non-authenticated state.
this._identity = new AnonymousIdentity();
this._key = null;
this._chain = null;

if (options.returnTo) {
Expand Down