Skip to content

Commit

Permalink
feat: add autoLogin function (#121)
Browse files Browse the repository at this point in the history
* feat: add autoConnect function

* v0.2.8

---------

Co-authored-by: Chaitanya <yourchaitu@gmail.com>
  • Loading branch information
wa0x6e and ChaituVR authored Oct 7, 2024
1 parent e7a47e5 commit 6763bc1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
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 6763bc1

Please sign in to comment.