Skip to content

Commit ea6a623

Browse files
Merge pull request #2137 from Web3Auth/feat/optimization
optimise auth connector loading
2 parents 4b7be55 + 6c84476 commit ea6a623

File tree

19 files changed

+871
-278
lines changed

19 files changed

+871
-278
lines changed

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

Lines changed: 664 additions & 175 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: 16 additions & 19 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) => {
@@ -109,63 +117,51 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
109117
console.error("some error on plugin login", error);
110118
});
111119
};
120+
const controller = new AbortController();
112121

113122
async function init() {
114123
try {
115124
const currentChainConfig = CHAIN_CONFIG[chain];
116125
setIsLoading(true);
117126
const clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ";
118-
119127
const web3AuthInstance = new Web3Auth({
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({ signal: controller.signal });
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 {
165157
setIsLoading(false);
166158
}
167159
}
168160
init();
161+
162+
return () => {
163+
controller.abort();
164+
};
169165
}, [chain, web3AuthNetwork]);
170166

171167
const login = async () => {
@@ -304,6 +300,7 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
304300
provider,
305301
user,
306302
isLoading,
303+
isReady,
307304
login,
308305
logout,
309306
getUserInfo,

demo/vue-app-new/package-lock.json

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

demo/vue-app-new/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
"dependencies": {
1313
"@solana/web3.js": "^1.98.0",
1414
"@toruslabs/base-controllers": "^8.1.0",
15+
"@toruslabs/bs58": "^1.0.0",
1516
"@toruslabs/ethereum-controllers": "^8.2.0",
1617
"@toruslabs/solana-controllers": "^8.1.0",
18+
"@toruslabs/tweetnacl-js": "^1.0.4",
1719
"@toruslabs/vue-components": "^8.0.6",
1820
"@toruslabs/vue-icons": "^8.0.2",
19-
"@web3auth/auth": "^10.3.0",
21+
"@web3auth/auth": "^10.4.0",
2022
"@web3auth/modal": "file:../../packages/modal",
2123
"@web3auth/no-modal": "file:../../packages/no-modal",
2224
"@web3auth/sign-in-with-ethereum": "^5.0.0",
2325
"@web3auth/ws-embed": "^5.0.0",
24-
"@toruslabs/bs58": "^1.0.0",
2526
"ethers": "^6.13.5",
2627
"petite-vue-i18n": "^11.1.3",
27-
"@toruslabs/tweetnacl-js": "^1.0.4",
2828
"vue": "^3.5.13"
2929
},
3030
"devDependencies": {

0 commit comments

Comments
 (0)