Skip to content

Commit

Permalink
feat: add autoConnect function
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Oct 3, 2024
1 parent e7a47e5 commit 31283e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
26 changes: 15 additions & 11 deletions connectors/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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;
Expand Down
18 changes: 16 additions & 2 deletions plugins/vue3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,31 @@ 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;
provider.value = null;
}
}

async function getConnector() {
async function getConnector(): Promise<boolean | string> {
const connector = localStorage.getItem(`_${name}.connector`);
if (connector) {
const lockConnector = lockClient.getConnector(connector);
Expand All @@ -59,6 +72,7 @@ export const useLock = ({ ...options }) => {
lockClient,
login,
logout,
autoLogin,
getConnector
};

Expand Down
4 changes: 4 additions & 0 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default class Connector {
return;
}

async autoConnect(): Promise<any> {
return this.connect();
}

logout(): any {
return true;
}
Expand Down

0 comments on commit 31283e1

Please sign in to comment.