/
login.ts
57 lines (47 loc) · 1.63 KB
/
login.ts
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 { Command } from "@oclif/command";
import { isLogin } from "../lib/configStore";
import { startSpinner, stopSpinner } from "../lib/spinner";
import express = require("express");
import { NextFunction, Request, Response } from 'express';
const open = require("open");
const ConfigStore = require("configstore");
const { dotenv } = require("../index");
export default class Login extends Command {
static description = "Login into your Panacloud Portal Account";
async run() {
const loginSpinner = startSpinner(
"Logging into you Panacloud Panacloud Portal Account..."
);
// Check if user is logged in or not
const login = await isLogin();
if (login) {
stopSpinner(loginSpinner, "You are already logged in", true);
process.exit(1);
}
const app = express();
const port = process.env.PORT;
const config = new ConfigStore("panacloud", {
id: "",
token: "",
});
app.use(express.json());
app.use((req: Request, res: Response, next:NextFunction) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
const ExpressServer = async () => {
const server = await app.listen(port);
app.post("/oauth", (req: Request, res: Response) => {
const { id, token } = req.body;
config.set("id", id);
config.set("token", token);
res.send(`Credentials Received`);
stopSpinner(loginSpinner, "Logged in", false);
server.close();
});
};
await ExpressServer();
await open(`https://beta.panacloud.com/authenticate`);
}
}