Skip to content

Commit 399a5bc

Browse files
authored
add both IPs to the CLI (#120)
Signed-off-by: Derek Anderson <dmikey@users.noreply.github.com>
1 parent 5c981c9 commit 399a5bc

1 file changed

Lines changed: 62 additions & 56 deletions

File tree

src/commands/login/index.ts

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
import Chalk from "chalk"
1+
import Chalk from "chalk";
22
import Fastify from "fastify";
33
import { getDb } from "../../store/db";
44
import { openInBrowser } from "../../lib/browser";
5-
import { getGatewayUrl, validateGatewayVersion } from "../../lib/urls"
6-
import axios from "axios"
5+
import { getGatewayUrl, validateGatewayVersion } from "../../lib/urls";
6+
import axios from "axios";
77

88
const portastic = require("portastic");
99
const clientId = "7ddcb826-e84a-4102-b95b-d9b8d3a57176";
1010

1111
const fastify = Fastify({
12-
// no logging to the console
13-
logger: false,
14-
//return jwt bigger than default
15-
maxParamLength: 10000,
12+
// no logging to the console
13+
logger: false,
14+
//return jwt bigger than default
15+
maxParamLength: 10000,
1616
});
1717

1818
// we have the jwt, store it
1919
fastify.get("/token/:userId", async (request: any, reply: any) => {
20-
const userId = request.params.userId;
21-
const error = request.params.error;
22-
getDb().set("config.token", userId).write();
20+
const userId = request.params.userId;
21+
const error = request.params.error;
22+
getDb().set("config.token", userId).write();
2323

24-
if (error) {
25-
console.log("Error when attempting to login");
26-
return;
27-
}
24+
if (error) {
25+
console.log("Error when attempting to login");
26+
return;
27+
}
2828

29-
reply.redirect("/complete");
29+
reply.redirect("/complete");
3030
});
3131

3232
fastify.get("/complete", async (request: any, reply: any) => {
33-
const html = `<!DOCTYPE html>
33+
const html = `<!DOCTYPE html>
3434
<html lang="en">
3535
<head>
3636
<style>
@@ -86,58 +86,64 @@ fastify.get("/complete", async (request: any, reply: any) => {
8686
</div>
8787
</body>
8888
</html>`;
89-
reply.header("Content-Type", "text/html").send(html);
89+
reply.header("Content-Type", "text/html").send(html);
9090

91-
console.log('')
92-
console.log(Chalk.green('Authentication Completed!'));
93-
console.log('You have successfully authenticated with the server.')
94-
console.log('')
95-
fastify.close()
96-
process.exit(0)
91+
console.log("");
92+
console.log(Chalk.green("Authentication Completed!"));
93+
console.log("You have successfully authenticated with the server.");
94+
console.log("");
95+
fastify.close();
96+
process.exit(0);
9797
});
9898

9999
// run the server
100100
const start = async (url: string) => {
101-
try {
102-
const ports = await portastic.find({ min: 8000, max: 8999 });
103-
const serverPort = ports[Math.floor(Math.random() * ports.length)];
101+
try {
102+
const ports = await portastic.find({ min: 8000, max: 8999 });
103+
const serverPort = ports[Math.floor(Math.random() * ports.length)];
104104

105-
fastify.get("/", async (request, reply) => {
106-
reply.redirect(
107-
`${url}/login?redirect=http://0.0.0.0:${serverPort}/token&clientId=${clientId}`
108-
);
109-
});
105+
fastify.get("/", async (request, reply) => {
106+
const host = request.headers.host;
107+
reply.redirect(
108+
`${url}/login?redirect=http://${host}/token&clientId=${clientId}`,
109+
);
110+
});
110111

111-
fastify.listen({ port: serverPort }).then(async () => {
112-
console.log(
113-
`Open Browser at http://0.0.0.0:${serverPort} to complete login`
114-
);
115-
openInBrowser(`http://0.0.0.0:${serverPort}`);
116-
});
117-
} catch (err) {
118-
fastify.log.error(err);
119-
process.exit(1);
120-
}
112+
fastify.listen({ port: serverPort }).then(async () => {
113+
console.log(
114+
`Open Browser at http://0.0.0.0:${serverPort} to complete login`,
115+
);
116+
console.log(
117+
`-OR- Open Browser at http://127.0.0.1:${serverPort} to complete login`,
118+
);
119+
// openInBrowser(`http://0.0.0.0:${serverPort}`);
120+
});
121+
} catch (err) {
122+
fastify.log.error(err);
123+
process.exit(1);
124+
}
121125
};
122126

123127
// run the command when cli is called
124128
export async function run(options?: any) {
125-
if (options?.authUrl) {
126-
getDb().set("config.authUrl", options?.authUrl.replace(/^https?:\/\//, '')).write();
127-
}
129+
if (options?.authUrl) {
130+
getDb()
131+
.set("config.authUrl", options?.authUrl.replace(/^https?:\/\//, ""))
132+
.write();
133+
}
128134

129-
if (options?.authPort) {
130-
getDb().set("config.authPort", parseInt(options?.authPort)).write();
131-
}
135+
if (options?.authPort) {
136+
getDb().set("config.authPort", parseInt(options?.authPort)).write();
137+
}
132138

133-
const gatewayUrl = getGatewayUrl()
134-
const gatewayVersion = await validateGatewayVersion(gatewayUrl)
135-
getDb().set("config.apiVersion", gatewayVersion).write()
139+
const gatewayUrl = getGatewayUrl();
140+
const gatewayVersion = await validateGatewayVersion(gatewayUrl);
141+
getDb().set("config.apiVersion", gatewayVersion).write();
136142

137-
if (options?.authToken) {
138-
const token = options?.authToken;
139-
getDb().set("config.token", token).write();
140-
} else {
141-
start(gatewayUrl);
142-
}
143+
if (options?.authToken) {
144+
const token = options?.authToken;
145+
getDb().set("config.token", token).write();
146+
} else {
147+
start(gatewayUrl);
148+
}
143149
}

0 commit comments

Comments
 (0)