@web3auth/modal
Version:
Multi chain wallet aggregator for web3Auth
65 lines (62 loc) • 1.8 kB
JavaScript
import { WalletInitializationError } from '@web3auth/no-modal';
import { ref, watch } from 'vue';
import { useWeb3AuthInner } from './useWeb3AuthInner.js';
const useWeb3AuthConnect = () => {
const {
web3Auth,
isConnected
} = useWeb3AuthInner();
const loading = ref(false);
const error = ref(null);
const connectorName = ref(null);
watch(isConnected, newVal => {
if (!newVal && connectorName.value) {
connectorName.value = null;
}
if (newVal && !connectorName.value) {
var _web3Auth$value;
connectorName.value = (_web3Auth$value = web3Auth.value) === null || _web3Auth$value === void 0 ? void 0 : _web3Auth$value.connectedConnectorName;
}
}, {
immediate: true
});
const connect = async () => {
try {
if (!web3Auth.value) throw WalletInitializationError.notReady();
error.value = null;
loading.value = true;
const localProvider = await web3Auth.value.connect();
connectorName.value = web3Auth.value.connectedConnectorName;
return localProvider;
} catch (err) {
error.value = err;
return null;
} finally {
loading.value = false;
}
};
const connectTo = async (connectorType, loginParams) => {
try {
if (!web3Auth.value) throw WalletInitializationError.notReady();
error.value = null;
loading.value = true;
const localProvider = await web3Auth.value.connectTo(connectorType, loginParams);
connectorName.value = web3Auth.value.connectedConnectorName;
return localProvider;
} catch (err) {
error.value = err;
return null;
} finally {
loading.value = false;
}
};
return {
isConnected,
loading,
error,
connectorName,
connect,
connectTo
};
};
export { useWeb3AuthConnect };