@@ -92,15 +92,9 @@ export async function restoreCredsFromBackupIfNeeded(authDir: string): Promise<b
9292 return false ;
9393 }
9494
95- const backupRaw = readCredsJsonRaw ( backupPath ) ;
96- if ( ! backupRaw || ! isValidJson ( backupRaw ) ) {
95+ if ( ! ( await restoreWebCredsFromBackupRaw ( { credsPath, backupPath } ) ) ) {
9796 return false ;
9897 }
99- await writeWebCredsRawAtomically ( {
100- filePath : credsPath ,
101- content : backupRaw ,
102- tempPrefix : ".creds.restore" ,
103- } ) ;
10498 logger . warn ( { credsPath } , "restored corrupted WhatsApp creds.json from backup" ) ;
10599 return true ;
106100 } catch {
@@ -109,6 +103,25 @@ export async function restoreCredsFromBackupIfNeeded(authDir: string): Promise<b
109103 return false ;
110104}
111105
106+ async function restoreWebCredsFromBackupRaw ( params : {
107+ backupPath : string ;
108+ credsPath : string ;
109+ } ) : Promise < boolean > {
110+ const backupRaw = readCredsJsonRaw ( params . backupPath ) ;
111+ if ( ! backupRaw ) {
112+ return false ;
113+ }
114+
115+ // Ensure backup is parseable before restoring.
116+ JSON . parse ( backupRaw ) ;
117+ await writeWebCredsRawAtomically ( {
118+ filePath : params . credsPath ,
119+ content : backupRaw ,
120+ tempPrefix : ".creds.restore" ,
121+ } ) ;
122+ return true ;
123+ }
124+
112125async function clearOwnedWebAuthDir ( params : {
113126 authDir : string ;
114127 isLegacyAuthDir : boolean ;
@@ -178,6 +191,20 @@ export async function clearStalePhoneCodePairingAuthIfNeeded(params: {
178191 return false ;
179192 }
180193
194+ if (
195+ await restoreWebCredsFromBackupRaw ( {
196+ credsPath : resolveWebCredsPath ( resolvedAuthDir ) ,
197+ backupPath : resolveWebCredsBackupPath ( resolvedAuthDir ) ,
198+ } ) . catch ( ( ) => false )
199+ ) {
200+ runtime . log (
201+ info (
202+ "Restored WhatsApp Web credentials from backup instead of clearing partial phone-code auth." ,
203+ ) ,
204+ ) ;
205+ return true ;
206+ }
207+
181208 const cleared = await clearOwnedWebAuthDir ( {
182209 authDir : resolvedAuthDir ,
183210 isLegacyAuthDir : Boolean ( params . isLegacyAuthDir ) ,
0 commit comments