Skip to content

Commit e0957a5

Browse files
committed
Rename to init + adds login_mode + adds connectTo hook in web3auth modal
1 parent b58312f commit e0957a5

File tree

15 files changed

+91
-24
lines changed

15 files changed

+91
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Hop on to the [Web3Auth Dashboard](https://dashboard.web3auth.io/) and create a
5151
Web3Auth needs to initialise as soon as your app loads up to enable the user to log in. Preferably done within a constructor, initialisation is the step where you can pass on all the configurations for Web3Auth you want. A simple integration for Ethereum blockchain will look like this:
5252

5353
```js
54-
import { Web3Auth } from "@web3auth/modal";
54+
import { Web3Auth } from "web3auth";
5555

5656
//Initialize within your constructor
5757
const web3auth = new Web3Auth({
@@ -62,7 +62,7 @@ const web3auth = new Web3Auth({
6262
},
6363
});
6464

65-
await web3auth.initModal();
65+
await web3auth.init();
6666
```
6767

6868
### Login your User

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import styles from "../styles/Home.module.css";
1616

1717
const Main = () => {
1818
const { provider, isConnected } = useWeb3Auth();
19-
const { loading: connecting, connect, error: connectingError, connectorName } = useWeb3AuthConnect();
19+
const { loading: connecting, connect, error: connectingError, connectorName, connectTo } = useWeb3AuthConnect();
2020
const { disconnect } = useWeb3AuthDisconnect();
2121
const { signMessageAsync, data: signedMessageData } = useSignMessage();
2222
const { address, isConnected: isWagmiConnected } = useAccount();
@@ -210,9 +210,14 @@ const Main = () => {
210210
{connecting ? (
211211
<p>Connecting to {connectorName}...</p>
212212
) : (
213-
<button onClick={() => connect()} className={styles.card}>
214-
Login
215-
</button>
213+
<>
214+
<button onClick={() => connect()} className={styles.card}>
215+
Login with Modal
216+
</button>
217+
<button onClick={() => connectTo("auth", { authConnection: "facebook" })} className={styles.card}>
218+
Login with Facebook
219+
</button>
220+
</>
216221
)}
217222
{connectingError && <p>Error: {connectingError.message}</p>}
218223
</>

packages/modal/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Hop on to the [Web3Auth Dashboard](https://dashboard.web3auth.io/) and create a
4343
Web3Auth needs to initialise as soon as your app loads up to enable the user to log in. Preferably done within a constructor, initialisation is the step where you can pass on all the configurations for Web3Auth you want. A simple integration for Ethereum blockchain will look like this:
4444

4545
```js
46-
import { Web3Auth } from "@web3auth/modal";
46+
import { Web3Auth } from "web3auth";
4747

4848
//Initialize within your constructor
4949
const web3auth = new Web3Auth({
@@ -54,7 +54,7 @@ const web3auth = new Web3Auth({
5454
},
5555
});
5656

57-
await web3auth.initModal();
57+
await web3auth.init();
5858
```
5959

6060
### Login your User

packages/modal/src/interface.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ export interface ConnectorsModalConfig {
99
hideWalletDiscovery?: boolean;
1010
}
1111
export interface IWeb3AuthModal extends IWeb3Auth {
12-
initModal(options?: { signal?: AbortSignal }): Promise<void>;
1312
connect(): Promise<IProvider | null>;
1413
}

packages/modal/src/modalManager.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type IWeb3AuthCoreOptions,
1818
IWeb3AuthState,
1919
log,
20+
LOGIN_MODE,
2021
type LoginMethodConfig,
2122
type ProjectConfig,
2223
type WALLET_CONNECTOR_TYPE,
@@ -61,7 +62,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
6162
log.info("modalConfig", this.modalConfig);
6263
}
6364

64-
public async initModal(options?: { signal?: AbortSignal }): Promise<void> {
65+
public async init(options?: { signal?: AbortSignal }): Promise<void> {
6566
const { signal } = options || {};
6667

6768
super.checkInitRequirements();
@@ -512,7 +513,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
512513

513514
private onSocialLogin = async (params: { connector: WALLET_CONNECTOR_TYPE; loginParams: AuthLoginParams }): Promise<void> => {
514515
try {
515-
await this.connectTo(WALLET_CONNECTORS.AUTH, params.loginParams);
516+
await this.connectTo(WALLET_CONNECTORS.AUTH, params.loginParams, LOGIN_MODE.MODAL);
516517
} catch (error) {
517518
log.error(`Error while connecting to connector: ${params.connector}`, error);
518519
}
@@ -523,7 +524,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
523524
loginParams: { chainNamespace: ChainNamespaceType };
524525
}): Promise<void> => {
525526
try {
526-
await this.connectTo(params.connector as WALLET_CONNECTOR_TYPE, params.loginParams);
527+
await this.connectTo(params.connector as WALLET_CONNECTOR_TYPE, params.loginParams, LOGIN_MODE.MODAL);
527528
} catch (error) {
528529
log.error(`Error while connecting to connector: ${params.connector}`, error);
529530
}

packages/modal/src/react/context/Web3AuthInnerContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
4646
try {
4747
setInitError(null);
4848
setIsInitializing(true);
49-
await web3Auth.initModal({ signal: controller.signal });
49+
await web3Auth.init({ signal: controller.signal });
5050
} catch (error) {
5151
setInitError(error as Error);
5252
} finally {

packages/modal/src/react/hooks/useWeb3AuthConnect.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IProvider, WALLET_CONNECTOR_TYPE, Web3AuthError } from "@web3auth/no-modal";
1+
import type { IProvider, LoginParamMap, WALLET_CONNECTOR_TYPE, Web3AuthError } from "@web3auth/no-modal";
22
import { useCallback, useEffect, useState } from "react";
33

44
import { useWeb3AuthInner } from "../hooks/useWeb3AuthInner";
@@ -9,6 +9,7 @@ export interface IUseWeb3AuthConnect {
99
error: Web3AuthError | null;
1010
connectorName: WALLET_CONNECTOR_TYPE | null;
1111
connect(): Promise<IProvider | null>;
12+
connectTo<T extends WALLET_CONNECTOR_TYPE>(connector: T, params?: LoginParamMap[T]): Promise<IProvider | null>;
1213
}
1314

1415
export const useWeb3AuthConnect = (): IUseWeb3AuthConnect => {
@@ -44,11 +45,31 @@ export const useWeb3AuthConnect = (): IUseWeb3AuthConnect => {
4445
}
4546
}, [web3Auth]);
4647

48+
const connectTo = useCallback(
49+
async <T extends WALLET_CONNECTOR_TYPE>(connector: T, params?: LoginParamMap[T]) => {
50+
setLoading(true);
51+
setError(null);
52+
try {
53+
const provider = await web3Auth.connectTo(connector, params);
54+
if (provider) {
55+
setConnectorName(web3Auth.connectedConnectorName);
56+
}
57+
return provider;
58+
} catch (error) {
59+
setError(error as Web3AuthError);
60+
} finally {
61+
setLoading(false);
62+
}
63+
},
64+
[web3Auth]
65+
);
66+
4767
return {
4868
isConnected,
4969
loading,
5070
error,
5171
connectorName,
5272
connect,
73+
connectTo,
5374
};
5475
};

packages/modal/src/ui/loginModal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
CONNECTOR_EVENTS,
1111
type IConnectorDataEvent,
1212
log,
13+
LOGIN_MODE,
1314
type LoginMethodConfig,
1415
type MetaMaskConnectorData,
1516
type WALLET_CONNECTOR_TYPE,
@@ -367,7 +368,7 @@ export class LoginModal {
367368
listener.on(CONNECTOR_EVENTS.CONNECTED, (data: CONNECTED_EVENT_DATA) => {
368369
log.debug("connected with connector", data);
369370
// only show success if not being reconnected again.
370-
if (!data.reconnected) {
371+
if (!data.reconnected && data.loginMode === LOGIN_MODE.MODAL) {
371372
this.setState({
372373
status: MODAL_STATUS.CONNECTED,
373374
modalVisibility: true,

packages/modal/src/ui/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h1>Test web3auth app</h1>
2929
theme: "dark",
3030
// version: "1",
3131
});
32-
await modal.initModal();
32+
await modal.initSdk();
3333
console.log("modal init");
3434
modal.addSocialLogins(
3535
"auth",

packages/modal/src/vue/Web3AuthProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const Web3AuthProvider = defineComponent({
7474
try {
7575
initError.value = null;
7676
isInitializing.value = true;
77-
await newWeb3Auth.initModal({ signal: controller.signal });
77+
await newWeb3Auth.init({ signal: controller.signal });
7878
} catch (error) {
7979
initError.value = error as Error;
8080
} finally {

0 commit comments

Comments
 (0)