@@ -10,6 +10,8 @@ export interface NpmExecResult {
1010 exitCode : number
1111 /** True if the operation failed due to missing/invalid OTP */
1212 requiresOtp ?: boolean
13+ /** True if the operation failed due to authentication failure (not logged in or token expired) */
14+ authFailure ?: boolean
1315}
1416
1517function detectOtpRequired ( stderr : string ) : boolean {
@@ -18,12 +20,29 @@ function detectOtpRequired(stderr: string): boolean {
1820 'one-time password' ,
1921 'This operation requires a one-time password' ,
2022 '--otp=<code>' ,
21- 'OTP' ,
2223 ]
2324 const lowerStderr = stderr . toLowerCase ( )
2425 return otpPatterns . some ( pattern => lowerStderr . includes ( pattern . toLowerCase ( ) ) )
2526}
2627
28+ function detectAuthFailure ( stderr : string ) : boolean {
29+ const authPatterns = [
30+ 'ENEEDAUTH' ,
31+ 'You must be logged in' ,
32+ 'authentication error' ,
33+ 'Unable to authenticate' ,
34+ 'code E401' ,
35+ 'code E403' ,
36+ '401 Unauthorized' ,
37+ '403 Forbidden' ,
38+ 'not logged in' ,
39+ 'npm login' ,
40+ 'npm adduser' ,
41+ ]
42+ const lowerStderr = stderr . toLowerCase ( )
43+ return authPatterns . some ( pattern => lowerStderr . includes ( pattern . toLowerCase ( ) ) )
44+ }
45+
2746function filterNpmWarnings ( stderr : string ) : string {
2847 return stderr
2948 . split ( '\n' )
@@ -70,11 +89,15 @@ export async function execNpm(
7089 const err = error as { stdout ?: string , stderr ?: string , code ?: number }
7190 const stderr = err . stderr ?. trim ( ) ?? String ( error )
7291 const requiresOtp = detectOtpRequired ( stderr )
92+ const authFailure = detectAuthFailure ( stderr )
7393
7494 if ( ! options . silent ) {
7595 if ( requiresOtp ) {
7696 logError ( 'OTP required' )
7797 }
98+ else if ( authFailure ) {
99+ logError ( 'Authentication required - please run "npm login" and restart the connector' )
100+ }
78101 else {
79102 logError ( filterNpmWarnings ( stderr ) . split ( '\n' ) [ 0 ] || 'Command failed' )
80103 }
@@ -84,9 +107,12 @@ export async function execNpm(
84107 stdout : err . stdout ?. trim ( ) ?? '' ,
85108 stderr : requiresOtp
86109 ? 'This operation requires a one-time password (OTP).'
87- : filterNpmWarnings ( stderr ) ,
110+ : authFailure
111+ ? 'Authentication failed. Please run "npm login" and restart the connector.'
112+ : filterNpmWarnings ( stderr ) ,
88113 exitCode : err . code ?? 1 ,
89114 requiresOtp,
115+ authFailure,
90116 }
91117 }
92118}
0 commit comments