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

Mysky login flow fixes #403

Merged
merged 4 commits into from
Mar 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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 @@ -359,8 +359,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
11 changes: 7 additions & 4 deletions src/mysky/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@ export class MySky {
// // If we can log in to the portal account, set up auto-relogin.
// if (await this.checkPortalLogin()) {
// this.connector.client.customOptions.loginFn = this.portalLogin;
// } else {
// // Clear the old login function.
// this.connector.client.customOptions.loginFn = undefined;
// }
// } catch (e) {
// // Don't throw an error if we couldn't redirect. The user will never be
Expand Down Expand Up @@ -660,9 +663,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 @@ -676,7 +679,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