WIP: A simple cli for daily tangled use cases and AI integration. This is for my personal use right now, but happy if others get mileage from it! :)
1import { Command } from 'commander';
2import { createApiClient } from '../lib/api-client.js';
3import { getCurrentSessionMetadata } from '../lib/session.js';
4import { promptForLogin } from '../utils/prompts.js';
5
6/**
7 * Create the auth command with login and logout subcommands
8 */
9export function createAuthCommand(): Command {
10 const auth = new Command('auth');
11 auth.description('Manage authentication with AT Protocol');
12
13 // Login command
14 auth
15 .command('login')
16 .description('Login to your AT Protocol account')
17 .action(async () => {
18 try {
19 const client = createApiClient();
20
21 // Check if already logged in
22 const existingSession = await getCurrentSessionMetadata();
23 if (existingSession) {
24 console.log(`Already logged in as @${existingSession.handle}`);
25 console.log('Run "tangled auth logout" first to switch accounts');
26 process.exit(0);
27 }
28
29 // Prompt for credentials
30 const { identifier, password } = await promptForLogin();
31
32 // Attempt login
33 console.log('\nAuthenticating...');
34 const session = await client.login(identifier, password);
35
36 console.log(`\n✓ Successfully logged in as @${session.handle}`);
37 console.log(` DID: ${session.did}`);
38 } catch (error) {
39 console.error(
40 `\n✗ Login failed: ${error instanceof Error ? error.message : 'Unknown error'}`
41 );
42 process.exit(1);
43 }
44 });
45
46 // Logout command
47 auth
48 .command('logout')
49 .description('Logout and clear stored credentials')
50 .action(async () => {
51 try {
52 const client = createApiClient();
53
54 // Check if logged in
55 const session = await getCurrentSessionMetadata();
56 if (!session) {
57 console.log('Not currently logged in');
58 process.exit(0);
59 }
60
61 // Perform logout
62 await client.logout();
63
64 console.log(`✓ Logged out @${session.handle}`);
65 } catch (error) {
66 console.error(
67 `✗ Logout failed: ${error instanceof Error ? error.message : 'Unknown error'}`
68 );
69 process.exit(1);
70 }
71 });
72
73 // Status command
74 auth
75 .command('status')
76 .description('Check authentication status')
77 .action(async () => {
78 try {
79 const session = await getCurrentSessionMetadata();
80
81 if (session) {
82 console.log('✓ Authenticated');
83 console.log(` Handle: @${session.handle}`);
84 console.log(` DID: ${session.did}`);
85 console.log(` PDS: ${session.pds}`);
86 console.log(` Last used: ${new Date(session.lastUsed).toLocaleString()}`);
87 } else {
88 console.log('✗ Not authenticated');
89 console.log('Run "tangled auth login" to authenticate');
90 }
91 } catch (error) {
92 console.error(
93 `✗ Failed to check status: ${error instanceof Error ? error.message : 'Unknown error'}`
94 );
95 process.exit(1);
96 }
97 });
98
99 return auth;
100}