Update iframe modal for auto signin

This commit is contained in:
Shreerang Kale 2025-02-10 10:59:10 +05:30
parent b5b52df06f
commit bf2123cfbc

View File

@ -1,15 +1,12 @@
import { useCallback, useEffect } from 'react';
import { Box, Modal } from '@mui/material';
import {
BASE_URL,
VITE_WALLET_IFRAME_URL,
} from 'utils/constants';
import { generateNonce, SiweMessage } from 'siwe';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import { Box, Modal } from '@mui/material';
import { BASE_URL, VITE_WALLET_IFRAME_URL } from 'utils/constants';
const axiosInstance = axios.create({
baseURL: BASE_URL,
headers: {
@ -19,23 +16,12 @@ const axiosInstance = axios.create({
withCredentials: true,
});
const AutoSignInIFrameModal = (
// {
// setAccounts,
// setIsDataReceived,
// isVisible,
// }: {
// setAccounts: (accounts: string[]) => void;
// setIsDataReceived: (isReceived: boolean) => void;
// isVisible: boolean;
// }
) => {
const AutoSignInIFrameModal = () => {
const navigate = useNavigate();
useEffect(() => {
const handleMessage = async (event: MessageEvent) => {
if (event.origin !== VITE_WALLET_IFRAME_URL) return;
// setIsDataReceived(true);
if (event.data.type === 'SIGN_IN_RESPONSE') {
try {
const { success } = (
@ -46,13 +32,12 @@ const AutoSignInIFrameModal = (
).data;
if (success) {
navigate("/")
navigate('/');
}
return success;
} catch (error) {
return false;
}
// setAccounts(event.data.data);
} else if (event.data.type === 'ERROR') {
console.error('Error from wallet:', event.data.message);
}
@ -70,7 +55,9 @@ const AutoSignInIFrameModal = (
if (event.origin !== VITE_WALLET_IFRAME_URL) return;
if (event.data.type === 'ACCOUNT_ADDRESS_RESPONSE') {
const iframe = document.getElementById('autoSignInFrame') as HTMLIFrameElement;
const iframe = document.getElementById(
'autoSignInFrame',
) as HTMLIFrameElement;
if (!iframe.contentWindow) {
console.error('Iframe not found or not loaded');
@ -86,7 +73,7 @@ const AutoSignInIFrameModal = (
nonce: generateNonce(),
// Human-readable ASCII assertion that the user will sign, and it must not contain `\n`.
statement: 'Sign in With Ethereum.',
}).prepareMessage()
}).prepareMessage();
iframe.contentWindow.postMessage(
{
@ -96,8 +83,6 @@ const AutoSignInIFrameModal = (
},
VITE_WALLET_IFRAME_URL,
);
// setAccounts(event.data.data);
} else if (event.data.type === 'ERROR') {
console.error('Error from wallet:', event.data.message);
}
@ -110,9 +95,10 @@ const AutoSignInIFrameModal = (
};
}, []);
// /////////////////////////////////////////////////
const getAddressFromWallet = useCallback(() => {
const iframe = document.getElementById('autoSignInFrame') as HTMLIFrameElement;
const iframe = document.getElementById(
'autoSignInFrame',
) as HTMLIFrameElement;
if (!iframe.contentWindow) {
console.error('Iframe not found or not loaded');