Skip to content

Commit 994250c

Browse files
authored
Merge pull request #2088 from Web3Auth/feat/sdk-v10-auth-connector
provide more options for authConnector
2 parents 91f7481 + 5b5520c commit 994250c

File tree

8 files changed

+46
-79
lines changed

8 files changed

+46
-79
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ const options = computed((): Web3AuthOptions => {
3838
3939
// Wallet services settings
4040
let walletServicesConfig: Web3AuthOptions["walletServicesConfig"] = {
41-
// walletUrls: {
42-
// production: {
43-
// url: "http://localhost:4050",
44-
// }
45-
// }
46-
};
41+
// walletUrls: {
42+
// production: {
43+
// url: "http://localhost:4050",
44+
// }
45+
// }
46+
};
4747
if (formData.walletPlugin.enable) {
4848
const { confirmationStrategy } = formData.walletPlugin;
4949
walletServicesConfig = {
@@ -125,7 +125,7 @@ const getExternalAdapterByName = (name: string): ConnectorFn[] => {
125125
case "coinbase":
126126
return [coinbaseConnector()];
127127
case "wallet-connect-v2":
128-
return [walletConnectV2Connector({ projectId: "d3c63f19f9582f8ba48e982057eb096b" })];
128+
return [walletConnectV2Connector({ walletConnectInitOptions: { projectId: "d3c63f19f9582f8ba48e982057eb096b" } })];
129129
default:
130130
return [];
131131
}

packages/modal/src/ui/interfaces.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import type { SafeEventEmitter, UX_MODE_TYPE, WhiteLabelData } from "@web3auth/auth";
1+
import type { SafeEventEmitter } from "@web3auth/auth";
22
import {
33
BaseConnectorConfig,
44
ChainNamespaceType,
55
LoginMethodConfig,
6+
UIConfig as CoreUIConfig,
67
WALLET_CONNECTOR_TYPE,
78
WalletRegistry,
89
WalletRegistryItem,
910
Web3AuthNoModalEvents,
1011
} from "@web3auth/no-modal";
1112

1213
// capture whitelabel only once
13-
export interface UIConfig extends WhiteLabelData {
14+
export interface UIConfig extends CoreUIConfig {
1415
/**
1516
* order of how login methods are shown
1617
*
@@ -48,11 +49,6 @@ export interface UIConfig extends WhiteLabelData {
4849
primaryButton?: "externalLogin" | "socialLogin" | "emailLogin";
4950

5051
connectorListener: SafeEventEmitter<Web3AuthNoModalEvents>;
51-
52-
/**
53-
* UX Mode for the auth connector
54-
*/
55-
uxMode?: UX_MODE_TYPE;
5652
}
5753

5854
export interface LoginModalProps extends UIConfig {

packages/no-modal/src/base/core/IWeb3Auth.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type AccountAbstractionConfig } from "@toruslabs/ethereum-controllers";
2-
import { SafeEventEmitter, type WhiteLabelData } from "@web3auth/auth";
2+
import { SafeEventEmitter, UX_MODE_TYPE, type WhiteLabelData } from "@web3auth/auth";
33
import { type WsEmbedParams } from "@web3auth/ws-embed";
44

55
import { type CustomChainConfig } from "../chain/IChainInterface";
@@ -36,6 +36,13 @@ export type WalletServicesConfig = Omit<
3636
modalZIndex?: number;
3737
};
3838

39+
export interface UIConfig extends WhiteLabelData {
40+
/**
41+
* UX Mode for the auth connector
42+
*/
43+
uxMode?: UX_MODE_TYPE;
44+
}
45+
3946
export interface IWeb3AuthCoreOptions {
4047
/**
4148
* Client id for web3auth.
@@ -84,7 +91,7 @@ export interface IWeb3AuthCoreOptions {
8491
/**
8592
* WhiteLabel options for web3auth
8693
*/
87-
uiConfig?: WhiteLabelData;
94+
uiConfig?: UIConfig;
8895

8996
/**
9097
* Account abstraction config for your chain namespace

packages/no-modal/src/connectors/auth-connector/authConnector.ts

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type EthereumProviderConfig } from "@toruslabs/ethereum-controllers";
2-
import { Auth, LOGIN_PROVIDER, LoginParams, SUPPORTED_KEY_CURVES, UX_MODE, UX_MODE_TYPE, WEB3AUTH_NETWORK } from "@web3auth/auth";
2+
import { Auth, LOGIN_PROVIDER, LoginParams, SUPPORTED_KEY_CURVES, UX_MODE, WEB3AUTH_NETWORK } from "@web3auth/auth";
33
import { type default as WsEmbed } from "@web3auth/ws-embed";
44
import deepmerge from "deepmerge";
55

@@ -27,7 +27,6 @@ import {
2727
Web3AuthError,
2828
} from "@/core/base";
2929

30-
import { getAuthDefaultOptions } from "./config";
3130
import type { AuthConnectorOptions, LoginConfig, LoginSettings, PrivateKeyProvider, WalletServicesSettings } from "./interface";
3231

3332
export type AuthLoginParams = LoginParams & {
@@ -59,15 +58,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
5958
constructor(params: AuthConnectorOptions) {
6059
super(params);
6160

62-
// set auth options
63-
const defaultOptions = getAuthDefaultOptions();
64-
log.info("setting connector settings", params.connectorSettings);
65-
this.authOptions = deepmerge.all([
66-
defaultOptions.connectorSettings,
67-
this.authOptions || {},
68-
params.connectorSettings || {},
69-
]) as AuthConnectorOptions["connectorSettings"];
70-
61+
this.authOptions = params.connectorSettings;
7162
this.loginSettings = params.loginSettings || { loginProvider: "" };
7263
this.wsSettings = params.walletServicesSettings || {};
7364
}
@@ -98,7 +89,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
9889
const isRedirectResult = this.authOptions.uxMode === UX_MODE.REDIRECT;
9990

10091
this.authOptions = { ...this.authOptions, replaceUrlOnRedirect: isRedirectResult, useCoreKitKey: this.coreOptions.useCoreKitKey };
101-
const web3AuthNetwork = this.authOptions.network || this.coreOptions.web3AuthNetwork || WEB3AUTH_NETWORK.SAPPHIRE_MAINNET;
92+
const web3AuthNetwork = this.coreOptions.web3AuthNetwork || WEB3AUTH_NETWORK.SAPPHIRE_MAINNET;
10293
this.authInstance = new Auth({
10394
...this.authOptions,
10495
clientId: this.coreOptions.clientId,
@@ -114,7 +105,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
114105
case CHAIN_NAMESPACES.SOLANA: {
115106
const { default: WsEmbed } = await import("@web3auth/ws-embed");
116107
this.wsEmbedInstance = new WsEmbed({
117-
web3AuthClientId: this.coreOptions.clientId || "",
108+
web3AuthClientId: this.coreOptions.clientId,
118109
web3AuthNetwork,
119110
modalZIndex: this.wsSettings.modalZIndex,
120111
});
@@ -357,15 +348,10 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
357348
}
358349
}
359350

360-
export const authConnector = (params?: { uxMode?: UX_MODE_TYPE }): ConnectorFn => {
351+
export const authConnector = (params?: AuthConnectorOptions): ConnectorFn => {
361352
return ({ projectConfig, coreOptions }: ConnectorParams) => {
362-
const connectorSettings: AuthConnectorOptions["connectorSettings"] = {
363-
network: coreOptions.web3AuthNetwork || WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
364-
clientId: coreOptions.clientId,
365-
uxMode: params?.uxMode || UX_MODE.POPUP,
366-
};
367-
368-
// sms otp config
353+
// Connector settings
354+
const connectorSettings: AuthConnectorOptions["connectorSettings"] = { uxMode: UX_MODE.POPUP };
369355
const { sms_otp_enabled: smsOtpEnabled, whitelist } = projectConfig;
370356
if (smsOtpEnabled !== undefined) {
371357
connectorSettings.loginConfig = {
@@ -377,17 +363,12 @@ export const authConnector = (params?: { uxMode?: UX_MODE_TYPE }): ConnectorFn =
377363
} as LoginConfig[keyof LoginConfig],
378364
};
379365
}
380-
381-
// whitelist config
382-
if (whitelist) {
383-
connectorSettings.originData = whitelist.signed_urls;
384-
}
385-
386-
// whitelabel config
387-
const { whitelabel } = projectConfig;
388-
const uiConfig = deepmerge(cloneDeep(whitelabel || {}), coreOptions.uiConfig || {});
366+
if (whitelist) connectorSettings.originData = whitelist.signed_urls;
367+
if (coreOptions.uiConfig?.uxMode) connectorSettings.uxMode = coreOptions.uiConfig.uxMode;
368+
const uiConfig = deepmerge(cloneDeep(projectConfig?.whitelabel || {}), coreOptions.uiConfig || {});
389369
if (!uiConfig.mode) uiConfig.mode = "light";
390370
connectorSettings.whiteLabel = uiConfig;
371+
const finalConnectorSettings = deepmerge(params?.connectorSettings || {}, connectorSettings) as AuthConnectorOptions["connectorSettings"];
391372

392373
// WS settings
393374
const finalWsSettings: WalletServicesSettings = {
@@ -400,12 +381,12 @@ export const authConnector = (params?: { uxMode?: UX_MODE_TYPE }): ConnectorFn =
400381
enableLogging: coreOptions.enableLogging,
401382
};
402383

403-
const connectorOptions: AuthConnectorOptions = {
404-
connectorSettings,
384+
return new AuthConnector({
385+
connectorSettings: finalConnectorSettings,
405386
walletServicesSettings: finalWsSettings,
387+
loginSettings: params?.loginSettings,
406388
coreOptions,
407-
};
408-
return new AuthConnector(connectorOptions);
389+
});
409390
};
410391
};
411392

packages/no-modal/src/connectors/auth-connector/config.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from "./authConnector";
2-
export * from "./config";
32
export * from "./interface";

packages/no-modal/src/connectors/auth-connector/interface.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import { type BaseConnectorSettings, type IBaseProvider } from "@/core/base";
55

66
export type LoginSettings = Partial<LoginParams> & Partial<BaseRedirectParams>;
77

8-
type MakeOptional<Type, Key extends keyof Type> = Omit<Type, Key> & Partial<Pick<Type, Key>>;
9-
108
export type PrivateKeyProvider = IBaseProvider<string>;
119

1210
export type WalletServicesSettings = WsEmbedParams & { modalZIndex?: number };
1311

1412
export interface AuthConnectorOptions extends BaseConnectorSettings {
15-
connectorSettings?: MakeOptional<AuthOptions, "clientId" | "network">;
13+
connectorSettings?: Omit<AuthOptions, "clientId" | "network">;
1614
loginSettings?: LoginSettings;
1715
walletServicesSettings?: WalletServicesSettings;
1816
}

packages/no-modal/src/connectors/wallet-connect-v2-connector/walletConnectV2Connector.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from "@/core/base";
3737

3838
import { getWalletConnectV2Settings } from "./config";
39-
import { WalletConnectV2ConnectorOptions } from "./interface";
39+
import { IConnectorSettings, WalletConnectV2ConnectorOptions } from "./interface";
4040
import { WalletConnectV2Provider } from "./WalletConnectV2Provider";
4141

4242
class WalletConnectV2Connector extends BaseConnector<void> {
@@ -397,19 +397,23 @@ class WalletConnectV2Connector extends BaseConnector<void> {
397397
}
398398
}
399399

400-
export const walletConnectV2Connector = (params?: { projectId: string }): ConnectorFn => {
400+
export const walletConnectV2Connector = (params?: IConnectorSettings): ConnectorFn => {
401401
return ({ projectConfig, coreOptions }: ConnectorParams) => {
402-
let { projectId } = params || {};
402+
let { projectId } = params?.walletConnectInitOptions || {};
403403

404+
// use project config if projectId is not set
404405
if (projectConfig) {
405406
const { wallet_connect_enabled: walletConnectEnabled, wallet_connect_project_id: walletConnectProjectId } = projectConfig;
406-
if (walletConnectEnabled && walletConnectProjectId) projectId = walletConnectProjectId;
407+
if (walletConnectEnabled && walletConnectProjectId && !projectId) projectId = walletConnectProjectId;
407408
}
408409

410+
const connectorSettings = {
411+
...params,
412+
walletConnectInitOptions: { ...params?.walletConnectInitOptions, projectId },
413+
};
414+
409415
return new WalletConnectV2Connector({
410-
connectorSettings: {
411-
walletConnectInitOptions: { projectId },
412-
},
416+
connectorSettings,
413417
coreOptions,
414418
});
415419
};

0 commit comments

Comments
 (0)