Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #64 from /feature/connect-status-cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Oct 6, 2021
2 parents 2a99e03 + cb05623 commit 08af748
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"ethers": "5.4.6",
"html2canvas": "1.3.2",
"https-browserify": "1.0.0",
"js-cookie": "3.0.1",
"lodash": "4.17.21",
"marked": "3.0.4",
"moment": "2.29.1",
Expand Down Expand Up @@ -70,6 +71,7 @@
"@babel/preset-env": "7.15.6",
"@babel/preset-typescript": "7.15.0",
"@types/chrome": "0.0.150",
"@types/js-cookie": "3.0.0",
"@types/lodash": "4.14.171",
"@types/marked": "3.0.1",
"@vue/compiler-sfc": "3.1.5",
Expand Down
35 changes: 20 additions & 15 deletions src/common/rss3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RSS3Account, RSS3AccountInput } from 'rss3-next/types/rss3';
import axios from 'axios';
import { GitcoinResponse, GeneralAsset, NFTResponse } from './types';
import config from '@/config';
import Cookies from 'js-cookie';

let rss3: RSS3 | null;
let web3: Web3 | null;
Expand Down Expand Up @@ -93,8 +94,8 @@ async function metamaskConnect(skipSign?: boolean) {
web3 = new Web3(metamaskEthereum);

let address: string;
if (localStorage.getItem('lastConnect') === 'metamask' && localStorage.getItem('lastAddress')) {
address = <string>localStorage.getItem('lastAddress');
if (Cookies.get('LAST_CONNECT_METHOD') === 'metamask' && Cookies.get('LAST_CONNECT_ADDRESS')) {
address = Cookies.get('LAST_CONNECT_ADDRESS');
} else {
const accounts = await metamaskEthereum.request({
method: 'eth_requestAccounts',
Expand Down Expand Up @@ -126,31 +127,39 @@ async function disconnect() {
if (provider) {
await provider.disconnect();
}
localStorage.removeItem('lastConnect');
localStorage.removeItem('lastAddress');
Cookies.remove('LAST_CONNECT_METHOD');
Cookies.remove('LAST_CONNECT_ADDRESS');
}

export default {
walletConnect: async () => {
await walletConnect();
if (isValidRSS3()) {
localStorage.setItem('lastConnect', 'walletConnect');
Cookies.set('LAST_CONNECT_METHOD', 'walletConnect');
}
return rss3;
},
metamaskConnect: async () => {
await metamaskConnect();
if (isValidRSS3()) {
localStorage.setItem('lastConnect', 'metamask');
Cookies.set('LAST_CONNECT_METHOD', 'metamask');
}
return rss3;
},
disconnect: async () => {
disconnect();
},
disconnect: disconnect,
reconnect: async (): Promise<boolean> => {
// Migrate
if (localStorage.getItem('lastConnect')) {
Cookies.set('LAST_CONNECT_METHOD', localStorage.getItem('lastConnect'));
localStorage.removeItem('lastConnect');
}
if (localStorage.getItem('lastAddress')) {
Cookies.set('LAST_CONNECT_ADDRESS', localStorage.getItem('lastAddress'));
localStorage.removeItem('lastAddress');
}

if (!isValidRSS3()) {
const lastConnect = localStorage.getItem('lastConnect');
const lastConnect = Cookies.get('LAST_CONNECT_METHOD');
switch (lastConnect) {
case 'walletConnect':
await walletConnect(true);
Expand All @@ -159,11 +168,7 @@ export default {
await metamaskConnect(true);
break;
}
if (isValidRSS3()) {
return true;
} else {
return false;
}
return isValidRSS3();
}
return true;
},
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,11 @@
dependencies:
"@types/node" "*"

"@types/js-cookie@3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/js-cookie/-/js-cookie-3.0.0.tgz#6dcc3ab167251ace6964310789d8ea80f732a82c"
integrity sha512-GDVwSzwBm4OdQajFCit2UMxskZVcOhs/hYeOvzVW1R+iW6ZOVIBgD+RSrYCtPT0pNBnwNgRaoPPKfoXcwDo+hg==

"@types/json-schema@*", "@types/json-schema@^7.0.5":
version "7.0.7"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
Expand Down

0 comments on commit 08af748

Please sign in to comment.