Skip to content

Commit

Permalink
⚡️ Simplify login logic and reduce await post login (#1917)
Browse files Browse the repository at this point in the history
* ⚡️ Simplify login logic and reduce await post login

* ✏️ Fix event typo

Co-authored-by: Ng Wing Tat, David <[email protected]>

---------

Co-authored-by: Ng Wing Tat, David <[email protected]>
  • Loading branch information
williamchong and nwingt authored Oct 29, 2024
1 parent a6367c2 commit 3270c61
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
12 changes: 3 additions & 9 deletions src/mixins/wallet-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ export default {
'signLogin',
'openAuthcoreModal',
]),
async connectWallet({
shouldSkipLogin = false,
isOpenAuthcore = false,
isSignUp = false,
} = {}) {
async connectWallet({ isOpenAuthcore = false, isSignUp = false } = {}) {
try {
logTrackerEvent(
this,
Expand Down Expand Up @@ -54,15 +50,13 @@ export default {
1
);

const res = await (shouldSkipLogin
? this.initWallet(connection)
: this.initWalletAndLogin(connection));
const res = await this.initWalletAndLogin(connection);

if (res) {
logTrackerEvent(
this,
'user',
`connect_wallet_done${shouldSkipLogin ? '' : '_with_login'}`,
`connect_wallet_done_with_login`,
'connect_wallet_done',
1
);
Expand Down
43 changes: 13 additions & 30 deletions src/store/modules/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ const actions = {
method: getters.walletMethodType,
event: 'login',
});
dispatch('walletFetchSessionUserData', { shouldSkipUserInfo: true });
dispatch('walletFetchSessionUserData');
} else if (getters.getAddress) {
// Re-login if the wallet address is different from session
await dispatch('signLogin');
Expand All @@ -385,26 +385,11 @@ const actions = {
return connector;
},

async handleConnectorRedirect(
{ dispatch, getters },
{ method, params, isLogin = true }
) {
async handleConnectorRedirect({ dispatch }, { method, params }) {
const connector = await dispatch('getConnector');
const connection = await connector.handleRedirect(method, params);
if (connection) {
if (isLogin) {
await dispatch('initWalletAndLogin', connection);
} else {
await dispatch('initWallet', connection);
if (getters.walletIsMatchedSession) {
await setLoggerUser(this, {
wallet: getters.loginAddress,
method: getters.walletMethodType,
event: 'login',
});
dispatch('walletFetchSessionUserData', { shouldSkipUserInfo: true });
}
}
await dispatch('initWalletAndLogin', connection);
}
return connection;
},
Expand Down Expand Up @@ -496,20 +481,16 @@ const actions = {
await dispatch('initWallet', { accounts, method });
if (getters.walletIsMatchedSession) {
await setLoggerUser(this, { wallet: getters.loginAddress, method });
dispatch('walletFetchSessionUserData', { shouldSkipUserInfo: true });
dispatch('walletFetchSessionUserData');
}
}
},

async initIfNecessary({ dispatch }, { isLogin = true } = {}) {
async initIfNecessary({ dispatch }) {
const connector = await dispatch('getConnector');
const connection = await connector.initIfNecessary();
if (connection) {
if (isLogin) {
await dispatch('initWalletAndLogin', connection);
} else {
await dispatch('initWallet', connection);
}
await dispatch('initWalletAndLogin', connection);
}
},

Expand Down Expand Up @@ -869,15 +850,17 @@ const actions = {
},
async walletFetchSessionUserData(
{ dispatch },
{ shouldSkipUserInfo = false } = {}
{ awaitForWalletEvents = false } = {}
) {
const promises = [];
if (!shouldSkipUserInfo) {
promises.push(dispatch('walletFetchSessionUserInfo'));
promises.push(dispatch('walletFetchSessionUserInfo'));
const walletEventPromise = dispatch('walletFetchFollowees').then(() =>
dispatch('fetchWalletEvents', { shouldFetchDetails: false })
);
if (awaitForWalletEvents) {
promises.push(walletEventPromise);
}
promises.push(dispatch('walletFetchFollowees'));
await Promise.all(promises);
await dispatch('fetchWalletEvents', { shouldFetchDetails: false });
},
async signLogin({ state, commit, dispatch }) {
// Do not trigger login if the window is not focused
Expand Down

0 comments on commit 3270c61

Please sign in to comment.