Skip to content

Commit

Permalink
fix: double check before login
Browse files Browse the repository at this point in the history
  • Loading branch information
yqrashawn committed Jun 30, 2021
1 parent 9feb779 commit 80befb1
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/useConfluxPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export default function useConfluxPortal(tokenAddrs = []) {
// NOTE: if portal is installed, there must be window.conflux here
window.conflux
? window.localStorage.getItem("CFXJS_REACT_HOOK_PORTAL_ADDRESS_CACHE") ||
undefined
undefined
: null
);

const {chainId, networkId} = useChainNetId()
const [error,setError]=useState(null)
const { chainId, networkId } = useChainNetId()
const [error, setError] = useState(null)

useEffectOnce(() => {
window?.conflux?.send({ method: "cfx_accounts" }).then((accounts) => {
Expand All @@ -53,19 +53,19 @@ export default function useConfluxPortal(tokenAddrs = []) {
});
});

const login = (fallbackFn) => {
if (!address) {
if (window?.conflux)
return window.conflux
.send('cfx_requestAccounts')
.then(
(addresses) => validAddresses(addresses) && setAddress(addresses[0])
)
.catch(
(error)=>setError(error)
)
return typeof fallbackFn === "function" && fallbackFn();
}
const login = async (fallbackFn) => {
if (!window?.conflux) return
const hasAddr = !!address && (await window.conflux.send('cfx_accounts')).length
if (hasAddr) return
return window.conflux
.send('cfx_requestAccounts')
.then(
(addresses) => validAddresses(addresses) && setAddress(addresses[0])
)
.catch(
(error) => setError(error)
)
return typeof fallbackFn === "function" && fallbackFn();
};

const useEnsurePortalLogin = () => {
Expand Down

0 comments on commit 80befb1

Please sign in to comment.