diff --git a/connectors/injected.ts b/connectors/injected.ts index cfcff26..2055857 100644 --- a/connectors/injected.ts +++ b/connectors/injected.ts @@ -8,17 +8,7 @@ export default class Connector extends LockConnector { try { await window['ethereum'].request({ method: 'eth_requestAccounts' }); } catch (e: any) { - if (e.message.includes('Already processing eth_requestAccounts')) { - try { - await provider.request({ - method: 'wallet_requestPermissions', - params: [{ eth_accounts: {} }] - }); - } catch (e: any) { - if (e.code === 4001 || e.code === -32002) return; - } - } - + console.log(e); if (e.code === 4001 || e.code === -32002) return; } } else if (window['web3']) { @@ -27,6 +17,20 @@ export default class Connector extends LockConnector { return provider; } + async autoConnect() { + let provider; + + if (window['ethereum']) { + provider = window['ethereum']; + } else if (window['web3']) { + provider = window['web3'].currentProvider; + } + + const accounts = await provider.request({ method: 'eth_accounts' }); + + return accounts.length > 0 ? provider : null; + } + async isLoggedIn() { if (!window['ethereum']) return false; if (window['ethereum'].request({ method: 'eth_accounts' })) return true; diff --git a/package.json b/package.json index 5085ae1..cb99e52 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@snapshot-labs/lock", - "version": "0.2.7", + "version": "0.2.8", "repository": "snapshot-labs/lock", "license": "MIT", "main": "dist/lock.cjs.js", diff --git a/plugins/vue3.ts b/plugins/vue3.ts index 894e10d..0ff43f2 100644 --- a/plugins/vue3.ts +++ b/plugins/vue3.ts @@ -32,10 +32,23 @@ export const useLock = ({ ...options }) => { return provider; } + async function autoLogin(connector: string) { + const lockConnector = lockClient.getConnector(connector); + const localProvider = await lockConnector.autoConnect(); + + if (!localProvider) return; + + provider.value = localProvider; + localStorage.setItem(`_${name}.connector`, connector); + isAuthenticated.value = true; + + return provider; + } + async function logout() { const connector = await getConnector(); if (connector) { - const lockConnector = lockClient.getConnector(connector); + const lockConnector = lockClient.getConnector(connector as string); await lockConnector.logout(); localStorage.removeItem(`_${name}.connector`); isAuthenticated.value = false; @@ -43,7 +56,7 @@ export const useLock = ({ ...options }) => { } } - async function getConnector() { + async function getConnector(): Promise { const connector = localStorage.getItem(`_${name}.connector`); if (connector) { const lockConnector = lockClient.getConnector(connector); @@ -59,6 +72,7 @@ export const useLock = ({ ...options }) => { lockClient, login, logout, + autoLogin, getConnector }; diff --git a/src/connector.ts b/src/connector.ts index 8c852b5..783812d 100644 --- a/src/connector.ts +++ b/src/connector.ts @@ -9,6 +9,10 @@ export default class Connector { return; } + async autoConnect(): Promise { + return this.connect(); + } + logout(): any { return true; }