Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Fix bug with potential infinite recursion on 401 error
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnski committed Feb 18, 2022
1 parent ce90b88 commit 39f8543
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type CustomClientOptions = {
customCookie?: string;
onDownloadProgress?: (progress: number, event: ProgressEvent) => void;
onUploadProgress?: (progress: number, event: ProgressEvent) => void;
loginFn?: () => Promise<void>;
loginFn?: (config: RequestConfig) => Promise<void>;
};

/**
Expand Down Expand Up @@ -374,8 +374,10 @@ export class SkynetClient {
} catch (e) {
if (config.loginFn && (e as ExecuteRequestError).responseStatus === 401) {
// Try logging in again.
await config.loginFn();
return await this.executeRequest(config);
await config.loginFn(config);
// Unset the login function on the recursive call so that we don't try
// to login again, avoiding infinite loops.
return await this.executeRequest({ ...config, loginFn: undefined });
} else {
throw e;
}
Expand Down
8 changes: 4 additions & 4 deletions src/mysky/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,9 @@ export class MySky {
* Load MySky redirect flow:
*
* 1. SDK opens MySky on the same portal as the skapp.
* 2. If the preferred portal is found in localstorage, MySky connects to it
* and we go to step 5.
* 3. Else, MySky connects to siasky.net.
* 2. If a seed was not found, no preferred portal can be found, so exit the
* flow.
* 3. MySky connects to siasky.net first.
* 4. MySky tries to get the saved portal preference.
* 1. If the portal is set, MySky switches to using the preferred portal.
* 2. If it is not set or we don't have the seed, MySky switches to using
Expand All @@ -651,7 +651,7 @@ export class MySky {
* Login redirect flow:
*
* 1. SDK logs in through the UI.
* 2. MySky UI switches to siasky.net and tries to get the saved portal
* 2. MySky switches to siasky.net and tries to get the saved portal
* preference.
* 1. If the portal is set, MySky switches to using the preferred portal.
* 2. If it is not set or we don't have the seed, MySky switches to using
Expand Down

0 comments on commit 39f8543

Please sign in to comment.