diff --git a/lib/actions/user-actions.js b/lib/actions/user-actions.js --- a/lib/actions/user-actions.js +++ b/lib/actions/user-actions.js @@ -67,6 +67,7 @@ } from '../types/user-types.js'; import { authoritativeKeyserverID } from '../utils/authoritative-keyserver.js'; import { getConfig } from '../utils/config.js'; +import { getMessageForException } from '../utils/errors.js'; import { useSelector } from '../utils/redux-utils.js'; import { usingCommServicesAccessToken } from '../utils/services-utils.js'; import sleep from '../utils/sleep.js'; @@ -644,6 +645,8 @@ const client = React.useContext(IdentityClientContext); const identityClient = client?.identityClient; const preRequestUserState = useSelector(state => state.currentUserInfo); + const callClaimUsername = useClaimUsername(); + const registerReservedPasswordUser = useIdentityPasswordRegisterReserved(); return React.useCallback( (username, password) => { @@ -651,17 +654,38 @@ throw new Error('Identity service client is not initialized'); } return (async () => { - const result = await identityClient.logInPasswordUser( - username, - password, - ); + let result; + try { + result = await identityClient.logInPasswordUser(username, password); + } catch (e) { + if ( + identityClient.registerReservedPasswordUser && + getMessageForException(e) === + 'need keyserver message to claim username' + ) { + const { message, signature } = await callClaimUsername(); + result = await registerReservedPasswordUser( + username, + password, + message, + signature, + ); + } else { + throw e; + } + } return { ...result, preRequestUserState, }; })(); }, - [identityClient, preRequestUserState], + [ + identityClient, + preRequestUserState, + callClaimUsername, + registerReservedPasswordUser, + ], ); } function useIdentityWalletLogIn(): ( @@ -671,8 +695,43 @@ ) => Promise { const client = React.useContext(IdentityClientContext); const identityClient = client?.identityClient; + const callClaimUsername = useClaimUsername(); + const registerReservedWalletUser = useIdentityWalletRegisterReserved(); + invariant(identityClient, 'Identity client should be set'); - return identityClient.logInWalletUser; + return React.useCallback( + (walletAddress, siweMessage, siweSignature) => { + if (!identityClient) { + throw new Error('Identity service client is not initialized'); + } + return (async () => { + try { + return await identityClient.logInWalletUser( + walletAddress, + siweMessage, + siweSignature, + ); + } catch (e) { + if ( + identityClient.registerReservedWalletUser && + getMessageForException(e) === + 'need keyserver message to claim username' + ) { + const { message, signature } = await callClaimUsername(); + return await registerReservedWalletUser( + walletAddress, + siweMessage, + siweSignature, + message, + signature, + ); + } + throw e; + } + })(); + }, + [identityClient, callClaimUsername, registerReservedWalletUser], + ); } const legacyLogInActionTypes = Object.freeze({