-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathlogin.ts
More file actions
57 lines (52 loc) · 1.24 KB
/
login.ts
File metadata and controls
57 lines (52 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {
isAuthenticated,
startAuthenticationSession,
checkAuthSession,
} from "./api";
import open from "open";
import { exit } from "process";
import { prompt } from "enquirer";
import ora from "ora";
import chalk from "chalk";
export async function login() {
const _spnr_check_auth = ora("Verifing...").start();
if (await isAuthenticated()) {
_spnr_check_auth.succeed("You are already logged in.");
exit(0);
} else {
_spnr_check_auth.stop();
}
//
let session;
let url;
try {
const { authUrl, id: _session } = await startAuthenticationSession();
session = _session;
url = authUrl;
await open(authUrl);
} catch (e) {
throw new Error("Error while starting authentication session");
exit(1);
}
//
await prompt({
name: "continue",
message:
"Press enter after you have completed authentication on your browser.",
type: "input",
initial: "Continue",
format(value) {
return "";
},
// @ts-ignore
async validate() {
try {
return await checkAuthSession(session);
} catch (e) {
return "Seems like you are not logged in. Please try again - " + url;
}
},
});
console.log(chalk.green("✔️") + " Logged in.");
return;
}