diff --git a/lib/hooks/login-hooks.js b/lib/hooks/login-hooks.js --- a/lib/hooks/login-hooks.js +++ b/lib/hooks/login-hooks.js @@ -32,48 +32,20 @@ const inactiveStep = { step: 'inactive' }; -type LogInInputs = - | { - +accountType: 'username', - +username: string, - +password: string, - } - | { - +accountType: 'ethereum', - +walletAddress: string, - +siweMessage: string, - +siweSignature: string, - }; - -function useLogIn(): LogInInputs => Promise { +function useLogIn(): (identityAuthPromise: Promise) => Promise { const [currentStep, setCurrentStep] = React.useState(inactiveStep); - const identityPasswordLogIn = useIdentityPasswordLogIn(); - const identityWalletLogIn = useIdentityWalletLogIn(); - const dispatchActionPromise = useDispatchActionPromise(); const returnedFunc = React.useCallback( - (logInInputs: LogInInputs) => + (promise: Promise) => new Promise( // eslint-disable-next-line no-async-promise-executor async (resolve, reject) => { if (currentStep.step !== 'inactive') { return; } - const action = - logInInputs.accountType === 'username' - ? identityPasswordLogIn( - logInInputs.username, - logInInputs.password, - ) - : identityWalletLogIn( - logInInputs.walletAddress, - logInInputs.siweMessage, - logInInputs.siweSignature, - ); - void dispatchActionPromise(identityLogInActionTypes, action); try { - await action; + await promise; setCurrentStep({ step: 'identity_login_dispatched', resolve, @@ -84,12 +56,7 @@ } }, ), - [ - currentStep, - dispatchActionPromise, - identityPasswordLogIn, - identityWalletLogIn, - ], + [currentStep], ); const keyserverAuth = useKeyserverAuthWithRetry(authoritativeKeyserverID()); @@ -107,6 +74,7 @@ const registeringOnAuthoritativeKeyserverRef = React.useRef(false); const dispatch = useDispatch(); + const dispatchActionPromise = useDispatchActionPromise(); React.useEffect(() => { if ( !isRegisteredOnIdentity || @@ -159,15 +127,22 @@ username: string, password: string, ) => Promise { + const identityPasswordLogIn = useIdentityPasswordLogIn(); + const dispatchActionPromise = useDispatchActionPromise(); + const identityPasswordAuth = React.useCallback( + (username: string, password: string) => { + const promise = identityPasswordLogIn(username, password); + void dispatchActionPromise(identityLogInActionTypes, promise); + return promise; + }, + [identityPasswordLogIn, dispatchActionPromise], + ); + const logIn = useLogIn(); return React.useCallback( (username: string, password: string) => - logIn({ - accountType: 'username', - username, - password, - }), - [logIn], + logIn(identityPasswordAuth(username, password)), + [logIn, identityPasswordAuth], ); } @@ -176,16 +151,26 @@ siweMessage: string, siweSignature: string, ) => Promise { - const logIn = useLogIn(); - return React.useCallback( - (walletAddress: string, siweMessage: string, siweSignature: string) => - logIn({ - accountType: 'ethereum', + const identityWalletLogIn = useIdentityWalletLogIn(); + const dispatchActionPromise = useDispatchActionPromise(); + const identityWalletAuth = React.useCallback( + (walletAddress: string, siweMessage: string, siweSignature: string) => { + const promise = identityWalletLogIn( walletAddress, siweMessage, siweSignature, - }), - [logIn], + ); + void dispatchActionPromise(identityLogInActionTypes, promise); + return promise; + }, + [identityWalletLogIn, dispatchActionPromise], + ); + + const logIn = useLogIn(); + return React.useCallback( + (walletAddress: string, siweMessage: string, siweSignature: string) => + logIn(identityWalletAuth(walletAddress, siweMessage, siweSignature)), + [logIn, identityWalletAuth], ); }