Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Use temporary instead of permanent redirect during login #363

Merged
merged 2 commits into from
May 11, 2020
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
27 changes: 17 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,34 @@ exports.login = async function (options) {

// attempt to capture accountId automatically via browser callback
let tempUrl;
// See: https://github.com/near/near-shell/issues/358
const isMac = process.platform === 'darwin';
const isWin = process.platform === 'win32';

// find a callback URL on the local machine
try {
// capture callback isn't working reliably outside of Mac. This is a workaround to not use it
if (isMac) {
if (!isWin) { // capture callback is currently not working on windows. This is a workaround to not use it
tempUrl = await capture.callback(5000);
// if we found a suitable URL, attempt to use it
newUrl.searchParams.set('success_url', `http://${tempUrl.hostname}:${tempUrl.port}`);
await openUrl(newUrl);
} else {
// redirect automatically, but do not use the browser callback
await openUrl(newUrl);
}
} catch (error) {
// console.error("Failed to find suitable port.", error.message)
// TODO: Is it? Try triggering error
// silent error is better here
}

// if we found a suitable URL, attempt to use it
if (tempUrl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can move all this logic into the above try catch under if (!isWin) but I'm fine with it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikedotexe I just reverted previous change in this part of code.

if (process.env.GITPOD_WORKSPACE_URL) {
const workspaceUrl = new URL(process.env.GITPOD_WORKSPACE_URL);
newUrl.searchParams.set('success_url', `https://${tempUrl.port}-${workspaceUrl.hostname}`);
// Browser not opened, as will open automatically for opened port
} else {
newUrl.searchParams.set('success_url', `http://${tempUrl.hostname}:${tempUrl.port}`);
openUrl(newUrl);
}
} else if (isWin) {
// redirect automatically on windows, but do not use the browser callback
openUrl(newUrl);
}

console.log(chalk`\n{dim If your browser doesn't automatically open, please visit this URL\n${newUrl.toString()}}`);

const getAccountFromWebpage = async () => {
Expand Down
2 changes: 1 addition & 1 deletion utils/capture-login-success.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const payload = (fields, { port, hostname }, redirectUrl) => new Promise((resolv
resolve(results);
});
} else {
res.writeHead(301, { Location: redirectUrl });
res.writeHead(302, { Location: redirectUrl });
res.end();
}
} catch (e) {
Expand Down