-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathAccountService.swift
More file actions
37 lines (32 loc) · 1.06 KB
/
AccountService.swift
File metadata and controls
37 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import AuthenticationServices
import FirebaseAuth
extension NSError {
var requiresReauthentication: Bool {
domain == AuthErrorDomain && code == AuthErrorCode.requiresRecentLogin.rawValue
}
var credentialAlreadyInUse: Bool {
domain == AuthErrorDomain && code == AuthErrorCode.credentialAlreadyInUse.rawValue
}
}
enum AuthenticationToken {
case apple(ASAuthorizationAppleIDCredential, String)
case firebase(String)
}
protocol AuthenticatedOperation {
func callAsFunction(on user: User) async throws
func reauthenticate() async throws -> AuthenticationToken
}
extension AuthenticatedOperation {
func callAsFunction(on _: User,
_ performOperation: () async throws -> Void) async throws {
do {
try await performOperation()
} catch let error as NSError where error.requiresReauthentication {
let token = try await reauthenticate()
try await performOperation()
} catch AuthServiceError.reauthenticationRequired {
let token = try await reauthenticate()
try await performOperation()
}
}
}