7
7
8
8
import Foundation
9
9
import Amplify
10
+ import AWSPluginsCore
10
11
11
12
class FetchAuthSessionOperationHelper {
12
13
13
14
typealias FetchAuthSessionCompletion = ( Result < AuthSession , AuthError > ) -> Void
15
+ var environment : Environment ? = nil
14
16
15
17
func fetch( _ authStateMachine: AuthStateMachine ,
16
18
forceRefresh: Bool = false ) async throws -> AuthSession {
@@ -98,7 +100,7 @@ class FetchAuthSessionOperationHelper {
98
100
case . sessionEstablished( let credentials) :
99
101
return credentials. cognitoSession
100
102
case . error( let authorizationError) :
101
- return try sessionResultWithError (
103
+ return try await sessionResultWithError (
102
104
authorizationError,
103
105
authenticationState: authenticationState)
104
106
default : continue
@@ -111,7 +113,7 @@ class FetchAuthSessionOperationHelper {
111
113
func sessionResultWithError(
112
114
_ error: AuthorizationError ,
113
115
authenticationState: AuthenticationState
114
- ) throws -> AuthSession {
116
+ ) async throws -> AuthSession {
115
117
log. verbose ( " Received fetch auth session error - \( error) " )
116
118
117
119
var isSignedIn = false
@@ -129,8 +131,10 @@ class FetchAuthSessionOperationHelper {
129
131
authError = fetchError. authError
130
132
}
131
133
case . sessionExpired( let error) :
134
+ await setRefreshTokenExpiredInSignedInData ( )
132
135
let session = AuthCognitoSignedInSessionHelper . makeExpiredSignedInSession (
133
136
underlyingError: error)
137
+
134
138
return session
135
139
default :
136
140
break
@@ -143,6 +147,43 @@ class FetchAuthSessionOperationHelper {
143
147
cognitoTokensResult: . failure( authError) )
144
148
return session
145
149
}
150
+
151
+ func setRefreshTokenExpiredInSignedInData( ) async {
152
+ let credentialStoreClient = ( environment as? AuthEnvironment ) ? . credentialsClient
153
+ do {
154
+ let data = try await credentialStoreClient? . fetchData (
155
+ type: . amplifyCredentials
156
+ )
157
+ guard case . amplifyCredentials( var credentials) = data else {
158
+ return
159
+ }
160
+
161
+ // Update SignedInData based on credential type
162
+ switch credentials {
163
+ case . userPoolOnly( var signedInData) :
164
+ signedInData. isRefreshTokenExpired = true
165
+ credentials = . userPoolOnly( signedInData: signedInData)
166
+
167
+ case . userPoolAndIdentityPool( var signedInData, let identityId, let awsCredentials) :
168
+ signedInData. isRefreshTokenExpired = true
169
+ credentials = . userPoolAndIdentityPool(
170
+ signedInData: signedInData,
171
+ identityID: identityId,
172
+ credentials: awsCredentials)
173
+
174
+ case . identityPoolOnly, . identityPoolWithFederation, . noCredentials:
175
+ return
176
+ }
177
+
178
+ try await credentialStoreClient? . storeData ( data: . amplifyCredentials( credentials) )
179
+ } catch KeychainStoreError . itemNotFound {
180
+ let logger = ( environment as? LoggerProvider ) ? . logger
181
+ logger? . info ( " No existing credentials found. " )
182
+ } catch {
183
+ let logger = ( environment as? LoggerProvider ) ? . logger
184
+ logger? . error ( " Unable to update credentials with error: \( error) " )
185
+ }
186
+ }
146
187
}
147
188
148
189
extension FetchAuthSessionOperationHelper : DefaultLogger { }
0 commit comments