@@ -90,6 +90,22 @@ async function handleOTPLogin({ otp_autosend }: { otp_autosend: OTPType }, { ema
9090 return { ...loginResult , otp_type : otp_autosend , pin_code : pinCode . value } ;
9191}
9292
93+ function _parseOTPError ( error : unknown ) : { otp_enabled ?: boolean ; otp_autosend : OTPType } | undefined {
94+ if ( error && typeof error === "object" && "otp_enabled" in error ) {
95+ return error as { otp_enabled ?: boolean ; otp_autosend : OTPType } ;
96+ }
97+
98+ if ( typeof error === "string" ) {
99+ try {
100+ return JSON . parse ( error ) ;
101+ } catch {
102+ return undefined ;
103+ }
104+ }
105+
106+ return undefined ;
107+ }
108+
93109/**
94110 * Logs in a user with email and password.
95111 * @param email The user's email.
@@ -101,14 +117,10 @@ async function loginWithEmailPassword(email: string, password: string) {
101117 // @ts -expect-error ts don't know what kind of otp_enabled we are using
102118 const loginResult = await Account . login ( { email, password } ) ;
103119 return loginResult ;
104- } catch ( error ) {
105- try {
106- const errorJSON = JSON . parse ( String ( error ) ) ;
107- if ( errorJSON ?. otp_enabled ) {
108- return handleOTPLogin ( errorJSON , { email, password } ) ;
109- }
110- } catch {
111- // Ignore JSON parsing errors
120+ } catch ( error : unknown ) {
121+ const otpPayload = _parseOTPError ( error ) ;
122+ if ( otpPayload ?. otp_enabled ) {
123+ return handleOTPLogin ( otpPayload , { email, password } ) ;
112124 }
113125
114126 errorHandler ( error ) ;
0 commit comments