Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions demo/nextjs-ssr-app/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Main = () => {
const { showCheckout, loading: isCheckoutLoading, error: checkoutError } = useCheckout();
const { showWalletConnectScanner, loading: isWalletConnectScannerLoading, error: walletConnectScannerError } = useWalletConnectScanner();
const { showWalletUI, loading: isWalletUILoading, error: walletUIError } = useWalletUI();
const { token, loading: isUserTokenLoading, error: userTokenError, authenticateUser } = useIdentityToken();
const { token, loading: isUserTokenLoading, error: userTokenError, getIdentityToken } = useIdentityToken();

console.log("isConnected", isConnected);

Expand Down Expand Up @@ -56,7 +56,7 @@ const Main = () => {
{isUserTokenLoading ? (
<p>Authenticating...</p>
) : (
<button onClick={() => authenticateUser()} className="card">
<button onClick={() => getIdentityToken()} className="card">
Authenticate User
</button>
)}
Expand Down
6 changes: 3 additions & 3 deletions demo/react-app-no-modal/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ function App() {
setProvider(web3authProvider);
};

const authenticateUser = async () => {
const getIdentityToken = async () => {
if (!web3auth) {
uiConsole("web3auth not initialized yet");
return;
}
const idToken = await web3auth.authenticateUser();
const idToken = await web3auth.getIdentityToken();
uiConsole(idToken);
};

Expand Down Expand Up @@ -156,7 +156,7 @@ function App() {
</button>
</div>
<div>
<button onClick={authenticateUser} className="card">
<button onClick={getIdentityToken} className="card">
Get ID Token
</button>
</div>
Expand Down
10 changes: 5 additions & 5 deletions demo/vue-app-new/src/components/AppDashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const { switchChain } = useWeb3AuthSwitchChain();
const { showWalletUI, loading: showWalletUILoading } = useWalletUI();
const { showWalletConnectScanner, loading: showWalletConnectScannerLoading } = useWalletConnectScanner();
const { showCheckout, loading: showCheckoutLoading } = useCheckout();
const { authenticateUser, loading: authenticateUserLoading } = useIdentityToken();
const { getIdentityToken, loading: getIdentityTokenLoading } = useIdentityToken();
const { status, address } = useAccount();
const { signTypedDataAsync } = useSignTypedData();
const { signMessageAsync } = useSignMessage();
Expand Down Expand Up @@ -164,8 +164,8 @@ const onGetUserInfo = async () => {
printToConsole("User Info", userInfo.value);
};

const onAuthenticateUser = async () => {
const idToken = await authenticateUser();
const ongetIdentityToken = async () => {
const idToken = await getIdentityToken();
printToConsole("id token", idToken);
};

Expand Down Expand Up @@ -451,7 +451,7 @@ const onSwitchChainNamespace = async () => {
<Button block size="xs" pill class="mb-2" @click="onSignPersonalMsg">
{{ t("app.buttons.btnSignPersonalMsg") }}
</Button>
<Button :loading="authenticateUserLoading" block size="xs" pill class="mb-2" @click="onAuthenticateUser">Get id token</Button>
<Button :loading="getIdentityTokenLoading" block size="xs" pill class="mb-2" @click="ongetIdentityToken">Get id token</Button>
</Card>

<!-- SOLANA -->
Expand All @@ -473,7 +473,7 @@ const onSwitchChainNamespace = async () => {
<Button block size="xs" pill class="mb-2" @click="onSignAllTransactions">
{{ t("app.buttons.btnSignAllTransactions") }}
</Button>
<Button :loading="authenticateUserLoading" block size="xs" pill class="mb-2" @click="onAuthenticateUser">Get id token</Button>
<Button :loading="getIdentityTokenLoading" block size="xs" pill class="mb-2" @click="ongetIdentityToken">Get id token</Button>
</Card>
</Card>
<Card
Expand Down
4 changes: 2 additions & 2 deletions demo/wagmi-react-app/src/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Main = () => {
const { showCheckout, loading: isCheckoutLoading, error: checkoutError } = useCheckout();
const { showWalletConnectScanner, loading: isWalletConnectScannerLoading, error: walletConnectScannerError } = useWalletConnectScanner();
const { showWalletUI, loading: isWalletUILoading, error: walletUIError } = useWalletUI();
const { token, loading: isUserTokenLoading, error: userTokenError, authenticateUser } = useIdentityToken();
const { token, loading: isUserTokenLoading, error: userTokenError, getIdentityToken } = useIdentityToken();
const { switchChainAsync, chains } = useSwitchChain();
const chainId = useChainId();

Expand Down Expand Up @@ -57,7 +57,7 @@ const Main = () => {
{isUserTokenLoading ? (
<p>Authenticating...</p>
) : (
<button onClick={() => authenticateUser()} className={styles.card}>
<button onClick={() => getIdentityToken()} className={styles.card}>
Authenticate User
</button>
)}
Expand Down
3 changes: 3 additions & 0 deletions demo/wagmi-react-app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const web3authConfig: Web3AuthContextConfig = {
web3AuthNetwork: "sapphire_devnet",
clientId: clientId,
authBuildEnv: "testing",
uiConfig: {
uxMode: "redirect",
},
},
};

Expand Down
8 changes: 4 additions & 4 deletions packages/modal/src/react/hooks/useIdentityToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface IUseIdentityToken {
loading: boolean;
error: Web3AuthError | null;
token: string | null;
authenticateUser: () => Promise<string | null>;
getIdentityToken: () => Promise<string | null>;
}

export const useIdentityToken = () => {
Expand All @@ -17,11 +17,11 @@ export const useIdentityToken = () => {
const [error, setError] = useState<Web3AuthError | null>(null);
const [token, setToken] = useState<string | null>(null);

const authenticateUser = useCallback(async () => {
const getIdentityToken = useCallback(async () => {
setLoading(true);
setError(null);
try {
const userAuthInfo = await web3Auth.authenticateUser();
const userAuthInfo = await web3Auth.getIdentityToken();
if (userAuthInfo?.idToken) {
setToken(userAuthInfo.idToken);
}
Expand All @@ -39,5 +39,5 @@ export const useIdentityToken = () => {
}
}, [isConnected, token]);

return { loading, error, token, authenticateUser };
return { loading, error, token, getIdentityToken };
};
8 changes: 5 additions & 3 deletions packages/modal/src/ui/loginModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { applyWhiteLabelTheme, LANGUAGES, SafeEventEmitter } from "@web3auth/aut
import {
type BaseConnectorConfig,
type ChainNamespaceType,
type CONNECTED_EVENT_DATA,
CONNECTOR_EVENTS,
type IConnectorDataEvent,
log,
LOGIN_MODE,
type LoginMethodConfig,
LoginModeType,
type MetaMaskConnectorData,
type SDK_CONNECTED_EVENT_DATA,
type WALLET_CONNECTOR_TYPE,
WALLET_CONNECTORS,
type WalletConnectV2Data,
Expand Down Expand Up @@ -365,7 +366,7 @@ export class LoginModal {

this.setState({ status: MODAL_STATUS.CONNECTING });
});
listener.on(CONNECTOR_EVENTS.CONNECTED, (data: CONNECTED_EVENT_DATA) => {
listener.on(CONNECTOR_EVENTS.CONNECTED, (data: SDK_CONNECTED_EVENT_DATA) => {
log.debug("connected with connector", data);
// only show success if not being reconnected again.
if (!data.reconnected && data.loginMode === LOGIN_MODE.MODAL) {
Expand All @@ -381,8 +382,9 @@ export class LoginModal {
}
});
// TODO: send connector name in error
listener.on(CONNECTOR_EVENTS.ERRORED, (error: Web3AuthError) => {
listener.on(CONNECTOR_EVENTS.ERRORED, (error: Web3AuthError, loginMode: LoginModeType) => {
log.error("error", error, error.message);
if (loginMode === LOGIN_MODE.NO_MODAL) return;
if (error.code === 5000) {
if (this.uiConfig.displayErrorsOnModal)
this.setState({
Expand Down
8 changes: 4 additions & 4 deletions packages/modal/src/vue/composables/useIdentityToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface IUseIdentityToken {
loading: Ref<boolean>;
error: Ref<Web3AuthError | null>;
token: Ref<string | null>;
authenticateUser: () => Promise<string | null>;
getIdentityToken: () => Promise<string | null>;
}

export const useIdentityToken = (): IUseIdentityToken => {
Expand All @@ -16,12 +16,12 @@ export const useIdentityToken = (): IUseIdentityToken => {
const error = ref<Web3AuthError | null>(null);
const token = ref<string | null>(null);

const authenticateUser = async () => {
const getIdentityToken = async () => {
try {
if (!web3Auth.value) throw WalletInitializationError.notReady();
error.value = null;
loading.value = true;
const result = await web3Auth.value.authenticateUser();
const result = await web3Auth.value.getIdentityToken();
if (result?.idToken) {
token.value = result.idToken;
}
Expand All @@ -43,6 +43,6 @@ export const useIdentityToken = (): IUseIdentityToken => {
loading,
error,
token,
authenticateUser,
getIdentityToken,
};
};
4 changes: 2 additions & 2 deletions packages/no-modal/src/base/connector/baseConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import type {
ConnectorEvents,
ConnectorInitOptions,
IConnector,
IdentityTokenInfo,
IProvider,
UserAuthInfo,
UserInfo,
} from "./interfaces";

Expand Down Expand Up @@ -101,6 +101,6 @@ export abstract class BaseConnector<T> extends SafeEventEmitter<ConnectorEvents>
abstract getUserInfo(): Promise<Partial<UserInfo>>;
abstract enableMFA(params?: T): Promise<void>;
abstract manageMFA(params?: T): Promise<void>;
abstract authenticateUser(): Promise<UserAuthInfo>;
abstract getIdentityToken(): Promise<IdentityTokenInfo>;
abstract switchChain(params: { chainId: string }): Promise<void>;
}
8 changes: 3 additions & 5 deletions packages/no-modal/src/base/connector/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import type { ChainNamespaceType, ConnectorNamespaceType, CustomChainConfig } from "../chain/IChainInterface";
import type { IWeb3AuthCoreOptions } from "../core/IWeb3Auth";
import { Web3AuthError } from "../errors";
import type { LoginModeType, ProjectConfig } from "../interfaces";
import type { ProjectConfig } from "../interfaces";
import type { ProviderEvents, SafeEventEmitterProvider } from "../provider/IProvider";
import { WALLET_CONNECTOR_TYPE } from "../wallet";
import { CONNECTOR_CATEGORY, CONNECTOR_EVENTS, CONNECTOR_STATUS } from "./constants";
Expand All @@ -41,7 +41,7 @@ export interface ConnectorInitOptions {

export type CONNECTOR_STATUS_TYPE = (typeof CONNECTOR_STATUS)[keyof typeof CONNECTOR_STATUS];

export type UserAuthInfo = { idToken: string };
export type IdentityTokenInfo = { idToken: string };

export interface BaseConnectorSettings {
coreOptions: IWeb3AuthCoreOptions;
Expand Down Expand Up @@ -81,7 +81,7 @@ export interface IConnector<T> extends SafeEventEmitter {
enableMFA(params?: T): Promise<void>;
manageMFA(params?: T): Promise<void>;
switchChain(params: { chainId: string }): Promise<void>;
authenticateUser(): Promise<UserAuthInfo>;
getIdentityToken(): Promise<IdentityTokenInfo>;
cleanup?(): Promise<void>;
}

Expand All @@ -100,8 +100,6 @@ export type CONNECTED_EVENT_DATA = {
connector: WALLET_CONNECTOR_TYPE;
provider: IProvider;
reconnected: boolean;
// only available from no-modal instance.
loginMode?: LoginModeType;
};

export interface IConnectorDataEvent {
Expand Down
31 changes: 27 additions & 4 deletions packages/no-modal/src/base/core/IWeb3Auth.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { type AccountAbstractionMultiChainConfig } from "@toruslabs/ethereum-controllers";
import { type BUILD_ENV_TYPE, type LoginParams, MfaSettings, SafeEventEmitter, UX_MODE_TYPE, type WhiteLabelData } from "@web3auth/auth";
import {
type BUILD_ENV_TYPE,
type LoginParams,
MfaLevelType,
MfaSettings,
SafeEventEmitter,
UX_MODE_TYPE,
type WhiteLabelData,
} from "@web3auth/auth";
import { type WsEmbedParams } from "@web3auth/ws-embed";

import { type ChainNamespaceType, type CustomChainConfig } from "../chain/IChainInterface";
import {
CONNECTED_EVENT_DATA,
CONNECTOR_EVENTS,
type CONNECTOR_STATUS_TYPE,
ConnectorEvents,
type ConnectorFn,
type IBaseProvider,
type IConnector,
type IdentityTokenInfo,
type IProvider,
type UserAuthInfo,
type UserInfo,
type WEB3AUTH_NETWORK_TYPE,
} from "../connector";
import { Web3AuthError } from "../errors";
import { LoginModeType } from "../interfaces";
import { type IPlugin, type PluginFn } from "../plugin";
import { type WALLET_CONNECTOR_TYPE, WALLET_CONNECTORS } from "../wallet";

Expand Down Expand Up @@ -162,6 +173,11 @@ export interface IWeb3AuthCoreOptions {
* MFA settings for the auth connector
*/
mfaSettings?: MfaSettings;

/**
* MFA level for the auth connector
*/
mfaLevel?: MfaLevelType;
}

export type LoginParamMap = {
Expand All @@ -182,7 +198,7 @@ export interface IWeb3AuthCore extends SafeEventEmitter {
getPlugin(pluginName: string): IPlugin | null;
logout(options?: { cleanup: boolean }): Promise<void>;
getUserInfo(): Promise<Partial<UserInfo>>;
authenticateUser(): Promise<UserAuthInfo>;
getIdentityToken(): Promise<IdentityTokenInfo>;
switchChain(params: { chainId: string }): Promise<void>;
}

Expand All @@ -200,6 +216,13 @@ export interface IWeb3Auth extends IWeb3AuthCore {
cleanup(): Promise<void>;
}

export type Web3AuthNoModalEvents = ConnectorEvents & { [CONNECTOR_EVENTS.READY]: () => void; MODAL_VISIBILITY: (visibility: boolean) => void };
export type SDK_CONNECTED_EVENT_DATA = CONNECTED_EVENT_DATA & { loginMode: LoginModeType };

export type Web3AuthNoModalEvents = Omit<ConnectorEvents, "connected" | "errored" | "ready"> & {
[CONNECTOR_EVENTS.READY]: () => void;
[CONNECTOR_EVENTS.CONNECTED]: (data: SDK_CONNECTED_EVENT_DATA) => void;
[CONNECTOR_EVENTS.ERRORED]: (error: Web3AuthError, loginMode: LoginModeType) => void;
MODAL_VISIBILITY: (visibility: boolean) => void;
};

export type Web3AuthNoModalOptions = IWeb3AuthCoreOptions;
14 changes: 10 additions & 4 deletions packages/no-modal/src/connectors/auth-connector/authConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
network: this.coreOptions.web3AuthNetwork,
sdkMode: SDK_MODE.IFRAME,
authConnectionConfig: this.authConnectionConfig.filter((x) => !x.isDefault),
mfaSettings: this.coreOptions.mfaSettings,
});
log.debug("initializing auth connector init", this.authOptions);

Expand Down Expand Up @@ -266,7 +267,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
this.emit(CONNECTOR_EVENTS.DISCONNECTED);
}

async authenticateUser(): Promise<{ idToken: string }> {
async getIdentityToken(): Promise<{ idToken: string }> {
if (this.status !== CONNECTOR_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
const userInfo = await this.getUserInfo();
return { idToken: userInfo.idToken as string };
Expand Down Expand Up @@ -498,7 +499,12 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
this.authInstance
.postLoginInitiatedMessage(loginParams as LoginParams, nonce)
.then(resolve)
.catch(reject);
.catch((error: unknown) => {
if (error instanceof Web3AuthError) {
throw error;
}
reject(WalletLoginError.connectionError(error instanceof Error ? error.message : (error as string) || "Failed to login with social"));
});
});
}

Expand Down Expand Up @@ -587,6 +593,7 @@ export const authConnector = (params?: AuthConnectorFuncParams): ConnectorFn =>
if (whitelist) connectorSettings.originData = whitelist.signed_urls;
if (sessionTime) connectorSettings.sessionTime = sessionTime;
if (coreOptions.uiConfig?.uxMode) connectorSettings.uxMode = coreOptions.uiConfig.uxMode;

const uiConfig = deepmerge(cloneDeep(whitelabel || {}), coreOptions.uiConfig || {});
if (!uiConfig.mode) uiConfig.mode = "light";
connectorSettings.whiteLabel = uiConfig;
Expand Down Expand Up @@ -644,10 +651,9 @@ export const authConnector = (params?: AuthConnectorFuncParams): ConnectorFn =>
return new AuthConnector({
connectorSettings: finalConnectorSettings,
walletServicesSettings: finalWsSettings,
loginSettings: params?.loginSettings,
loginSettings: { ...(params?.loginSettings || {}), mfaLevel: coreOptions.mfaLevel },
coreOptions,
authConnectionConfig: projectConfig.embeddedWalletAuth,
mfaSettings: coreOptions.mfaSettings,
});
};
};
Expand Down
3 changes: 1 addition & 2 deletions packages/no-modal/src/connectors/auth-connector/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AuthConnectionConfigItem, type AuthOptions, type LoginParams, MfaSettings } from "@web3auth/auth";
import { type AuthConnectionConfigItem, type AuthOptions, type LoginParams } from "@web3auth/auth";
import { type WsEmbedParams } from "@web3auth/ws-embed";

import { type BaseConnectorSettings, type IBaseProvider } from "../../base";
Expand All @@ -14,7 +14,6 @@ export interface AuthConnectorOptions extends BaseConnectorSettings {
loginSettings?: LoginSettings;
walletServicesSettings?: WalletServicesSettings;
authConnectionConfig?: (AuthConnectionConfigItem & { isDefault?: boolean })[];
mfaSettings?: MfaSettings;
}

export {
Expand Down
Loading