@@ -79,6 +79,40 @@ import Foundation
79
79
fatalError ( " Internal Auth error: failed to get user enrolling in MultiFactor " )
80
80
}
81
81
82
+ let request = Self . enrollmentFinalizationRequest (
83
+ with: assertion,
84
+ displayName: displayName,
85
+ user: user,
86
+ auth: auth
87
+ )
88
+
89
+ Task {
90
+ do {
91
+ let response = try await auth. backend. call ( with: request)
92
+ let user = try await auth. completeSignIn ( withAccessToken: response. idToken,
93
+ accessTokenExpirationDate: nil ,
94
+ refreshToken: response. refreshToken,
95
+ anonymous: false )
96
+ try auth. updateCurrentUser ( user, byForce: false , savingToDisk: true )
97
+ if let completion {
98
+ DispatchQueue . main. async {
99
+ completion ( nil )
100
+ }
101
+ }
102
+ } catch {
103
+ if let completion {
104
+ DispatchQueue . main. async {
105
+ completion ( error)
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ private static func enrollmentFinalizationRequest( with assertion: MultiFactorAssertion ,
113
+ displayName: String ? ,
114
+ user: User ,
115
+ auth: Auth ) -> FinalizeMFAEnrollmentRequest {
82
116
var request : FinalizeMFAEnrollmentRequest ? = nil
83
117
if assertion. factorID == PhoneMultiFactorInfo . TOTPMultiFactorID {
84
118
guard let totpAssertion = assertion as? TOTPMultiFactorAssertion else {
@@ -121,30 +155,10 @@ import Foundation
121
155
122
156
guard let request else {
123
157
// Assertion is not a phone assertion or TOTP assertion.
124
- return
158
+ fatalError ( " Internal Error: Unsupported assertion with factor ID: \( assertion . factorID ) . " )
125
159
}
126
160
127
- Task {
128
- do {
129
- let response = try await auth. backend. call ( with: request)
130
- let user = try await auth. completeSignIn ( withAccessToken: response. idToken,
131
- accessTokenExpirationDate: nil ,
132
- refreshToken: response. refreshToken,
133
- anonymous: false )
134
- try auth. updateCurrentUser ( user, byForce: false , savingToDisk: true )
135
- if let completion {
136
- DispatchQueue . main. async {
137
- completion ( nil )
138
- }
139
- }
140
- } catch {
141
- if let completion {
142
- DispatchQueue . main. async {
143
- completion ( error)
144
- }
145
- }
146
- }
147
- }
161
+ return request
148
162
}
149
163
150
164
/// Enrolls a second factor as identified by the `MultiFactorAssertion` parameter for the
0 commit comments