|
1 | | -import Chalk from "chalk" |
| 1 | +import Chalk from "chalk"; |
2 | 2 | import Fastify from "fastify"; |
3 | 3 | import { getDb } from "../../store/db"; |
4 | 4 | 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"; |
7 | 7 |
|
8 | 8 | const portastic = require("portastic"); |
9 | 9 | const clientId = "7ddcb826-e84a-4102-b95b-d9b8d3a57176"; |
10 | 10 |
|
11 | 11 | 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, |
16 | 16 | }); |
17 | 17 |
|
18 | 18 | // we have the jwt, store it |
19 | 19 | 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(); |
23 | 23 |
|
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 | + } |
28 | 28 |
|
29 | | - reply.redirect("/complete"); |
| 29 | + reply.redirect("/complete"); |
30 | 30 | }); |
31 | 31 |
|
32 | 32 | fastify.get("/complete", async (request: any, reply: any) => { |
33 | | - const html = `<!DOCTYPE html> |
| 33 | + const html = `<!DOCTYPE html> |
34 | 34 | <html lang="en"> |
35 | 35 | <head> |
36 | 36 | <style> |
@@ -86,58 +86,64 @@ fastify.get("/complete", async (request: any, reply: any) => { |
86 | 86 | </div> |
87 | 87 | </body> |
88 | 88 | </html>`; |
89 | | - reply.header("Content-Type", "text/html").send(html); |
| 89 | + reply.header("Content-Type", "text/html").send(html); |
90 | 90 |
|
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); |
97 | 97 | }); |
98 | 98 |
|
99 | 99 | // run the server |
100 | 100 | 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)]; |
104 | 104 |
|
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 | + }); |
110 | 111 |
|
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 | + } |
121 | 125 | }; |
122 | 126 |
|
123 | 127 | // run the command when cli is called |
124 | 128 | 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 | + } |
128 | 134 |
|
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 | + } |
132 | 138 |
|
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(); |
136 | 142 |
|
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 | + } |
143 | 149 | } |
0 commit comments