Skip to content

Commit 74f76a1

Browse files
committed
optimise auth connector loading
1 parent 7270c72 commit 74f76a1

File tree

6 files changed

+85
-62
lines changed

6 files changed

+85
-62
lines changed

demo/vite-react-app/package-lock.json

Lines changed: 35 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/vite-react-app/src/components/Main.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WALLET_CONNECTORS } from "@web3auth/modal";
1+
import { CONNECTOR_STATUS, WALLET_CONNECTORS } from "@web3auth/modal";
22
import { useWeb3Auth } from "../services/web3auth";
33
import styles from "../styles/Home.module.css";
44

@@ -18,6 +18,7 @@ const Main = () => {
1818
showWalletConnectScanner,
1919
enableMFA,
2020
manageMFA,
21+
isReady,
2122
} = useWeb3Auth();
2223

2324
const loggedInView = (
@@ -68,9 +69,15 @@ const Main = () => {
6869
);
6970

7071
const unloggedInView = (
71-
<button onClick={login} className={styles.card}>
72-
Login
73-
</button>
72+
<>
73+
{isReady ? (
74+
<button onClick={login} className={styles.card}>
75+
Login
76+
</button>
77+
) : (
78+
<p>Loading Web3Auth...</p>
79+
)}
80+
</>
7481
);
7582

7683
return <div className={styles.grid}>{provider ? loggedInView : unloggedInView}</div>;

demo/vite-react-app/src/services/web3auth.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface IWeb3AuthContext {
77
web3Auth: Web3Auth | null;
88
provider: IProvider | null;
99
isLoading: boolean;
10+
isReady: boolean;
1011
user: unknown;
1112
chain: string;
1213
login: () => Promise<void>;
@@ -29,6 +30,7 @@ export const Web3AuthContext = createContext<IWeb3AuthContext>({
2930
isLoading: false,
3031
user: null,
3132
chain: "",
33+
isReady: false,
3234
login: async () => {},
3335
logout: async () => {},
3436
getUserInfo: async () => {},
@@ -66,6 +68,7 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
6668
const [wsPlugin, setWsPlugin] = useState<WalletServicesPluginType | null>(null);
6769
const [user, setUser] = useState<unknown | null>(null);
6870
const [isLoading, setIsLoading] = useState(false);
71+
const [isReady, setIsReady] = useState(false);
6972

7073
useEffect(() => {
7174
const subscribeAuthEvents = (web3auth: Web3Auth) => {
@@ -88,6 +91,11 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
8891
web3auth.on(CONNECTOR_EVENTS.ERRORED, (error) => {
8992
console.error("some error or user has cancelled login request", error);
9093
});
94+
95+
web3auth.on(CONNECTOR_EVENTS.READY, () => {
96+
console.log("web3auth is ready");
97+
setIsReady(true);
98+
});
9199
};
92100

93101
const subscribePluginEvents = (plugin: WalletServicesPluginType) => {
@@ -120,45 +128,29 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
120128
// get your client id from https://dashboard.web3auth.io
121129
clientId,
122130
web3AuthNetwork,
123-
accountAbstractionConfig: {
124-
smartAccountType: SMART_ACCOUNT.SAFE,
125-
bundlerConfig: {
126-
url: `https://api.pimlico.io/v2/11155111/rpc?apikey=${pimlicoAPIKey}`,
127-
},
128-
paymasterConfig: {
129-
url: `https://api.pimlico.io/v2/11155111/rpc?apikey=${pimlicoAPIKey}`,
130-
},
131-
},
132131
chains: [currentChainConfig],
133132
uiConfig: {
134-
uxMode: "redirect",
135133
appName: "W3A Heroes",
136134
appUrl: "https://web3auth.io/",
137135
theme: {
138136
primary: "#5f27cd",
139137
onPrimary: "white",
140138
},
141-
logoLight: "https://web3auth.io/images/web3auth-logo.svg",
142-
logoDark: "https://web3auth.io/images/web3auth-logo.svg",
143139
defaultLanguage: "en", // en, de, ja, ko, zh, es, fr, pt, nl, tr
144140
mode: "auto", // whether to enable dark mode. defaultValue: auto
145141
// useLogoLoader: true,
146142
loginGridCol: 3,
147143
primaryButton: "socialLogin",
148144
},
149145
enableLogging: true,
150-
plugins: [walletServicesPlugin()],
146+
authBuildEnv: "testing"
151147
});
152148

153-
setWsPlugin(wsPlugin);
154149

155150
subscribeAuthEvents(web3AuthInstance);
156151
setWeb3Auth(web3AuthInstance);
157-
await web3AuthInstance.init();
152+
await web3AuthInstance.initModal();
158153

159-
const plugin = web3AuthInstance.getPlugin(EVM_PLUGINS.WALLET_SERVICES) as WalletServicesPluginType;
160-
setWsPlugin(plugin);
161-
subscribePluginEvents(plugin);
162154
} catch (error) {
163155
console.error(error);
164156
} finally {
@@ -304,6 +296,7 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
304296
provider,
305297
user,
306298
isLoading,
299+
isReady,
307300
login,
308301
logout,
309302
getUserInfo,

package-lock.json

Lines changed: 3 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/no-modal/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"@walletconnect/sign-client": "^2.19.1",
6969
"@walletconnect/types": "^2.19.1",
7070
"@walletconnect/utils": "^2.19.1",
71-
"@web3auth/auth": "^10.3.0",
71+
"@web3auth/auth": "/Users/architgupta/Desktop/web3auth/OpenLoginSdk/web3auth-auth-10.3.2.tgz",
7272
"@web3auth/ws-embed": "^5.0.0",
7373
"assert": "^2.1.0",
7474
"bignumber.js": "^9.1.2",

packages/no-modal/src/connectors/auth-connector/authConnector.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
7272

7373
private authConnectionConfig: (AuthConnectionConfigItem & { isDefault?: boolean })[] = [];
7474

75+
private wsEmbedInstancePromise: Promise<void> | null = null;
76+
7577
constructor(params: AuthConnectorOptions) {
7678
super(params);
7779

@@ -120,7 +122,8 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
120122
});
121123
log.debug("initializing auth connector init", this.authOptions);
122124

123-
await this.authInstance.init();
125+
// making it async here to initialize provider.
126+
const authInstancePromise = this.authInstance.init();
124127

125128
// Use this for xrpl, mpc cases
126129
if (this.coreOptions.privateKeyProvider) {
@@ -139,15 +142,20 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
139142
const wsSupportedChains = chains.filter(
140143
(x) => x.chainNamespace === CHAIN_NAMESPACES.EIP155 || x.chainNamespace === CHAIN_NAMESPACES.SOLANA
141144
);
142-
await this.wsEmbedInstance.init({
143-
...this.wsSettings,
144-
chains: wsSupportedChains as ProviderConfig[],
145-
chainId,
146-
whiteLabel: {
147-
...this.authOptions.whiteLabel,
148-
...this.wsSettings.whiteLabel,
149-
},
150-
});
145+
this.wsEmbedInstancePromise = this.wsEmbedInstance
146+
.init({
147+
...this.wsSettings,
148+
chains: wsSupportedChains as ProviderConfig[],
149+
chainId,
150+
whiteLabel: {
151+
...this.authOptions.whiteLabel,
152+
...this.wsSettings.whiteLabel,
153+
},
154+
})
155+
.then(() => {
156+
this.wsEmbedInstancePromise = null;
157+
return;
158+
});
151159
break;
152160
}
153161
case CHAIN_NAMESPACES.XRPL:
@@ -164,6 +172,9 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
164172
}
165173
}
166174

175+
// wait for auth instance to be ready.
176+
await authInstancePromise;
177+
167178
this.status = CONNECTOR_STATUS.READY;
168179
this.emit(CONNECTOR_EVENTS.READY, WALLET_CONNECTORS.AUTH);
169180

@@ -339,6 +350,9 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
339350

340351
// setup WS embed if chainNamespace is EIP155 or SOLANA
341352
if (chainNamespace === CHAIN_NAMESPACES.EIP155 || chainNamespace === CHAIN_NAMESPACES.SOLANA) {
353+
// wait for ws embed instance to be ready.
354+
if (this.wsEmbedInstancePromise) await this.wsEmbedInstancePromise;
355+
342356
const { sessionId, sessionNamespace } = this.authInstance || {};
343357
if (sessionId) {
344358
const isLoggedIn = await this.wsEmbedInstance.loginWithSessionId({

0 commit comments

Comments
 (0)