-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathAmplifyAuthTask.swift
More file actions
62 lines (51 loc) · 1.9 KB
/
Copy pathAmplifyAuthTask.swift
File metadata and controls
62 lines (51 loc) · 1.9 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
import Amplify
#if canImport(UIKit)
import UIKit
#endif
protocol AmplifyAuthTask {
associatedtype Success
associatedtype Request
associatedtype Failure: AmplifyError
typealias AmplifyAuthTaskResult = Result<Success, Failure>
var value: Success { get async throws }
var eventName: HubPayloadEventName { get }
func execute() async throws -> Success
func dispatch(result: AmplifyAuthTaskResult)
}
extension AmplifyAuthTask where Self: DefaultLogger {
var value: Success {
get async throws {
do {
log.info("Starting execution for \(eventName)")
#if canImport(UIKit)
guard await UIApplication.shared.isProtectedDataAvailable else {
throw AuthError.configuration(
AuthPluginErrorConstants.protectedDataUnavailableError.errorDescription,
AuthPluginErrorConstants.protectedDataUnavailableError.recoverySuggestion,
AWSCognitoAuthError.protectedDataUnavailable)
}
#endif
let valueReturned = try await execute()
log.info("Successfully completed execution for \(eventName) with result:\n\(valueReturned)")
dispatch(result: .success(valueReturned))
return valueReturned
} catch let error as Failure {
log.error("Failed execution for \(eventName) with error:\n\(error)")
dispatch(result: .failure(error))
throw error
}
}
}
func dispatch(result: AmplifyAuthTaskResult) {
let channel = HubChannel(from: .auth)
let payload = HubPayload(eventName: eventName, context: nil, data: result)
Amplify.Hub.dispatch(to: channel, payload: payload)
}
}