Commit ba2000af authored by tom's avatar tom

refetch csrf token after login and fix connect wallet from contract page

parent 8ef67799
...@@ -10,7 +10,7 @@ import useFetch from 'lib/hooks/useFetch'; ...@@ -10,7 +10,7 @@ import useFetch from 'lib/hooks/useFetch';
export default function useGetCsrfToken() { export default function useGetCsrfToken() {
const nodeApiFetch = useFetch(); const nodeApiFetch = useFetch();
useQuery({ return useQuery({
queryKey: getResourceKey('csrf'), queryKey: getResourceKey('csrf'),
queryFn: async() => { queryFn: async() => {
if (!isNeedProxy()) { if (!isNeedProxy()) {
......
...@@ -5,15 +5,29 @@ import config from 'configs/app'; ...@@ -5,15 +5,29 @@ import config from 'configs/app';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import useWeb3Wallet from 'lib/web3/useWallet'; import useWeb3Wallet from 'lib/web3/useWallet';
import AddressEntity from 'ui/shared/entities/address/AddressEntity'; import AddressEntity from 'ui/shared/entities/address/AddressEntity';
import useIsAuth from 'ui/snippets/auth/useIsAuth';
import useSignInWithWallet from 'ui/snippets/auth/useSignInWithWallet';
interface Props { interface Props {
isLoading?: boolean; isLoading?: boolean;
} }
const ContractConnectWallet = ({ isLoading }: Props) => { const ContractConnectWallet = ({ isLoading }: Props) => {
const signInWithWallet = useSignInWithWallet({ source: 'Smart contracts' });
const isAuth = useIsAuth();
const web3Wallet = useWeb3Wallet({ source: 'Smart contracts' }); const web3Wallet = useWeb3Wallet({ source: 'Smart contracts' });
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const shouldSignIn = config.features.account.isEnabled && !isAuth;
const handleConnectClick = React.useCallback(() => {
if (shouldSignIn) {
signInWithWallet.start();
} else {
web3Wallet.connect();
}
}, [ signInWithWallet, web3Wallet, shouldSignIn ]);
const content = (() => { const content = (() => {
if (!web3Wallet.isConnected) { if (!web3Wallet.isConnected) {
return ( return (
...@@ -21,13 +35,13 @@ const ContractConnectWallet = ({ isLoading }: Props) => { ...@@ -21,13 +35,13 @@ const ContractConnectWallet = ({ isLoading }: Props) => {
<span>Disconnected</span> <span>Disconnected</span>
<Button <Button
ml={ 3 } ml={ 3 }
onClick={ web3Wallet.connect } onClick={ handleConnectClick }
size="sm" size="sm"
variant="outline" variant="outline"
isLoading={ web3Wallet.isOpen } isLoading={ shouldSignIn ? signInWithWallet.isPending : web3Wallet.isOpen }
loadingText="Connect wallet" loadingText="Connect wallet"
> >
Connect wallet Connect wallet
</Button> </Button>
</> </>
); );
......
...@@ -4,6 +4,7 @@ import React from 'react'; ...@@ -4,6 +4,7 @@ import React from 'react';
import type { Screen, ScreenSuccess } from './types'; import type { Screen, ScreenSuccess } from './types';
import useGetCsrfToken from 'lib/hooks/useGetCsrfToken';
import * as mixpanel from 'lib/mixpanel'; import * as mixpanel from 'lib/mixpanel';
import IconSvg from 'ui/shared/IconSvg'; import IconSvg from 'ui/shared/IconSvg';
...@@ -34,6 +35,7 @@ const AuthModal = ({ initialScreen, onClose, mixpanelConfig }: Props) => { ...@@ -34,6 +35,7 @@ const AuthModal = ({ initialScreen, onClose, mixpanelConfig }: Props) => {
const router = useRouter(); const router = useRouter();
const profileQuery = useProfileQuery(); const profileQuery = useProfileQuery();
const csrfQuery = useGetCsrfToken();
React.useEffect(() => { React.useEffect(() => {
if ('isAuth' in initialScreen && initialScreen.isAuth) { if ('isAuth' in initialScreen && initialScreen.isAuth) {
...@@ -79,11 +81,12 @@ const AuthModal = ({ initialScreen, onClose, mixpanelConfig }: Props) => { ...@@ -79,11 +81,12 @@ const AuthModal = ({ initialScreen, onClose, mixpanelConfig }: Props) => {
} }
const { data } = await profileQuery.refetch(); const { data } = await profileQuery.refetch();
await csrfQuery.refetch();
if (data) { if (data) {
onNextStep({ ...screen, profile: data }); onNextStep({ ...screen, profile: data });
} }
// TODO @tom2drum handle error case // TODO @tom2drum handle error case
}, [ initialScreen, mixpanelConfig?.account_link_info.source, onNextStep, profileQuery ]); }, [ initialScreen, mixpanelConfig?.account_link_info.source, onNextStep, profileQuery, csrfQuery ]);
const onModalClose = React.useCallback(() => { const onModalClose = React.useCallback(() => {
onClose(isSuccess); onClose(isSuccess);
......
...@@ -14,7 +14,7 @@ interface Props { ...@@ -14,7 +14,7 @@ interface Props {
buttonVariant?: ButtonProps['variant']; buttonVariant?: ButtonProps['variant'];
} }
const WalletDesktop = ({ buttonSize, buttonVariant = 'header' }: Props) => { const UserWalletDesktop = ({ buttonSize, buttonVariant = 'header' }: Props) => {
const walletMenu = useDisclosure(); const walletMenu = useDisclosure();
const web3Wallet = useWeb3Wallet({ source: 'Header' }); const web3Wallet = useWeb3Wallet({ source: 'Header' });
...@@ -65,4 +65,4 @@ const WalletDesktop = ({ buttonSize, buttonVariant = 'header' }: Props) => { ...@@ -65,4 +65,4 @@ const WalletDesktop = ({ buttonSize, buttonVariant = 'header' }: Props) => {
); );
}; };
export default React.memo(WalletDesktop); export default React.memo(UserWalletDesktop);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment