import { AtpAgent } from "@atproto/api"; import { parseArgs } from "@std/cli"; export const loginToService = async () => { const { username = Deno.env.get("ATP_USERNAME"), password = Deno.env.get("ATP_PASSWORD"), service = Deno.env.get("SERVICE_URI") ?? "https://bsky.social", proxy = Deno.env.get("SERVICE_DID"), } = parseArgs(Deno.args); const agent = new AtpAgent({ service }); agent.configureProxy(proxy); try { await agent.login({ identifier: username, password }); } catch (e) { console.dir(e); if (e instanceof Error && e.name === "AuthFactorTokenRequiredError") { let authFactorToken; do { authFactorToken = prompt( `Check your email for a login token and enter it below:` ); } while (!authFactorToken); try { await agent.login({ identifier: username, password, authFactorToken, }); } catch (e) { console.error(e); } } } return agent; };