diff --git a/.gitpod.yml b/.gitpod.yml index 4f0be795..cef968d0 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -4,5 +4,5 @@ github: prebuilds: addBadge: true ports: - - port: 5000-5010 - onOpen: ignore \ No newline at end of file + - port: 6000 + onOpen: open-browser \ No newline at end of file diff --git a/index.js b/index.js index a2ce0e5d..4541d55a 100644 --- a/index.js +++ b/index.js @@ -63,7 +63,7 @@ exports.login = async function(options) { // find a callback URL on the local machine try { - tempUrl = await capture.callback(5000); + tempUrl = await capture.callback(6000); } catch (error) { // console.error("Failed to find suitable port.", error.message) // TODO: Is it? Try triggering error @@ -75,15 +75,16 @@ exports.login = async function(options) { 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}`); - } - try { - // open a browser to capture NEAR Wallet callback (and quietly direct the user if open fails) - await open(newUrl.toString()); - } catch (error) { - console.error(`Failed to open the URL [ ${newUrl.toString()} ]`, error); + try { + // open a browser to capture NEAR Wallet callback (and quietly direct the user if open fails) + await open(newUrl.toString()); + } catch (error) { + console.error(`Failed to open the URL [ ${newUrl.toString()} ]`, error); + } } } @@ -91,7 +92,7 @@ exports.login = async function(options) { const getAccountFromWebpage = async () => { // capture account_id as provided by NEAR Wallet - const [accountId] = await capture.payload(['account_id'], tempUrl); + const [accountId] = await capture.payload(['account_id'], tempUrl, newUrl); return accountId; }; diff --git a/utils/capture-login-success.js b/utils/capture-login-success.js index 27e9b462..5da75812 100644 --- a/utils/capture-login-success.js +++ b/utils/capture-login-success.js @@ -13,30 +13,35 @@ let server; @param port the port the server should use @param hostname the hostname the server should use */ -const payload = (fields, { port, hostname }) => new Promise((resolve, reject) => { - - const message = renderWebPage('You are logged in. Please close this window.'); +const payload = (fields, { port, hostname }, redirectUrl) => new Promise((resolve, reject) => { server = stoppable(http.createServer(handler)).listen(port, hostname); /** request handler for single-use node server */ function handler(req, res){ - res.statusCode = 200; - res.setHeader('Content-Type', 'text/html'); try { let parsedUrl = url.parse(req.url, true); let results = fields.map((field) => parsedUrl.query[field]); - res.end(message, () => { - server.stop(); - resolve(results); - }); + + if (Object.keys(parsedUrl.query).length > 0) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + // TODO: Make code more specialized (vs handling generic fields) and only output this if login succeeded end to end + res.end(renderWebPage('You are logged in. Please close this window.'), () => { + server.stop(); + resolve(results); + }); + } else { + res.writeHead(301, { Location: redirectUrl }); + res.end(); + } } catch (e) { - console.log('Unexpected error: ', e); + console.error('Unexpected error: ', e); res.statusCode = 400; res.end('It\'s a scam!'); server.stop(); - reject('Failed to capture accountId'); + reject(new Error('Failed to capture accountId')); } } }); @@ -49,6 +54,11 @@ const payload = (fields, { port, hostname }) => new Promise((resolve, reject) => @param range the number of ports to try scanning before giving up */ const callback = async (port = 3000, hostname = '127.0.0.1', range = 10) => { + if (process.env.GITPOD_WORKSPACE_URL) { + // NOTE: Port search interferes with GitPod port management + return { port, hostname }; + } + const start = port; const end = start + range; let inUse = true;