Created
August 9, 2020 16:44
-
-
Save antpb/85f38045ada56f60ef465284927a8cd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import readline from "readline"; | |
import { connectToReticulum } from "../src/utils/phoenix-utils"; | |
import Store from "../src/storage/store"; | |
import AuthChannel from "../src/utils/auth-channel"; | |
import configs from "../src/utils/configs.js"; | |
import { Socket } from "phoenix-channels"; | |
import { writeFileSync } from "fs"; | |
const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); | |
(async () => { | |
console.log("Logging into Hubs Cloud.\n"); | |
const host = "your.hubsinstance.com"; | |
if (!host) { | |
console.log("Invalid host."); | |
process.exit(1); | |
} | |
const url = `https://${host}/api/v1/meta`; | |
try { | |
const res = await fetch(url); | |
const meta = await res.json(); | |
if (!meta.phx_host) { | |
throw new Error(); | |
} | |
} catch (e) { | |
console.log("Sorry, that doesn't look like a Hubs Cloud server."); | |
process.exit(0); | |
} | |
configs.RETICULUM_SERVER = host; | |
configs.RETICULUM_SOCKET_PROTOCOL = "wss:"; | |
const socket = await connectToReticulum(false, null, Socket); | |
const store = new Store(); | |
const email = "andmin@your.hubsinstance.com"; | |
console.log(`Logging into ${host} as ${email}. Click on the link in your email to continue.`); | |
const authChannel = new AuthChannel(store); | |
authChannel.setSocket(socket); | |
const { authComplete } = await authChannel.startAuthentication(email); | |
await authComplete; | |
const { token } = store.state.credentials; | |
const creds = { | |
host, | |
email, | |
token | |
}; | |
writeFileSync(".ret.credentials", JSON.stringify(creds)); | |
rl.close(); | |
console.log("Login successful.\nCredentials written to .ret.credentials. Run npm run logout to remove credentials."); | |
process.exit(0); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment