From 981b9215fb6c2049fce50a1f6339407fdbf40f9e Mon Sep 17 00:00:00 2001 From: S Date: Sat, 3 Jul 2021 08:02:29 +0530 Subject: [PATCH 1/2] [FIX]: Issue#63 - Logout on Page Refresh --- src/ducks/signIn.js | 5 ++++- src/redux/rootReducer.js | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/ducks/signIn.js b/src/ducks/signIn.js index c4afb017..9ce946b4 100644 --- a/src/ducks/signIn.js +++ b/src/ducks/signIn.js @@ -1,5 +1,6 @@ import Portis from '@portis/web3'; import Web3 from 'web3'; +import { REHYDRATE } from 'redux-persist'; import { appName, INFURA_PROVIDER_WSS, @@ -43,6 +44,8 @@ export default function reducer( state = initialState, action ) { const { type, payload, error } = action; switch (type) { + case REHYDRATE: + return { ...state, isAuthenticated: payload && payload.isAuthenticated } case FETCH_SIGN_IN_REQUEST: return Object.assign({}, state, { isFetching: true, @@ -51,7 +54,7 @@ export default function reducer( state = initialState, action ) { error: null }); case SET_DEFAULT_WEB3: - return { + return {...state, web3: payload.web3, error: null }; diff --git a/src/redux/rootReducer.js b/src/redux/rootReducer.js index e14f0731..8659b539 100644 --- a/src/redux/rootReducer.js +++ b/src/redux/rootReducer.js @@ -1,4 +1,6 @@ import { combineReducers } from "redux"; +import { persistReducer } from 'redux-persist'; +import storage from 'redux-persist/lib/storage'; import fetchProfileOrganizations, { moduleName as profileModule } from '../ducks/fetchProfile'; import fetchSearchOrganizations, { moduleName as searchModule } from '../ducks/fetchSearchResults'; import fetchOrganizationInfo, { moduleName as orgInfoModule } from '../ducks/fetchOrganizationInfo'; @@ -10,12 +12,18 @@ import joinOrganisations, {moduleName as joinOrganisationsModule} from '../ducks import orgActiveStatus, {moduleName as orgActiveStatusModule} from '../ducks/orgActiveStatus'; import directories, { moduleName as directoriesModule } from '../ducks/directories'; +const signInPersistConfig = { + storage, + key: 'signIn', + blacklist: ['web3'] +} + //Add all reducers here export default combineReducers({ [profileModule]: fetchProfileOrganizations, [searchModule]: fetchSearchOrganizations, [orgInfoModule]: fetchOrganizationInfo, - [signInModule]: fetchSignIn, + [signInModule]: persistReducer(signInPersistConfig, fetchSignIn), [wizardModule]: extendWizard, [depositModule]: fetchLifDeposit, [backendStatusModule]: backendStatus, From 7b5eb73d09b5aaae941fddb07bff787e177c449e Mon Sep 17 00:00:00 2001 From: S Date: Sat, 3 Jul 2021 10:52:14 +0530 Subject: [PATCH 2/2] [FIX]: Issue#63~extension - Signin success/failure on refresh or browser open --- src/ducks/signIn.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ducks/signIn.js b/src/ducks/signIn.js index 9ce946b4..13a66e46 100644 --- a/src/ducks/signIn.js +++ b/src/ducks/signIn.js @@ -45,7 +45,7 @@ export default function reducer( state = initialState, action ) { switch (type) { case REHYDRATE: - return { ...state, isAuthenticated: payload && payload.isAuthenticated } + return { ...state, isAuthenticated: payload && payload.isAuthenticated && window.ethereum.isConnected() } // TODO: Set appropriate checks to make sure Wallet is connected with the right address case FETCH_SIGN_IN_REQUEST: return Object.assign({}, state, { isFetching: true, @@ -218,6 +218,7 @@ export const subscribeMetamaskEventChannel = web3 => { window.ethereum.on('accountsChanged', handleNewAccounts); window.ethereum.on('chainChanged', handleChainChange); + window.ethereum.on('disconnect', handleNewAccounts); return () => {}; }); @@ -233,9 +234,20 @@ const openPortisPopUp = async () => { function* setDefaultWeb3Saga() { try { + const provider = yield select(selectProvider); + const address = yield select(selectSignInAddress); + + const web3Instance = new Web3(INFURA_PROVIDER_WSS) yield put(setDefaultWeb3({ - web3: new Web3(INFURA_PROVIDER_WSS) + web3: web3Instance })); + + const ethAccounts = yield call(window.ethereum.request, {method: 'eth_accounts'}) + if(ethAccounts[0] === address) { + yield put(fetchSignInSuccess({provider, web3: web3Instance, address})); + } else { + yield put(fetchSignInFailure((new Error('Address mismatch')))); + } } catch (error) { // Connection Failure yield put(fetchSignInFailure(error));