From 597960df32e1771248cee2805b4b2c43aaa8395c Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Fri, 18 Nov 2022 16:53:16 +0800 Subject: [PATCH] refactor: loginManually takes promptLogin --- src/app.ts | 13 ++++++++++++- src/iksm.ts | 13 +++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/app.ts b/src/app.ts index 677b39f..6024c9c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -263,9 +263,20 @@ export class App { } async run() { await this.profile.readState(); + const { logger, readline, newFetcher } = this.env; if (!this.profile.state.loginState?.sessionToken) { - const sessionToken = await loginManually(this.env); + const sessionToken = await loginManually({ + newFetcher, + promptLogin: async (url: string) => { + logger.log("Navigate to this URL in your browser:"); + logger.log(url); + logger.log( + 'Log in, right click the "Select this account" button, copy the link address, and paste it below:', + ); + return await readline(); + }, + }); await this.profile.writeState({ ...this.profile.state, diff --git a/src/iksm.ts b/src/iksm.ts index 8af8a4c..cb66932 100644 --- a/src/iksm.ts +++ b/src/iksm.ts @@ -9,7 +9,10 @@ import { APIError } from "./APIError.ts"; import { Env, Fetcher } from "./env.ts"; export async function loginManually( - { logger, readline, newFetcher }: Env, + { newFetcher, promptLogin }: { + newFetcher: () => Fetcher; + promptLogin: (url: string) => Promise; + }, ): Promise { const fetch = newFetcher(); @@ -52,13 +55,7 @@ export async function loginManually( }, ); - logger.log("Navigate to this URL in your browser:"); - logger.log(res.url); - logger.log( - 'Log in, right click the "Select this account" button, copy the link address, and paste it below:', - ); - - const login = (await readline()).trim(); + const login = (await promptLogin(res.url)).trim(); if (!login) { throw new Error("No login URL provided"); }