1
- import { cliux , CLIError , handleAndLogError , log } from '@contentstack/cli-utilities' ;
1
+ import { cliux , CLIError , log , cliErrorHandler } from '@contentstack/cli-utilities' ;
2
2
import { User } from '../interfaces' ;
3
3
import { askOTPChannel , askOTP } from './interactive' ;
4
4
@@ -28,8 +28,15 @@ class AuthHandler {
28
28
* TBD: take out the otp implementation from login and create a new method/function to handle otp
29
29
*/
30
30
async login ( email : string , password : string , tfaToken ?: string ) : Promise < User > {
31
- log . debug ( 'Starting login process' , { module : 'auth-handler' , email, hasPassword : ! ! password , hasTfaToken : ! ! tfaToken } ) ;
32
-
31
+ const hasCredentials = ! ! password ;
32
+ const hasTfaToken = ! ! tfaToken ;
33
+ log . debug ( 'Starting login process' , {
34
+ module : 'auth-handler' ,
35
+ email,
36
+ hasCredentials,
37
+ hasTfaToken,
38
+ } ) ;
39
+
33
40
return new Promise ( ( resolve , reject ) => {
34
41
if ( email && password ) {
35
42
const loginPayload : {
@@ -41,22 +48,31 @@ class AuthHandler {
41
48
loginPayload . tfa_token = tfaToken ;
42
49
log . debug ( 'Adding TFA token to login payload' , { module : 'auth-handler' } ) ;
43
50
}
44
-
45
- log . debug ( 'Making login API call' , { module : 'auth-handler' , payload : { email, hasPassword : ! ! password , hasTfaToken : ! ! tfaToken } } ) ;
46
-
51
+
52
+ const hasCredentials = ! ! password ;
53
+ const hasTfaTokenPresent = ! ! tfaToken ;
54
+ log . debug ( 'Making login API call' , {
55
+ module : 'auth-handler' ,
56
+ payload : { email, hasCredentials, hasTfaTokenPresent } ,
57
+ } ) ;
58
+
47
59
this . _client
48
60
. login ( loginPayload )
49
61
. then ( async ( result : any ) => {
50
- log . debug ( 'Login API response received' , { module : 'auth-handler' , hasUser : ! ! result . user , errorCode : result . error_code } ) ;
51
-
62
+ log . debug ( 'Login API response received' , {
63
+ module : 'auth-handler' ,
64
+ hasUser : ! ! result . user ,
65
+ errorCode : result . error_code ,
66
+ } ) ;
67
+
52
68
if ( result . user ) {
53
69
log . debug ( 'Login successful, user found' , { module : 'auth-handler' , userEmail : result . user . email } ) ;
54
70
resolve ( result . user as User ) ;
55
71
} else if ( result . error_code === 294 ) {
56
72
log . debug ( 'TFA required, requesting OTP channel' , { module : 'auth-handler' } ) ;
57
73
const otpChannel = await askOTPChannel ( ) ;
58
74
log . debug ( `OTP channel selected: ${ otpChannel } ` , { module : 'auth-handler' } ) ;
59
-
75
+
60
76
// need to send sms to the mobile
61
77
if ( otpChannel === 'sms' ) {
62
78
log . debug ( 'Sending SMS OTP request' , { module : 'auth-handler' } ) ;
@@ -66,22 +82,23 @@ class AuthHandler {
66
82
cliux . print ( 'CLI_AUTH_LOGIN_SECURITY_CODE_SEND_SUCCESS' ) ;
67
83
} catch ( error ) {
68
84
log . debug ( 'SMS OTP request failed' , { module : 'auth-handler' , error } ) ;
69
- handleAndLogError ( error , { } , 'Failed to send the security code' )
70
- reject ( new CLIError ( { message : 'Failed to login - failed to send the security code' } ) ) ;
85
+ const err = cliErrorHandler . classifyError ( error ) ;
86
+ reject ( err ) ;
71
87
return ;
72
88
}
73
89
}
74
-
90
+
75
91
log . debug ( 'Requesting OTP input from user' , { module : 'auth-handler' } ) ;
76
92
const tfToken = await askOTP ( ) ;
77
93
log . debug ( 'OTP received, retrying login' , { module : 'auth-handler' } ) ;
78
-
94
+
79
95
try {
80
96
resolve ( await this . login ( email , password , tfToken ) ) ;
81
97
} catch ( error ) {
82
98
log . debug ( 'Login with TFA token failed' , { module : 'auth-handler' , error } ) ;
83
- handleAndLogError ( error , { } , 'Failed to login with tfa token' )
84
- reject ( new CLIError ( { message : 'Failed to login with the tf token' } ) ) ;
99
+ const err = cliErrorHandler . classifyError ( error ) ;
100
+ reject ( err ) ;
101
+ return ;
85
102
}
86
103
} else {
87
104
log . debug ( 'Login failed - no user found' , { module : 'auth-handler' , result } ) ;
@@ -90,11 +107,17 @@ class AuthHandler {
90
107
} )
91
108
. catch ( ( error : any ) => {
92
109
log . debug ( 'Login API call failed' , { module : 'auth-handler' , error : error . message || error } ) ;
93
- handleAndLogError ( error , { } , 'Failed to login with the credentials' ) ;
94
- reject ( new CLIError ( { message : error . errorMessage } ) ) ;
110
+ const err = cliErrorHandler . classifyError ( error ) ;
111
+ reject ( err ) ;
95
112
} ) ;
96
113
} else {
97
- log . debug ( 'Login failed - missing credentials' , { module : 'auth-handler' , hasEmail : ! ! email , hasPassword : ! ! password } ) ;
114
+ const hasEmail = ! ! email ;
115
+ const hasCredentials = ! ! password ;
116
+ log . debug ( 'Login failed - missing credentials' , {
117
+ module : 'auth-handler' ,
118
+ hasEmail,
119
+ hasCredentials,
120
+ } ) ;
98
121
reject ( new CLIError ( { message : 'No credential found to login' } ) ) ;
99
122
}
100
123
} ) ;
@@ -107,11 +130,11 @@ class AuthHandler {
107
130
*/
108
131
async logout ( authtoken : string ) : Promise < object > {
109
132
log . debug ( 'Starting logout process' , { module : 'auth-handler' , hasAuthToken : ! ! authtoken } ) ;
110
-
133
+
111
134
return new Promise ( ( resolve , reject ) => {
112
135
if ( authtoken ) {
113
136
log . debug ( 'Making logout API call' , { module : 'auth-handler' } ) ;
114
-
137
+
115
138
this . _client
116
139
. logout ( authtoken )
117
140
. then ( function ( response : object ) {
@@ -120,8 +143,8 @@ class AuthHandler {
120
143
} )
121
144
. catch ( ( error : Error ) => {
122
145
log . debug ( 'Logout API call failed' , { module : 'auth-handler' , error : error . message } ) ;
123
- handleAndLogError ( error , { } , 'Failed to logout' ) ;
124
- return reject ( new CLIError ( { message : 'Failed to logout - ' + error . message } ) ) ;
146
+ const err = cliErrorHandler . classifyError ( error ) ;
147
+ reject ( err ) ;
125
148
} ) ;
126
149
} else {
127
150
log . debug ( 'Logout failed - no auth token provided' , { module : 'auth-handler' } ) ;
@@ -137,11 +160,11 @@ class AuthHandler {
137
160
*/
138
161
async validateAuthtoken ( authtoken : string ) : Promise < object > {
139
162
log . debug ( 'Starting token validation' , { module : 'auth-handler' , hasAuthToken : ! ! authtoken } ) ;
140
-
163
+
141
164
return new Promise ( ( resolve , reject ) => {
142
165
if ( authtoken ) {
143
166
log . debug ( 'Making token validation API call' , { module : 'auth-handler' } ) ;
144
-
167
+
145
168
this . _client
146
169
. getUser ( )
147
170
. then ( ( user : object ) => {
@@ -150,8 +173,8 @@ class AuthHandler {
150
173
} )
151
174
. catch ( ( error : Error ) => {
152
175
log . debug ( 'Token validation failed' , { module : 'auth-handler' , error : error . message } ) ;
153
- handleAndLogError ( error , { } , 'Failed to validate token' ) ;
154
- reject ( new CLIError ( { message : 'Failed to validate token - ' + error . message } ) ) ;
176
+ const err = cliErrorHandler . classifyError ( error ) ;
177
+ reject ( err ) ;
155
178
} ) ;
156
179
} else {
157
180
log . debug ( 'Token validation failed - no auth token provided' , { module : 'auth-handler' } ) ;
0 commit comments