@pushchain/ui-kit
Version:
Push Chain is a true universal L1 that is 100% EVM compatible. It allows developers to deploy once and make their apps instantly compatible with users from all other L1s (Ethereum, Solana, etc) with zero on-chain code change.
68 lines • 2.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWaapAuth = void 0;
const react_1 = require("react");
const waapProvider_1 = require("./waapProvider");
const types_1 = require("../../../lib/types");
const getWaap = () => window.waap;
const useWaapAuth = () => {
const [address, setAddress] = (0, react_1.useState)(null);
const [loginType, setLoginType] = (0, react_1.useState)(null);
const [error, setError] = (0, react_1.useState)(null);
const ensureConnected = (0, react_1.useCallback)(async (opts) => {
try {
setError(null);
const waap = getWaap();
if (!waap)
throw new Error('WaaP not initialized (window.waap missing)');
let accounts = await waap.request({ method: 'eth_accounts' });
let addr = accounts?.[0];
if (!addr && opts?.interactive) {
const lt = await waap.login();
accounts = await waap.request({ method: 'eth_requestAccounts' });
addr = accounts?.[0];
setLoginType(lt);
}
if (!addr) {
return null;
}
await (0, waapProvider_1.switchNetwork)(types_1.ChainType.PUSH_WALLET);
setAddress(addr);
return { address: addr, loginType };
}
catch (e) {
console.error('[WaaP] ensureConnected failed', e);
setError(e);
return null;
}
}, [loginType]);
const loginWithWaapSocial = (0, react_1.useCallback)(async () => {
return ensureConnected({ interactive: true });
}, [ensureConnected]);
const logoutWaap = (0, react_1.useCallback)(async () => {
try {
const waap = getWaap();
if (waap) {
await waap.logout();
}
}
finally {
setAddress(null);
setLoginType(null);
}
}, []);
const tryAutoConnect = (0, react_1.useCallback)(async () => {
const res = await ensureConnected({ interactive: false });
return res;
}, [ensureConnected]);
return {
address,
loginType,
error,
loginWithWaapSocial,
logoutWaap,
tryAutoConnect,
};
};
exports.useWaapAuth = useWaapAuth;
//# sourceMappingURL=useWaapAuth.js.map