forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1import axios from "axios";
2import chalk from "chalk";
3import cors from "cors";
4import express, { Request, Response } from "express";
5import fs from "fs/promises";
6import open from "open";
7import os from "os";
8import path from "path";
9
10export async function login(handle: string): Promise<void> {
11 const app = express();
12 app.use(cors());
13 app.use(express.json());
14
15 const server = app.listen(6996);
16
17 app.post("/token", async (req: Request, res: Response) => {
18 console.log(chalk.bold(chalk.greenBright("Login successful!\n")));
19 console.log(
20 "You can use this session key (Token) to authenticate with the API."
21 );
22 console.log("Received token (session key):", chalk.green(req.body.token));
23
24 const tokenPath = path.join(os.homedir(), ".rocksky", "token.json");
25 await fs.mkdir(path.dirname(tokenPath), { recursive: true });
26 await fs.writeFile(
27 tokenPath,
28 JSON.stringify({ token: req.body.token }, null, 2)
29 );
30
31 res.json({
32 ok: 1,
33 });
34
35 server.close();
36 });
37
38 const response = await axios.post("https://api.rocksky.app/login", {
39 handle,
40 cli: true,
41 });
42
43 const redirectUrl = response.data;
44
45 if (!redirectUrl.includes("authorize")) {
46 console.error("Failed to login, please check your handle and try again.");
47 server.close();
48 return;
49 }
50
51 console.log("Please visit this URL to authorize the app:");
52 console.log(chalk.cyan(redirectUrl));
53
54 open(redirectUrl);
55}