Skip to content

Commit

Permalink
Fix when web3auth redirect key is removed, and don't error when waiti…
Browse files Browse the repository at this point in the history
…ng for redirect.
  • Loading branch information
NoahSaso committed Apr 18, 2023
1 parent 3dcd1a9 commit 34c1737
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noahsaso/cosmodal",
"version": "0.10.6",
"version": "0.10.8",
"repository": {
"type": "git",
"url": "git+https://github.com/NoahSaso/cosmodal.git"
Expand Down
21 changes: 17 additions & 4 deletions src/wallets/web3auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import eccrypto, { Ecies } from "@toruslabs/eccrypto"
import { LOGIN_PROVIDER_TYPE, UX_MODE } from "@toruslabs/openlogin"
import { isMobile } from "@walletconnect/utils"
import {
ADAPTER_STATUS,
CHAIN_NAMESPACES,
SafeEventEmitterProvider,
WALLET_ADAPTERS,
Expand Down Expand Up @@ -132,7 +133,8 @@ export const connectClientAndProvider = async (
// If using redirect method while trying to login, set localStorage key
// indicating that we should try to reconnect to this wallet after the
// redirect on library init.
if (uxMode === UX_MODE.REDIRECT && !dontAttemptLogin) {
const usingRedirect = uxMode === UX_MODE.REDIRECT && !dontAttemptLogin
if (usingRedirect) {
localStorage.setItem(WEB3AUTH_REDIRECT_AUTO_CONNECT_KEY, walletType)
}

Expand All @@ -159,9 +161,20 @@ export const connectClientAndProvider = async (
loginProvider,
}))

// On successful connection, remove the localStorage key indicating that we
// should try to reconnect to this wallet after the redirect on library init.
localStorage.removeItem(WEB3AUTH_REDIRECT_AUTO_CONNECT_KEY)
if (usingRedirect) {
if (client.status === ADAPTER_STATUS.CONNECTED) {
// On successful connection from a redirect, remove the localStorage key
// so we do not attempt to auto connect on the next page load.
localStorage.removeItem(WEB3AUTH_REDIRECT_AUTO_CONNECT_KEY)
} else {
// If not yet connected but redirecting, hang to give the page time to
// redirect without throwing any errors. After 30 seconds, throw a
// timeout error because it should definitely have redirected by then.
await new Promise((_, reject) =>
setTimeout(() => reject(new Error("Redirect timed out.")), 30000)
)
}
}

return {
client,
Expand Down

1 comment on commit 34c1737

@vercel
Copy link

@vercel vercel bot commented on 34c1737 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

cosmodal – ./

cosmodal.vercel.app
cosmodal-git-main-noahsaso.vercel.app
cosmodal-noahsaso.vercel.app

Please sign in to comment.