Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add autoLogin function #121

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading