Skip to content

Commit e74ac8b

Browse files
committed
feat: updated hooks for isAuthorized status
1 parent cf9826b commit e74ac8b

File tree

32 files changed

+145
-31
lines changed

32 files changed

+145
-31
lines changed

demo/vue-app-new/src/MainView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const options = computed((): Web3AuthOptions => {
139139
connectors: modalParams.value,
140140
hideWalletDiscovery: !formData.showWalletDiscovery,
141141
},
142-
initialAuthenticationMode: "connect-and-sign",
142+
initialAuthenticationMode: formData.initialAuthenticationMode,
143143
};
144144
});
145145

demo/vue-app-new/src/components/AppDashboard.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const props = defineProps<{
4040
chains: CustomChainConfig[];
4141
}>();
4242
43-
const { isConnected, provider, web3Auth, isMFAEnabled } = useWeb3Auth();
43+
const { isConnected, provider, web3Auth, isMFAEnabled, isAuthorized } = useWeb3Auth();
4444
const { userInfo, loading: userInfoLoading } = useWeb3AuthUser();
4545
const { enableMFA } = useEnableMFA();
4646
const { manageMFA } = useManageMFA();
@@ -92,7 +92,7 @@ const isDisplay = (name: "dashboard" | "ethServices" | "solServices" | "walletSe
9292
const chainNamespace = currentChainNamespace.value;
9393
switch (name) {
9494
case "dashboard":
95-
return isConnected.value;
95+
return formData.initialAuthenticationMode === "connect-and-sign" ? isAuthorized.value : isConnected.value;
9696
9797
case "ethServices":
9898
return chainNamespace === CHAIN_NAMESPACES.EIP155;

demo/vue-app-new/src/components/AppHeader.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ import { Button } from "@toruslabs/vue-components";
33
import { useWeb3Auth, useWeb3AuthDisconnect } from "@web3auth/modal/vue";
44
import { useI18n } from "petite-vue-i18n";
55
import { watch } from "vue";
6+
import { formDataStore } from "../store/form";
67
78
const { log } = console;
89
const { t } = useI18n({ useScope: "global" });
910
10-
const { status, isConnected } = useWeb3Auth();
11+
const { status, isConnected, isAuthorized } = useWeb3Auth();
1112
const { disconnect } = useWeb3AuthDisconnect();
13+
const formData = formDataStore;
1214
1315
const isDisplay = (name: string): boolean => {
1416
switch (name) {
1517
case "btnLogout":
16-
return isConnected.value;
18+
return formData.initialAuthenticationMode === "connect-and-sign" ? isAuthorized.value : isConnected.value;
1719
1820
case "appHeading":
19-
return isConnected.value;
21+
return formData.initialAuthenticationMode === "connect-and-sign" ? isAuthorized.value : isConnected.value;
2022
2123
default: {
2224
return false;

demo/vue-app-new/src/components/AppSettings.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ const onSmartAccountChainChange = (chainIds: string[]) => {
229229
matchParentsWidth
230230
:show-check-box="true"
231231
/>
232+
<Select
233+
v-model="formData.initialAuthenticationMode"
234+
data-testid="selectInitialAuthenticationMode"
235+
:label="$t('app.initialAuthenticationMode')"
236+
:aria-label="$t('app.initialAuthenticationMode')"
237+
:placeholder="$t('app.initialAuthenticationMode')"
238+
:options="[
239+
{ name: 'Connect Only', value: 'connect-only' },
240+
{ name: 'Connect and Sign', value: 'connect-and-sign' },
241+
]"
242+
matchParentsWidth
243+
/>
232244
<Toggle
233245
v-model="formData.showWalletDiscovery"
234246
data-testid="showWalletDiscovery"

demo/vue-app-new/src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ChainNamespaceType,
55
CONFIRMATION_STRATEGY,
66
type CONFIRMATION_STRATEGY_TYPE,
7+
InitialAuthenticationModeType,
78
LoginMethodConfig,
89
ModalConfig,
910
SignTypedDataMessageV4,
@@ -109,6 +110,7 @@ export type FormData = {
109110
config: WhiteLabelData;
110111
};
111112
connectors: string[];
113+
initialAuthenticationMode: InitialAuthenticationModeType;
112114
loginProviders: AUTH_CONNECTION_TYPE[];
113115
showWalletDiscovery: boolean;
114116
multiInjectedProviderDiscovery: boolean;

demo/vue-app-new/src/store/form.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { chainConfigs, defaultLoginMethod, FormData, initWhiteLabel } from "../c
77
export const formDataStore = reactive<FormData>({
88
// authMode: "",
99
connectors: [],
10+
initialAuthenticationMode: "connect-only",
1011
network: process.env.NODE_ENV === "production" ? WEB3AUTH_NETWORK.SAPPHIRE_MAINNET : WEB3AUTH_NETWORK.SAPPHIRE_DEVNET,
1112
chainNamespaces: [CHAIN_NAMESPACES.EIP155, CHAIN_NAMESPACES.SOLANA],
1213
chains: [chainConfigs[CHAIN_NAMESPACES.EIP155][0], chainConfigs[CHAIN_NAMESPACES.SOLANA][0]],

demo/vue-app-new/src/translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"defaultChainId": "Default Chain ID",
1010
"loginProviders": "Login provider",
1111
"connectors": "Connectors",
12+
"initialAuthenticationMode": "Authentication Mode",
1213
"showWalletDiscovery": "Show Wallet Discovery",
1314
"multiInjectedProviderDiscovery": "Multi Injected Provider Discovery",
1415
"greeting": "Let's configure Web3Auth!",

packages/modal/src/modalManager.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
123123
onSocialLogin: this.onSocialLogin,
124124
onExternalWalletLogin: this.onExternalWalletLogin,
125125
onModalVisibility: this.onModalVisibility,
126+
onMobileVerifyConnect: this.onMobileVerifyConnect,
126127
}
127128
);
128129
await withAbort(() => this.loginModal.initModal(), signal);
@@ -659,6 +660,15 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
659660
}
660661
};
661662

663+
private onMobileVerifyConnect = async (params: { connector: WALLET_CONNECTOR_TYPE }): Promise<void> => {
664+
try {
665+
const connector = this.getConnector(params.connector);
666+
await connector.getIdentityToken();
667+
} catch (error) {
668+
log.error(`Error while connecting to connector: ${params.connector}`, error);
669+
}
670+
};
671+
662672
private getChainNamespaces = (): ChainNamespaceType[] => {
663673
return [...new Set(this.coreOptions.chains?.map((x) => x.chainNamespace) || [])];
664674
};

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
3434
}, [web3AuthOptions, initialState]);
3535

3636
const [isConnected, setIsConnected] = useState<boolean>(web3Auth.status === CONNECTOR_STATUS.CONNECTED);
37+
const [isAuthorized, setIsAuthorized] = useState<boolean>(web3Auth.status === CONNECTOR_STATUS.AUTHORIZED);
3738
const [status, setStatus] = useState<CONNECTOR_STATUS_TYPE | null>(web3Auth.status);
3839

3940
const getPlugin = useCallback(
@@ -97,6 +98,12 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
9798
setProvider(data.provider);
9899
}
99100
};
101+
const authorizedListener = (_data: { connector: string }) => {
102+
setStatus(web3Auth.status);
103+
if (web3Auth.status === CONNECTOR_STATUS.AUTHORIZED) {
104+
setIsAuthorized(true);
105+
}
106+
};
100107
const disconnectedListener = () => {
101108
setStatus(web3Auth.status);
102109
setIsConnected(false);
@@ -125,6 +132,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
125132
web3Auth.on(CONNECTOR_EVENTS.NOT_READY, notReadyListener);
126133
web3Auth.on(CONNECTOR_EVENTS.READY, readyListener);
127134
web3Auth.on(CONNECTOR_EVENTS.CONNECTED, connectedListener);
135+
web3Auth.on(CONNECTOR_EVENTS.AUTHORIZED, authorizedListener);
128136
web3Auth.on(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
129137
web3Auth.on(CONNECTOR_EVENTS.CONNECTING, connectingListener);
130138
web3Auth.on(CONNECTOR_EVENTS.ERRORED, errorListener);
@@ -137,6 +145,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
137145
web3Auth.off(CONNECTOR_EVENTS.NOT_READY, notReadyListener);
138146
web3Auth.off(CONNECTOR_EVENTS.READY, readyListener);
139147
web3Auth.off(CONNECTOR_EVENTS.CONNECTED, connectedListener);
148+
web3Auth.off(CONNECTOR_EVENTS.AUTHORIZED, authorizedListener);
140149
web3Auth.off(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
141150
web3Auth.off(CONNECTOR_EVENTS.CONNECTING, connectingListener);
142151
web3Auth.off(CONNECTOR_EVENTS.ERRORED, errorListener);
@@ -152,6 +161,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
152161
return {
153162
web3Auth,
154163
isConnected,
164+
isAuthorized,
155165
isInitialized,
156166
provider,
157167
status,
@@ -166,6 +176,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
166176
}, [
167177
web3Auth,
168178
isConnected,
179+
isAuthorized,
169180
isMFAEnabled,
170181
setIsMFAEnabled,
171182
isInitialized,

packages/modal/src/ui/components/Loader/Loader.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WALLET_CONNECTOR_TYPE } from "@web3auth/no-modal";
12
import { useEffect, useMemo } from "react";
23
import { useTranslation } from "react-i18next";
34

@@ -91,16 +92,14 @@ function ErroredStatus(props: ErroredStatusType) {
9192

9293
function AuthorizingStatus(props: AuthorizingStatusType) {
9394
const [t] = useTranslation(undefined, { i18n });
94-
const { connector, externalWalletsConfig, walletRegistry } = props;
95-
96-
// eslint-disable-next-line no-console
97-
console.log("externalWalletsConfig", externalWalletsConfig);
95+
const { connector, externalWalletsConfig, walletRegistry, handleMobileVerifyConnect } = props;
9896

9997
const registryItem = walletRegistry?.default?.[connector] || walletRegistry?.others?.[connector];
10098
const primaryColor = registryItem?.primaryColor || "";
10199

102-
// eslint-disable-next-line no-console
103-
console.log("registryItem", registryItem);
100+
const handleMobileVerifyConnectClick = () => {
101+
handleMobileVerifyConnect({ connector: connector as WALLET_CONNECTOR_TYPE });
102+
};
104103

105104
return (
106105
<div className="w3a--flex w3a--size-full w3a--flex-col w3a--items-center w3a--justify-between w3a--gap-y-6">
@@ -113,14 +112,12 @@ function AuthorizingStatus(props: AuthorizingStatusType) {
113112
</CircularLoader>
114113
</div>
115114
<p className="w3a--text-center w3a--text-sm w3a--text-app-gray-500 dark:w3a--text-app-gray-400">{t("modal.loader.authorizing-message")}</p>
116-
<a
117-
href={"https://www.google.com"}
118-
target="_blank"
119-
rel="noopener noreferrer"
115+
<button
116+
onClick={handleMobileVerifyConnectClick}
120117
className="w3a--w-full w3a--rounded-xl w3a--bg-app-gray-100 w3a--p-2 w3a--py-3 w3a--text-center w3a--text-sm w3a--text-app-gray-900 dark:w3a--bg-app-gray-800 dark:w3a--text-app-white md:w3a--hidden"
121118
>
122119
{t("modal.loader.authorizing-verify-btn")}
123-
</a>
120+
</button>
124121
</div>
125122
);
126123
}
@@ -141,8 +138,7 @@ function Loader(props: LoaderProps) {
141138
isConnectAndSignAuthenticationMode,
142139
externalWalletsConfig,
143140
walletRegistry,
144-
walletConnectUri,
145-
metamaskConnectUri,
141+
handleMobileVerifyConnect,
146142
} = props;
147143

148144
// eslint-disable-next-line no-console
@@ -170,7 +166,12 @@ function Loader(props: LoaderProps) {
170166
{modalStatus === MODAL_STATUS.ERRORED && <ErroredStatus message={message} />}
171167

172168
{modalStatus === MODAL_STATUS.AUTHORIZING && (
173-
<AuthorizingStatus connector={connector} externalWalletsConfig={externalWalletsConfig} walletRegistry={walletRegistry} />
169+
<AuthorizingStatus
170+
connector={connector}
171+
externalWalletsConfig={externalWalletsConfig}
172+
walletRegistry={walletRegistry}
173+
handleMobileVerifyConnect={handleMobileVerifyConnect}
174+
/>
174175
)}
175176
</div>
176177
);

0 commit comments

Comments
 (0)