refactor: loginManually takes promptLogin

main
spacemeowx2 2022-11-18 16:53:16 +08:00 committed by imspace
parent b5e314eaf1
commit 597960df32
2 changed files with 17 additions and 9 deletions

View File

@ -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,

View File

@ -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<string>;
},
): Promise<string> {
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");
}