Skip to content

Commit f8e0f2b

Browse files
add more hooks for authorized listener
1 parent e74ac8b commit f8e0f2b

File tree

8 files changed

+25
-4
lines changed

8 files changed

+25
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
101101
const authorizedListener = (_data: { connector: string }) => {
102102
setStatus(web3Auth.status);
103103
if (web3Auth.status === CONNECTOR_STATUS.AUTHORIZED) {
104+
setIsConnected(true);
104105
setIsAuthorized(true);
105106
}
106107
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { useWeb3AuthInner } from "./useWeb3AuthInner";
44
export type IUseWeb3Auth = Omit<IWeb3AuthInnerContext, "isMFAEnabled" | "setIsMFAEnabled" | "chainId" | "chainNamespace">;
55

66
export const useWeb3Auth = (): IUseWeb3Auth => {
7-
const { initError, isConnected, isInitialized, isInitializing, provider, status, web3Auth, getPlugin } = useWeb3AuthInner();
7+
const { initError, isConnected, isAuthorized, isInitialized, isInitializing, provider, status, web3Auth, getPlugin } = useWeb3AuthInner();
88
return {
99
initError,
1010
isConnected,
11+
isAuthorized,
1112
isInitialized,
1213
isInitializing,
1314
provider,

packages/modal/src/vue/Web3AuthProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export const Web3AuthProvider = defineComponent({
129129
const authorizedListener = () => {
130130
status.value = web3Auth.value!.status;
131131
if (web3Auth.value!.status === CONNECTOR_STATUS.AUTHORIZED) {
132+
isConnected.value = true;
132133
isAuthorized.value = true;
133134
}
134135
};

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
3434
const [isInitialized, setIsInitialized] = useState<boolean>(false);
3535
const [status, setStatus] = useState<CONNECTOR_STATUS_TYPE | null>(null);
3636
const [isMFAEnabled, setIsMFAEnabled] = useState<boolean>(false);
37+
const [isAuthorized, setIsAuthorized] = useState<boolean>(false);
3738

3839
const getPlugin = useCallback(
3940
(name: string) => {
@@ -113,19 +114,30 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
113114
const rehydrationErrorListener = () => {
114115
setStatus(web3Auth.status);
115116
setIsConnected(false);
117+
setIsAuthorized(false);
116118
setProvider(null);
117119
};
118120

121+
const authorizedListener = () => {
122+
setStatus(web3Auth.status);
123+
if (web3Auth.status === CONNECTOR_STATUS.AUTHORIZED) {
124+
setIsConnected(true);
125+
setIsAuthorized(true);
126+
}
127+
};
128+
119129
const mfaEnabledListener = (isMFAEnabled: boolean) => {
120130
if (typeof isMFAEnabled === "boolean") setIsMFAEnabled(isMFAEnabled);
121131
};
122132

133+
// TODO: In strict mode, web3auth becomes null and .off throws an error sometimes.
123134
if (web3Auth) {
124135
// web3Auth is initialized here.
125136
setStatus(web3Auth.status);
126137
web3Auth.on(CONNECTOR_EVENTS.NOT_READY, notReadyListener);
127138
web3Auth.on(CONNECTOR_EVENTS.READY, readyListener);
128139
web3Auth.on(CONNECTOR_EVENTS.CONNECTED, connectedListener);
140+
web3Auth.on(CONNECTOR_EVENTS.AUTHORIZED, authorizedListener);
129141
web3Auth.on(CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
130142
web3Auth.on(CONNECTOR_EVENTS.CONNECTING, connectingListener);
131143
web3Auth.on(CONNECTOR_EVENTS.ERRORED, errorListener);
@@ -143,6 +155,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
143155
web3Auth.off(CONNECTOR_EVENTS.ERRORED, errorListener);
144156
web3Auth.off(CONNECTOR_EVENTS.REHYDRATION_ERROR, rehydrationErrorListener);
145157
web3Auth.off(CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
158+
web3Auth.off(CONNECTOR_EVENTS.AUTHORIZED, authorizedListener);
146159
}
147160
};
148161
}, [web3Auth]);
@@ -161,6 +174,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
161174
chainNamespace,
162175
getPlugin,
163176
setIsMFAEnabled,
177+
isAuthorized,
164178
};
165179
}, [
166180
web3Auth,
@@ -175,6 +189,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
175189
setIsMFAEnabled,
176190
chainId,
177191
chainNamespace,
192+
isAuthorized,
178193
]);
179194

180195
return createElement(Web3AuthInnerContext.Provider, { value }, children);

packages/no-modal/src/react/hooks/useIdentityToken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const useIdentityToken = () => {
1616
const [loading, setLoading] = useState(false);
1717
const [error, setError] = useState<Web3AuthError | null>(null);
1818
const [token, setToken] = useState<string | null>(null);
19-
19+
// TODO: store the authorized token in state so that we can return it here if already authorized
2020
const getIdentityToken = useCallback(async () => {
2121
setLoading(true);
2222
setError(null);

packages/no-modal/src/react/hooks/useWeb3Auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useWeb3AuthInner } from "./useWeb3AuthInner";
44
export type IUseWeb3Auth = Omit<IWeb3AuthInnerContext, "isMFAEnabled" | "setIsMFAEnabled" | "chainId" | "chainNamespace">;
55

66
export const useWeb3Auth = (): IUseWeb3Auth => {
7-
const { initError, isConnected, isInitialized, isInitializing, provider, status, web3Auth, getPlugin } = useWeb3AuthInner();
7+
const { initError, isConnected, isAuthorized, isInitialized, isInitializing, provider, status, web3Auth, getPlugin } = useWeb3AuthInner();
88
return {
99
initError,
1010
isConnected,
@@ -13,6 +13,7 @@ export const useWeb3Auth = (): IUseWeb3Auth => {
1313
provider,
1414
status,
1515
web3Auth,
16+
isAuthorized,
1617
getPlugin,
1718
};
1819
};

packages/no-modal/src/vue/Web3AuthProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export const Web3AuthProvider = defineComponent({
116116
status.value = web3Auth.value!.status;
117117
if (web3Auth.value!.status === CONNECTOR_STATUS.AUTHORIZED) {
118118
isAuthorized.value = true;
119+
isConnected.value = true;
119120
}
120121
};
121122

packages/no-modal/src/vue/composables/useWeb3Auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { useWeb3AuthInner } from "./useWeb3AuthInner";
44
export type IWeb3AuthNoModalContext = Omit<IWeb3AuthInnerContext, "isMFAEnabled" | "setIsMFAEnabled">;
55

66
export const useWeb3Auth = (): IWeb3AuthNoModalContext => {
7-
const { initError, isConnected, isInitialized, isInitializing, provider, status, web3Auth, getPlugin } = useWeb3AuthInner();
7+
const { initError, isConnected, isInitialized, isInitializing, isAuthorized, provider, status, web3Auth, getPlugin } = useWeb3AuthInner();
88
return {
99
initError,
1010
isConnected,
1111
isInitialized,
1212
isInitializing,
13+
isAuthorized,
1314
provider,
1415
status,
1516
web3Auth,

0 commit comments

Comments
 (0)