Skip to content

Commit 143a8ea

Browse files
improve initialization of web3Auth
1 parent 962da66 commit 143a8ea

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

ios/ios-aggregate-verifier-example/ios-aggregate-example/ViewModel.swift

+32-44
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,47 @@ import Foundation
22
import Web3Auth
33

44
class ViewModel: ObservableObject {
5-
var web3Auth: Web3Auth?
5+
lazy var web3Auth: Web3Auth? = nil
66
@Published var loggedIn: Bool = false
77
@Published var user: Web3AuthState?
88
@Published var isLoading = false
99
@Published var navigationTitle: String = ""
1010
private var clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"
1111
private var network: Network = .sapphire_mainnet
12+
1213
func setup() async {
1314
guard web3Auth == nil else { return }
1415
await MainActor.run(body: {
1516
isLoading = true
1617
navigationTitle = "Loading"
1718
})
18-
web3Auth = await Web3Auth(.init(clientId: clientId, network: network))
19+
web3Auth = await Web3Auth(W3AInitParams(
20+
clientId: clientId, network: network,
21+
loginConfig: [
22+
TypeOfLogin.google.rawValue:
23+
.init(
24+
verifier: "aggregate-sapphire",
25+
typeOfLogin: .google,
26+
name: "Web3Auth-Aggregate-Verifier-Google-Example",
27+
clientId: "519228911939-cri01h55lsjbsia1k7ll6qpalrus75ps.apps.googleusercontent.com",
28+
verifierSubIdentifier: "w3a-google"
29+
)
30+
],
31+
whiteLabel: W3AWhiteLabelData(
32+
appName: "Web3Auth Stub",
33+
logoLight: "https://images.web3auth.io/web3auth-logo-w.svg",
34+
logoDark: "https://images.web3auth.io/web3auth-logo-w.svg",
35+
defaultLanguage: .en, // en, de, ja, ko, zh, es, fr, pt, nl
36+
mode: .dark,
37+
theme: ["primary": "#d53f8c"]
38+
),
39+
mfaSettings: MfaSettings(
40+
deviceShareFactor: MfaSetting(enable: true, priority: 1),
41+
backUpShareFactor: MfaSetting(enable: true, priority: 2),
42+
socialBackupFactor: MfaSetting(enable: true, priority: 3),
43+
passwordFactor: MfaSetting(enable: true, priority: 4)
44+
),
45+
))
1946
await MainActor.run(body: {
2047
if self.web3Auth?.state != nil {
2148
user = web3Auth?.state
@@ -29,33 +56,7 @@ class ViewModel: ObservableObject {
2956
func loginWithGoogle() {
3057
Task{
3158
do {
32-
let result = try await Web3Auth(.init(
33-
clientId: clientId,
34-
network: network,
35-
loginConfig: [
36-
TypeOfLogin.google.rawValue:
37-
.init(
38-
verifier: "aggregate-sapphire",
39-
typeOfLogin: .google,
40-
name: "Web3Auth-Aggregate-Verifier-Google-Example",
41-
clientId: "519228911939-cri01h55lsjbsia1k7ll6qpalrus75ps.apps.googleusercontent.com",
42-
verifierSubIdentifier: "w3a-google"
43-
)
44-
],
45-
whiteLabel: W3AWhiteLabelData(
46-
appName: "Web3Auth Stub",
47-
logoLight: "https://images.web3auth.io/web3auth-logo-w.svg",
48-
logoDark: "https://images.web3auth.io/web3auth-logo-w.svg",
49-
defaultLanguage: .en, // en, de, ja, ko, zh, es, fr, pt, nl
50-
mode: .dark,
51-
theme: ["primary": "#d53f8c"]),
52-
mfaSettings: MfaSettings(
53-
deviceShareFactor: MfaSetting(enable: true, priority: 1),
54-
backUpShareFactor: MfaSetting(enable: true, priority: 2),
55-
socialBackupFactor: MfaSetting(enable: true, priority: 3),
56-
passwordFactor: MfaSetting(enable: true, priority: 4)
57-
)
58-
)).login(
59+
let result = try await web3Auth?.login(
5960
W3ALoginParams(
6061
loginProvider: .GOOGLE,
6162
dappShare: nil,
@@ -76,20 +77,7 @@ class ViewModel: ObservableObject {
7677
func loginWithGitHub() {
7778
Task{
7879
do {
79-
let result = try await Web3Auth(.init(
80-
clientId: clientId,
81-
network: network,
82-
loginConfig: [
83-
TypeOfLogin.jwt.rawValue:
84-
.init(
85-
verifier: "aggregate-sapphire",
86-
typeOfLogin: .jwt,
87-
name: "Web3Auth-Aggregate-Verifier-GitHub-Example",
88-
clientId: "hiLqaop0amgzCC0AXo4w0rrG9abuJTdu",
89-
verifierSubIdentifier: "w3a-a0-github"
90-
)
91-
]
92-
)).login(
80+
let result = try await web3Auth?.login(
9381
W3ALoginParams(
9482
loginProvider: .JWT,
9583
dappShare: nil,
@@ -108,7 +96,7 @@ class ViewModel: ObservableObject {
10896
}
10997

11098
func logout() async throws {
111-
try await Web3Auth(W3AInitParams(clientId: clientId, network: network)).logout()
99+
try await web3Auth?.logout()
112100
await MainActor.run(body: {
113101
loggedIn = false
114102
})

0 commit comments

Comments
 (0)