1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from 'commander' ;
4
+ import chalk from 'chalk' ;
5
+ import inquirer from 'inquirer' ;
6
+ import ora from 'ora' ;
7
+ import Conf from 'conf' ;
8
+ import crypto from 'crypto' ;
9
+ import boxen from 'boxen' ;
10
+
11
+ // Create a separate config for mock auth
12
+ const config = new Conf ( {
13
+ projectName : 'zero-finance-cli-mock' ,
14
+ defaults : {
15
+ auth : {
16
+ token : null ,
17
+ expiresAt : null ,
18
+ email : null ,
19
+ } ,
20
+ } ,
21
+ } ) ;
22
+
23
+ const authCommand = new Command ( 'auth' )
24
+ . description ( 'Manage CLI authentication (Mock Mode)' ) ;
25
+
26
+ authCommand
27
+ . command ( 'login' )
28
+ . description ( 'Authenticate with Zero Finance (Mock)' )
29
+ . action ( async ( ) => {
30
+ console . log ( chalk . cyan ( '🔐 Zero Finance CLI Authentication (Test Mode)\n' ) ) ;
31
+
32
+ // Mock auth flow - no real browser needed
33
+ const answers = await inquirer . prompt ( [
34
+ {
35
+ type : 'input' ,
36
+ name : 'email' ,
37
+ message : 'Enter your email:' ,
38
+ validate : ( input ) => input . includes ( '@' ) || 'Please enter a valid email' ,
39
+
40
+ } ,
41
+ {
42
+ type : 'password' ,
43
+ name : 'password' ,
44
+ message : 'Enter password (any value for test):' ,
45
+ mask : '*' ,
46
+ default : 'test123'
47
+ }
48
+ ] ) ;
49
+
50
+ const spinner = ora ( 'Authenticating...' ) . start ( ) ;
51
+
52
+ // Simulate auth delay
53
+ await new Promise ( resolve => setTimeout ( resolve , 1500 ) ) ;
54
+
55
+ // Generate mock token
56
+ const mockToken = 'zf_test_' + crypto . randomBytes ( 32 ) . toString ( 'hex' ) ;
57
+
58
+ // Store auth data
59
+ config . set ( 'auth.token' , mockToken ) ;
60
+ config . set ( 'auth.email' , answers . email ) ;
61
+ const expiresAt = new Date ( ) ;
62
+ expiresAt . setDate ( expiresAt . getDate ( ) + 90 ) ;
63
+ config . set ( 'auth.expiresAt' , expiresAt . toISOString ( ) ) ;
64
+
65
+ spinner . succeed ( 'Authentication successful!' ) ;
66
+
67
+ console . log ( boxen (
68
+ chalk . green ( `
69
+ ✓ Authenticated as ${ chalk . bold ( answers . email ) }
70
+
71
+ Your CLI token (for testing):
72
+ ${ chalk . dim ( mockToken . substring ( 0 , 20 ) + '...' ) }
73
+
74
+ Token expires: ${ expiresAt . toLocaleDateString ( ) }
75
+ ` ) ,
76
+ {
77
+ padding : 1 ,
78
+ borderStyle : 'round' ,
79
+ borderColor : 'green'
80
+ }
81
+ ) ) ;
82
+ } ) ;
83
+
84
+ authCommand
85
+ . command ( 'logout' )
86
+ . description ( 'Log out from Zero Finance CLI' )
87
+ . action ( ( ) => {
88
+ const email = config . get ( 'auth.email' ) ;
89
+ config . delete ( 'auth.token' ) ;
90
+ config . delete ( 'auth.expiresAt' ) ;
91
+ config . delete ( 'auth.email' ) ;
92
+
93
+ if ( email ) {
94
+ console . log ( chalk . green ( `✓ Logged out from ${ email } ` ) ) ;
95
+ } else {
96
+ console . log ( chalk . green ( '✓ Logged out successfully' ) ) ;
97
+ }
98
+ } ) ;
99
+
100
+ authCommand
101
+ . command ( 'status' )
102
+ . description ( 'Check authentication status' )
103
+ . action ( ( ) => {
104
+ const token = config . get ( 'auth.token' ) ;
105
+ const expiresAt = config . get ( 'auth.expiresAt' ) ;
106
+ const email = config . get ( 'auth.email' ) ;
107
+
108
+ if ( ! token ) {
109
+ console . log ( chalk . yellow ( '✗ Not authenticated' ) ) ;
110
+ console . log ( chalk . dim ( 'Run "zero auth login" to authenticate' ) ) ;
111
+ return ;
112
+ }
113
+
114
+ // Check if token is expired
115
+ if ( expiresAt && new Date ( expiresAt ) < new Date ( ) ) {
116
+ console . log ( chalk . yellow ( '✗ Token expired' ) ) ;
117
+ console . log ( chalk . dim ( 'Run "zero auth login" to re-authenticate' ) ) ;
118
+ return ;
119
+ }
120
+
121
+ console . log ( chalk . green ( '✓ Authenticated' ) ) ;
122
+ if ( email ) {
123
+ console . log ( chalk . dim ( ` Email: ${ email } ` ) ) ;
124
+ }
125
+ console . log ( chalk . dim ( ` Token: ${ token . substring ( 0 , 20 ) } ...` ) ) ;
126
+
127
+ if ( expiresAt ) {
128
+ const expires = new Date ( expiresAt ) ;
129
+ const daysLeft = Math . ceil ( ( expires - new Date ( ) ) / ( 1000 * 60 * 60 * 24 ) ) ;
130
+ console . log ( chalk . dim ( ` Expires in ${ daysLeft } days` ) ) ;
131
+ }
132
+ } ) ;
133
+
134
+ authCommand
135
+ . command ( 'token' )
136
+ . description ( 'Display current token (test mode only)' )
137
+ . action ( ( ) => {
138
+ const token = config . get ( 'auth.token' ) ;
139
+
140
+ if ( ! token ) {
141
+ console . log ( chalk . yellow ( 'No token found. Please login first.' ) ) ;
142
+ return ;
143
+ }
144
+
145
+ console . log ( chalk . cyan ( 'Current Token (Test Mode):' ) ) ;
146
+ console . log ( token ) ;
147
+ } ) ;
148
+
149
+ export default authCommand ;
0 commit comments