Skip to content

Commit efa29b4

Browse files
refactor: return SignInOutcome for authentication methods
1 parent 3103a2d commit efa29b4

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public enum AuthView {
5151
case updatePassword
5252
}
5353

54+
public enum SignInOutcome: @unchecked Sendable {
55+
case signedIn(AuthDataResult?)
56+
}
57+
5458
@MainActor
5559
private final class AuthListenerManager {
5660
private var authStateHandle: AuthStateDidChangeListenerHandle?
@@ -156,9 +160,10 @@ public final class AuthService {
156160
)
157161
}
158162

159-
public func signIn(_ provider: AuthProviderSwift) async throws {
163+
public func signIn(_ provider: AuthProviderSwift) async throws -> SignInOutcome {
160164
let credential = try await provider.createAuthCredential()
161-
try await signIn(credentials: credential)
165+
let result = try await signIn(credentials: credential)
166+
return result
162167
}
163168

164169
// MARK: - End Provider APIs
@@ -217,12 +222,14 @@ public final class AuthService {
217222
}
218223
}
219224

220-
public func handleAutoUpgradeAnonymousUser(credentials: AuthCredential) async throws {
225+
public func handleAutoUpgradeAnonymousUser(credentials: AuthCredential) async throws -> SignInOutcome {
221226
if currentUser == nil {
222227
throw AuthServiceError.noCurrentUser
223228
}
224229
do {
225-
try await currentUser?.link(with: credentials)
230+
let result = try await currentUser?.link(with: credentials)
231+
updateAuthenticationState()
232+
return .signedIn(result)
226233
} catch let error as NSError {
227234
if error.code == AuthErrorCode.emailAlreadyInUse.rawValue {
228235
let context = AccountMergeConflictContext(
@@ -237,16 +244,17 @@ public final class AuthService {
237244
}
238245
}
239246

240-
public func signIn(credentials: AuthCredential) async throws {
247+
public func signIn(credentials: AuthCredential) async throws -> SignInOutcome {
241248
authenticationState = .authenticating
242249
do {
243250
if shouldHandleAnonymousUpgrade {
244-
try await handleAutoUpgradeAnonymousUser(credentials: credentials)
251+
return try await handleAutoUpgradeAnonymousUser(credentials: credentials)
245252
} else {
246253
let result = try await auth.signIn(with: credentials)
247254
signedInCredential = result.credential ?? credentials
255+
updateAuthenticationState()
256+
return .signedIn(result)
248257
}
249-
updateAuthenticationState()
250258
} catch {
251259
authenticationState = .unauthenticated
252260
errorMessage = string.localizedErrorMessage(
@@ -333,23 +341,24 @@ public extension AuthService {
333341
return self
334342
}
335343

336-
func signIn(withEmail email: String, password: String) async throws {
344+
func signIn(withEmail email: String, password: String) async throws -> SignInOutcome {
337345
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
338-
try await signIn(credentials: credential)
346+
return try await signIn(credentials: credential)
339347
}
340348

341-
func createUser(withEmail email: String, password: String) async throws {
349+
func createUser(withEmail email: String, password: String) async throws -> SignInOutcome {
342350
authenticationState = .authenticating
343351

344352
do {
345353
if shouldHandleAnonymousUpgrade {
346354
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
347-
try await handleAutoUpgradeAnonymousUser(credentials: credential)
355+
return try await handleAutoUpgradeAnonymousUser(credentials: credential)
348356
} else {
349357
let result = try await auth.createUser(withEmail: email, password: password)
350358
signedInCredential = result.credential
359+
updateAuthenticationState()
360+
return .signedIn(result)
351361
}
352-
updateAuthenticationState()
353362
} catch {
354363
authenticationState = .unauthenticated
355364
errorMessage = string.localizedErrorMessage(

0 commit comments

Comments
 (0)