-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'web3authcoresdk' into seedless-manual-sync
- Loading branch information
Showing
23 changed files
with
542 additions
and
285 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { MPCWalletState } from '@/hooks/wallets/mpc/useMPCWallet' | ||
import { Box, Button, SvgIcon, Typography } from '@mui/material' | ||
import { useContext, useEffect, useState } from 'react' | ||
import { MpcWalletContext } from './MPCWalletProvider' | ||
import { PasswordRecovery } from './PasswordRecovery' | ||
import GoogleLogo from '@/public/images/welcome/logo-google.svg' | ||
|
||
import css from './styles.module.css' | ||
import useWallet from '@/hooks/wallets/useWallet' | ||
import { ONBOARD_MPC_MODULE_LABEL } from '@/services/mpc/module' | ||
|
||
const MPCLogin = ({ onLogin }: { onLogin?: () => void }) => { | ||
const { loginPending, triggerLogin, userInfo, walletState, recoverFactorWithPassword } = useContext(MpcWalletContext) | ||
|
||
const wallet = useWallet() | ||
|
||
const [loginTriggered, setLoginTriggered] = useState(false) | ||
|
||
const login = async () => { | ||
setLoginTriggered(true) | ||
await triggerLogin() | ||
} | ||
|
||
// If login was triggered through the Button we immediately continue if logged in | ||
useEffect(() => { | ||
if (loginTriggered && wallet && wallet.label === ONBOARD_MPC_MODULE_LABEL && onLogin) { | ||
onLogin() | ||
} | ||
}, [loginTriggered, onLogin, wallet]) | ||
|
||
return ( | ||
<> | ||
{wallet && userInfo ? ( | ||
<> | ||
<Button | ||
variant="outlined" | ||
sx={{ padding: '1 2' }} | ||
onClick={onLogin} | ||
size="small" | ||
disabled={loginPending} | ||
fullWidth | ||
> | ||
<Box | ||
width="100%" | ||
justifyContent="space-between" | ||
display="flex" | ||
flexDirection="row" | ||
alignItems="center" | ||
gap={1} | ||
> | ||
<img | ||
src={userInfo.profileImage} | ||
className={css.profileImg} | ||
alt="Profile Image" | ||
referrerPolicy="no-referrer" | ||
/> | ||
<div className={css.profileData}> | ||
<Typography variant="subtitle2" fontWeight={700}> | ||
Continue as {userInfo.name} | ||
</Typography> | ||
<Typography variant="body2">{userInfo.email}</Typography> | ||
</div> | ||
<SvgIcon component={GoogleLogo} inheritViewBox fontSize="medium" /> | ||
</Box> | ||
</Button> | ||
</> | ||
) : ( | ||
<Button variant="outlined" onClick={login} size="small" disabled={loginPending} fullWidth> | ||
<Box display="flex" flexDirection="row" alignItems="center" gap={1}> | ||
<SvgIcon component={GoogleLogo} inheritViewBox fontSize="medium" /> Continue with Google | ||
</Box> | ||
</Button> | ||
)} | ||
|
||
{walletState === MPCWalletState.MANUAL_RECOVERY && ( | ||
<PasswordRecovery recoverFactorWithPassword={recoverFactorWithPassword} /> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default MPCLogin |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/components/common/ConnectWallet/__tests__/MPCLogin.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { act, render, waitFor } from '@/tests/test-utils' | ||
import * as useWallet from '@/hooks/wallets/useWallet' | ||
import * as useMPCWallet from '@/hooks/wallets/mpc/useMPCWallet' | ||
import MPCLogin from '../MPCLogin' | ||
import { hexZeroPad } from '@ethersproject/bytes' | ||
import { type EIP1193Provider } from '@web3-onboard/common' | ||
import { ONBOARD_MPC_MODULE_LABEL } from '@/services/mpc/module' | ||
import { MpcWalletProvider } from '../MPCWalletProvider' | ||
|
||
describe('MPCLogin', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should render continue with connected account', async () => { | ||
const mockOnLogin = jest.fn() | ||
const walletAddress = hexZeroPad('0x1', 20) | ||
jest.spyOn(useWallet, 'default').mockReturnValue({ | ||
address: walletAddress, | ||
chainId: '5', | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
provider: {} as unknown as EIP1193Provider, | ||
}) | ||
jest.spyOn(useMPCWallet, 'useMPCWallet').mockReturnValue({ | ||
userInfo: { | ||
email: '[email protected]', | ||
name: 'Test Testermann', | ||
profileImage: 'test.png', | ||
}, | ||
triggerLogin: jest.fn(), | ||
walletState: useMPCWallet.MPCWalletState.READY, | ||
} as unknown as useMPCWallet.MPCWalletHook) | ||
|
||
const result = render( | ||
<MpcWalletProvider> | ||
<MPCLogin onLogin={mockOnLogin} /> | ||
</MpcWalletProvider>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(result.findByText('Continue as Test Testermann')).resolves.toBeDefined() | ||
}) | ||
|
||
// We do not automatically invoke the callback as the user did not actively connect | ||
expect(mockOnLogin).not.toHaveBeenCalled() | ||
|
||
const button = await result.findByRole('button') | ||
button.click() | ||
|
||
expect(mockOnLogin).toHaveBeenCalled() | ||
}) | ||
|
||
it('should render google login button and invoke the callback on connection if no wallet is connected', async () => { | ||
const mockOnLogin = jest.fn() | ||
const walletAddress = hexZeroPad('0x1', 20) | ||
const mockUseWallet = jest.spyOn(useWallet, 'default').mockReturnValue(null) | ||
const mockTriggerLogin = jest.fn() | ||
const mockUseMPCWallet = jest.spyOn(useMPCWallet, 'useMPCWallet').mockReturnValue({ | ||
userInfo: { | ||
email: undefined, | ||
name: undefined, | ||
profileImage: undefined, | ||
}, | ||
triggerLogin: mockTriggerLogin, | ||
walletState: useMPCWallet.MPCWalletState.NOT_INITIALIZED, | ||
} as unknown as useMPCWallet.MPCWalletHook) | ||
|
||
const result = render( | ||
<MpcWalletProvider> | ||
<MPCLogin onLogin={mockOnLogin} /> | ||
</MpcWalletProvider>, | ||
) | ||
|
||
await waitFor(() => { | ||
expect(result.findByText('Continue with Google')).resolves.toBeDefined() | ||
}) | ||
|
||
// We do not automatically invoke the callback as the user did not actively connect | ||
expect(mockOnLogin).not.toHaveBeenCalled() | ||
|
||
await act(async () => { | ||
// Click the button and mock a successful login | ||
const button = await result.findByRole('button') | ||
button.click() | ||
mockUseMPCWallet.mockReset().mockReturnValue({ | ||
userInfo: { | ||
email: '[email protected]', | ||
name: 'Test Testermann', | ||
profileImage: 'test.png', | ||
}, | ||
triggerLogin: jest.fn(), | ||
walletState: useMPCWallet.MPCWalletState.READY, | ||
} as unknown as useMPCWallet.MPCWalletHook) | ||
|
||
mockUseWallet.mockReset().mockReturnValue({ | ||
address: walletAddress, | ||
chainId: '5', | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
provider: {} as unknown as EIP1193Provider, | ||
}) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(mockOnLogin).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.