forked from
slices.network/slices
Highly ambitious ATProtocol AppView service and sdks
1import { parseArgs } from "@std/cli/parse-args";
2import { performDeviceFlow } from "../auth/device_flow.ts";
3import { ConfigManager } from "../auth/config.ts";
4
5function showLoginHelp() {
6 console.log(`
7slices login - Authenticate with Slices using device code flow
8
9USAGE:
10 slices login [OPTIONS]
11
12OPTIONS:
13 --aip-url <URL> AIP server base URL (default: https://auth.slices.network)
14 --client-id <ID> OAuth client ID (default: 24e77d48-a892-4043-b113-ea241f339397)
15 --scope <SCOPE> OAuth scope (default: atproto transition:generic repo:*)
16 --force Force login even if already authenticated
17 -h, --help Show this help message
18
19EXAMPLES:
20 slices login
21 slices login --aip-url https://custom-aip.example.com
22 slices login --force
23`);
24}
25
26export async function loginCommand(
27 commandArgs: unknown[],
28 _globalArgs: Record<string, unknown>
29): Promise<void> {
30 const args = parseArgs(commandArgs as string[], {
31 boolean: ["help", "force"],
32 string: ["aip-url", "client-id", "scope"],
33 alias: {
34 h: "help",
35 },
36 });
37
38 if (args.help) {
39 showLoginHelp();
40 return;
41 }
42
43 const config = new ConfigManager();
44 await config.load();
45
46 // Check if already authenticated
47 if (!args.force && config.isAuthenticated()) {
48 const authConfig = config.get().auth!;
49 console.log(`Already authenticated as ${authConfig.did}`);
50 console.log("Use --force to login again");
51 return;
52 }
53
54 const aipBaseUrl = args["aip-url"] as string;
55 const clientId = args["client-id"] as string;
56 const scope = args.scope as string;
57
58 if (args.force && config.isAuthenticated()) {
59 await config.logout();
60 }
61
62 await performDeviceFlow(aipBaseUrl, clientId, scope);
63}