@@ -10,7 +10,7 @@ import Amplify
10
10
11
11
class FetchAuthSessionOperationHelper : DefaultLogger {
12
12
13
- static let expiryBufferInSeconds = TimeInterval . seconds ( 2 * 60 )
13
+
14
14
15
15
typealias FetchAuthSessionCompletion = ( Result < AuthSession , AuthError > ) -> Void
16
16
@@ -35,8 +35,8 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
35
35
36
36
case . sessionEstablished( let credentials) :
37
37
log. verbose ( " Session exists, checking validity " )
38
- return try await postAuthSessionEvent (
39
- forCredential : credentials,
38
+ return try await refreshIfRequired (
39
+ existingCredentials : credentials,
40
40
authStateMachine: authStateMachine,
41
41
forceRefresh: forceRefresh)
42
42
@@ -46,8 +46,8 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
46
46
let session = AuthCognitoSignedInSessionHelper . makeExpiredSignedInSession ( )
47
47
return session
48
48
} else if case . sessionError( _, let credentials) = error {
49
- return try await postAuthSessionEvent (
50
- forCredential : credentials,
49
+ return try await refreshIfRequired (
50
+ existingCredentials : credentials,
51
51
authStateMachine: authStateMachine,
52
52
forceRefresh: forceRefresh)
53
53
} else {
@@ -62,60 +62,28 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
62
62
}
63
63
}
64
64
65
- func postAuthSessionEvent (
66
- forCredential credentials: AmplifyCredentials ,
65
+ func refreshIfRequired (
66
+ existingCredentials credentials: AmplifyCredentials ,
67
67
authStateMachine: AuthStateMachine ,
68
68
forceRefresh: Bool ) async throws -> AuthSession {
69
- switch credentials {
70
-
71
- case . userPoolOnly( signedInData: let data) :
72
- if data. cognitoUserPoolTokens. doesExpire ( in: Self . expiryBufferInSeconds) ||
73
- forceRefresh {
74
- let event = AuthorizationEvent ( eventType: . refreshSession( forceRefresh) )
75
- await authStateMachine. send ( event)
76
- return try await listenForSession ( authStateMachine: authStateMachine)
77
- } else {
78
- return credentials. cognitoSession
79
- }
80
-
81
- case . identityPoolOnly( identityID: _, credentials: let awsCredentials) :
82
- if awsCredentials. doesExpire ( in: Self . expiryBufferInSeconds) ||
83
- forceRefresh {
84
- let event = AuthorizationEvent ( eventType: . refreshSession( forceRefresh) )
85
- await authStateMachine. send ( event)
86
- return try await listenForSession ( authStateMachine: authStateMachine)
87
- } else {
88
- return credentials. cognitoSession
89
- }
90
69
91
- case . userPoolAndIdentityPool( signedInData: let data,
92
- identityID: _,
93
- credentials: let awsCredentials) :
94
- if data. cognitoUserPoolTokens. doesExpire ( in: Self . expiryBufferInSeconds) ||
95
- awsCredentials. doesExpire ( in: Self . expiryBufferInSeconds) ||
96
- forceRefresh {
97
- let event = AuthorizationEvent ( eventType: . refreshSession( forceRefresh) )
98
- await authStateMachine. send ( event)
99
- return try await listenForSession ( authStateMachine: authStateMachine)
70
+ var event : AuthorizationEvent
71
+ if forceRefresh || !credentials. areValid ( ) {
72
+ if case . identityPoolWithFederation(
73
+ let federatedToken,
74
+ let identityId,
75
+ _
76
+ ) = credentials {
77
+ event = AuthorizationEvent (
78
+ eventType: . startFederationToIdentityPool( federatedToken, identityId)
79
+ )
100
80
} else {
101
- return credentials . cognitoSession
81
+ event = AuthorizationEvent ( eventType : . refreshSession ( forceRefresh ) )
102
82
}
103
-
104
- case . identityPoolWithFederation( let federatedToken, let identityId, let awsCredentials) :
105
- if awsCredentials. doesExpire ( ) || forceRefresh {
106
- let event = AuthorizationEvent . init (
107
- eventType: . startFederationToIdentityPool( federatedToken, identityId) )
108
- await authStateMachine. send ( event)
109
- return try await listenForSession ( authStateMachine: authStateMachine)
110
- } else {
111
- return credentials. cognitoSession
112
- }
113
-
114
- case . noCredentials:
115
- let event = AuthorizationEvent ( eventType: . refreshSession( forceRefresh) )
116
83
await authStateMachine. send ( event)
117
84
return try await listenForSession ( authStateMachine: authStateMachine)
118
85
}
86
+ return credentials. cognitoSession
119
87
}
120
88
121
89
func listenForSession( authStateMachine: AuthStateMachine ) async throws -> AuthSession {
@@ -155,8 +123,8 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
155
123
switch error {
156
124
case . sessionError( let fetchError, let credentials) :
157
125
return try sessionResultWithFetchError ( fetchError,
158
- authenticationState: authenticationState,
159
- existingCredentials: credentials)
126
+ authenticationState: authenticationState,
127
+ existingCredentials: credentials)
160
128
case . sessionExpired:
161
129
let session = AuthCognitoSignedInSessionHelper . makeExpiredSignedInSession ( )
162
130
return session
@@ -172,8 +140,8 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
172
140
}
173
141
174
142
func sessionResultWithFetchError( _ error: FetchSessionError ,
175
- authenticationState: AuthenticationState ,
176
- existingCredentials: AmplifyCredentials )
143
+ authenticationState: AuthenticationState ,
144
+ existingCredentials: AmplifyCredentials )
177
145
throws -> AuthSession {
178
146
179
147
var isSignedIn = false
0 commit comments