Skip to content

Instantly share code, notes, and snippets.

@antpb
Created August 9, 2020 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antpb/85f38045ada56f60ef465284927a8cd1 to your computer and use it in GitHub Desktop.
Save antpb/85f38045ada56f60ef465284927a8cd1 to your computer and use it in GitHub Desktop.
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