|
8 | 8 | import Foundation
|
9 | 9 | import Amplify
|
10 | 10 |
|
11 |
| -class FetchAuthSessionOperationHelper: DefaultLogger { |
| 11 | +class FetchAuthSessionOperationHelper { |
12 | 12 |
|
13 | 13 | typealias FetchAuthSessionCompletion = (Result<AuthSession, AuthError>) -> Void
|
14 | 14 |
|
@@ -108,85 +108,41 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
|
108 | 108 | "Auth plugin is in an invalid state")
|
109 | 109 | }
|
110 | 110 |
|
111 |
| - func sessionResultWithError(_ error: AuthorizationError, |
112 |
| - authenticationState: AuthenticationState) |
113 |
| - throws -> AuthSession { |
114 |
| - log.verbose("Received error - \(error)") |
| 111 | + func sessionResultWithError( |
| 112 | + _ error: AuthorizationError, |
| 113 | + authenticationState: AuthenticationState |
| 114 | + ) throws -> AuthSession { |
| 115 | + log.verbose("Received fetch auth session error - \(error)") |
115 | 116 |
|
116 | 117 | var isSignedIn = false
|
117 |
| - if case .signedIn = authenticationState { |
118 |
| - isSignedIn = true |
119 |
| - } |
120 |
| - switch error { |
121 |
| - case .sessionError(let fetchError, let credentials): |
122 |
| - return try sessionResultWithFetchError(fetchError, |
123 |
| - authenticationState: authenticationState, |
124 |
| - existingCredentials: credentials) |
125 |
| - case .sessionExpired(let error): |
126 |
| - let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( |
127 |
| - underlyingError: error) |
128 |
| - return session |
129 |
| - default: |
130 |
| - let message = "Unknown error occurred" |
131 |
| - let error = AuthError.unknown(message) |
132 |
| - let session = AWSAuthCognitoSession(isSignedIn: isSignedIn, |
133 |
| - identityIdResult: .failure(error), |
134 |
| - awsCredentialsResult: .failure(error), |
135 |
| - cognitoTokensResult: .failure(error)) |
136 |
| - return session |
137 |
| - } |
138 |
| - } |
139 |
| - |
140 |
| - func sessionResultWithFetchError(_ error: FetchSessionError, |
141 |
| - authenticationState: AuthenticationState, |
142 |
| - existingCredentials: AmplifyCredentials) |
143 |
| - throws -> AuthSession { |
| 118 | + var authError: AuthError = error.authError |
144 | 119 |
|
145 |
| - var isSignedIn = false |
146 | 120 | if case .signedIn = authenticationState {
|
147 | 121 | isSignedIn = true
|
148 | 122 | }
|
149 | 123 |
|
150 | 124 | switch error {
|
151 |
| - |
152 |
| - case .notAuthorized, .noCredentialsToRefresh: |
153 |
| - if !isSignedIn { |
| 125 | + case .sessionError(let fetchError, _): |
| 126 | + if (fetchError == .notAuthorized || fetchError == .noCredentialsToRefresh) && !isSignedIn { |
154 | 127 | return AuthCognitoSignedOutSessionHelper.makeSessionWithNoGuestAccess()
|
155 |
| - } |
156 |
| - |
157 |
| - case .service(let error): |
158 |
| - var authError: AuthError |
159 |
| - if let convertedAuthError = (error as? AuthErrorConvertible)?.authError { |
160 |
| - authError = convertedAuthError |
161 | 128 | } else {
|
162 |
| - authError = AuthError.service( |
163 |
| - "Unknown service error occurred", |
164 |
| - "See the attached error for more details", |
165 |
| - error) |
| 129 | + authError = fetchError.authError |
166 | 130 | }
|
167 |
| - let session = AWSAuthCognitoSession( |
168 |
| - isSignedIn: isSignedIn, |
169 |
| - identityIdResult: .failure(authError), |
170 |
| - awsCredentialsResult: .failure(authError), |
171 |
| - cognitoTokensResult: .failure(authError)) |
| 131 | + case .sessionExpired(let error): |
| 132 | + let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( |
| 133 | + underlyingError: error) |
172 | 134 | return session
|
173 |
| - default: break |
174 |
| - |
| 135 | + default: |
| 136 | + break |
175 | 137 | }
|
176 |
| - let message = "Unknown error occurred" |
177 |
| - let error = AuthError.unknown(message) |
178 |
| - let session = AWSAuthCognitoSession(isSignedIn: isSignedIn, |
179 |
| - identityIdResult: .failure(error), |
180 |
| - awsCredentialsResult: .failure(error), |
181 |
| - cognitoTokensResult: .failure(error)) |
182 |
| - return session |
183 |
| - } |
184 | 138 |
|
185 |
| - public static var log: Logger { |
186 |
| - Amplify.Logging.logger(forCategory: CategoryType.auth.displayName, forNamespace: String(describing: self)) |
187 |
| - } |
188 |
| - |
189 |
| - public var log: Logger { |
190 |
| - Self.log |
| 139 | + let session = AWSAuthCognitoSession( |
| 140 | + isSignedIn: isSignedIn, |
| 141 | + identityIdResult: .failure(authError), |
| 142 | + awsCredentialsResult: .failure(authError), |
| 143 | + cognitoTokensResult: .failure(authError)) |
| 144 | + return session |
191 | 145 | }
|
192 | 146 | }
|
| 147 | + |
| 148 | +extension FetchAuthSessionOperationHelper: DefaultLogger { } |
0 commit comments