@@ -52,33 +52,19 @@ import kotlinx.coroutines.flow.callbackFlow
5252 * An abstraction of the Amplify.Auth API that allows us to use coroutines with no exceptions
5353 */
5454internal interface AuthProvider {
55- suspend fun signIn (
56- username : String ,
57- password : String
58- ): AmplifyResult <AuthSignInResult >
55+ suspend fun signIn (username : String , password : String ): AmplifyResult <AuthSignInResult >
5956
60- suspend fun confirmSignIn (
61- challengeResponse : String
62- ): AmplifyResult <AuthSignInResult >
57+ suspend fun confirmSignIn (challengeResponse : String ): AmplifyResult <AuthSignInResult >
6358
64- suspend fun signUp (
65- username : String ,
66- password : String ,
67- options : AuthSignUpOptions
68- ): AmplifyResult <AuthSignUpResult >
59+ suspend fun signUp (username : String , password : String , options : AuthSignUpOptions ): AmplifyResult <AuthSignUpResult >
6960
70- suspend fun confirmSignUp (
71- username : String ,
72- code : String
73- ): AmplifyResult <AuthSignUpResult >
61+ suspend fun confirmSignUp (username : String , code : String ): AmplifyResult <AuthSignUpResult >
7462
75- suspend fun resendSignUpCode (
76- username : String
77- ): AmplifyResult <AuthCodeDeliveryDetails >
63+ suspend fun resendSignUpCode (username : String ): AmplifyResult <AuthCodeDeliveryDetails >
7864
79- suspend fun resetPassword (
80- username : String
81- ): AmplifyResult <AuthResetPasswordResult >
65+ suspend fun autoSignIn (): AmplifyResult < AuthSignInResult >
66+
67+ suspend fun resetPassword ( username : String ): AmplifyResult <AuthResetPasswordResult >
8268
8369 suspend fun confirmResetPassword (
8470 username : String ,
@@ -92,10 +78,7 @@ internal interface AuthProvider {
9278
9379 suspend fun fetchUserAttributes (): AmplifyResult <List <AuthUserAttribute >>
9480
95- suspend fun confirmUserAttribute (
96- key : AuthUserAttributeKey ,
97- confirmationCode : String
98- ): AmplifyResult <Unit >
81+ suspend fun confirmUserAttribute (key : AuthUserAttributeKey , confirmationCode : String ): AmplifyResult <Unit >
9982
10083 suspend fun resendUserAttributeConfirmationCode (key : AuthUserAttributeKey ): AmplifyResult <AuthCodeDeliveryDetails >
10184
@@ -108,15 +91,16 @@ internal interface AuthProvider {
10891
10992internal sealed interface AuthConfigurationResult {
11093 data class Valid (val configuration : AmplifyAuthConfiguration ) : AuthConfigurationResult
94+
11195 data class Invalid (val message : String , val cause : Exception ? = null ) : AuthConfigurationResult
96+
11297 object Missing : AuthConfigurationResult
11398}
11499
115100/* *
116101 * The [AuthProvider] implementation that calls through to [Amplify.Auth]
117102 */
118103internal class RealAuthProvider : AuthProvider {
119-
120104 init {
121105 val cognitoPlugin = getCognitoPlugin()
122106 cognitoPlugin?.addToUserAgent(AWSCognitoAuthMetadataType .Authenticator , BuildConfig .VERSION_NAME )
@@ -139,24 +123,18 @@ internal class RealAuthProvider : AuthProvider {
139123 )
140124 }
141125
142- override suspend fun signUp (
143- username : String ,
144- password : String ,
145- options : AuthSignUpOptions
146- ) = suspendCoroutine { continuation ->
147- Amplify .Auth .signUp(
148- username,
149- password,
150- options,
151- { continuation.resume(AmplifyResult .Success (it)) },
152- { continuation.resume(AmplifyResult .Error (it)) }
153- )
154- }
126+ override suspend fun signUp (username : String , password : String , options : AuthSignUpOptions ) =
127+ suspendCoroutine { continuation ->
128+ Amplify .Auth .signUp(
129+ username,
130+ password,
131+ options,
132+ { continuation.resume(AmplifyResult .Success (it)) },
133+ { continuation.resume(AmplifyResult .Error (it)) }
134+ )
135+ }
155136
156- override suspend fun confirmSignUp (
157- username : String ,
158- code : String
159- ) = suspendCoroutine { continuation ->
137+ override suspend fun confirmSignUp (username : String , code : String ) = suspendCoroutine { continuation ->
160138 Amplify .Auth .confirmSignUp(
161139 username,
162140 code,
@@ -165,40 +143,40 @@ internal class RealAuthProvider : AuthProvider {
165143 )
166144 }
167145
168- override suspend fun resendSignUpCode (
169- username : String
170- ) = suspendCoroutine { continuation ->
146+ override suspend fun resendSignUpCode (username : String ) = suspendCoroutine { continuation ->
171147 Amplify .Auth .resendSignUpCode(
172148 username,
173149 { continuation.resume(AmplifyResult .Success (it)) },
174150 { continuation.resume(AmplifyResult .Error (it)) }
175151 )
176152 }
177153
178- override suspend fun resetPassword (
179- username : String
180- ) = suspendCoroutine { continuation ->
181- Amplify .Auth .resetPassword(
182- username,
154+ override suspend fun autoSignIn () = suspendCoroutine { continuation ->
155+ Amplify .Auth .autoSignIn(
183156 { continuation.resume(AmplifyResult .Success (it)) },
184157 { continuation.resume(AmplifyResult .Error (it)) }
185158 )
186159 }
187160
188- override suspend fun confirmResetPassword (
189- username : String ,
190- newPassword : String ,
191- confirmationCode : String
192- ) = suspendCoroutine { continuation ->
193- Amplify .Auth .confirmResetPassword(
161+ override suspend fun resetPassword (username : String ) = suspendCoroutine { continuation ->
162+ Amplify .Auth .resetPassword(
194163 username,
195- newPassword,
196- confirmationCode,
197- { continuation.resume(AmplifyResult .Success (Unit )) },
164+ { continuation.resume(AmplifyResult .Success (it)) },
198165 { continuation.resume(AmplifyResult .Error (it)) }
199166 )
200167 }
201168
169+ override suspend fun confirmResetPassword (username : String , newPassword : String , confirmationCode : String ) =
170+ suspendCoroutine { continuation ->
171+ Amplify .Auth .confirmResetPassword(
172+ username,
173+ newPassword,
174+ confirmationCode,
175+ { continuation.resume(AmplifyResult .Success (Unit )) },
176+ { continuation.resume(AmplifyResult .Error (it)) }
177+ )
178+ }
179+
202180 override suspend fun signOut () = suspendCoroutine { continuation ->
203181 Amplify .Auth .signOut { continuation.resume(it) }
204182 }
@@ -217,27 +195,24 @@ internal class RealAuthProvider : AuthProvider {
217195 )
218196 }
219197
220- override suspend fun confirmUserAttribute (
221- key : AuthUserAttributeKey ,
222- confirmationCode : String
223- ) = suspendCoroutine { continuation ->
224- Amplify .Auth .confirmUserAttribute(
225- key,
226- confirmationCode,
227- { continuation.resume(AmplifyResult .Success (Unit )) },
228- { continuation.resume(AmplifyResult .Error (it)) }
229- )
230- }
198+ override suspend fun confirmUserAttribute (key : AuthUserAttributeKey , confirmationCode : String ) =
199+ suspendCoroutine { continuation ->
200+ Amplify .Auth .confirmUserAttribute(
201+ key,
202+ confirmationCode,
203+ { continuation.resume(AmplifyResult .Success (Unit )) },
204+ { continuation.resume(AmplifyResult .Error (it)) }
205+ )
206+ }
231207
232- override suspend fun resendUserAttributeConfirmationCode (
233- key : AuthUserAttributeKey
234- ) = suspendCoroutine { continuation ->
235- Amplify .Auth .resendUserAttributeConfirmationCode(
236- key,
237- { continuation.resume(AmplifyResult .Success (it)) },
238- { continuation.resume(AmplifyResult .Error (it)) }
239- )
240- }
208+ override suspend fun resendUserAttributeConfirmationCode (key : AuthUserAttributeKey ) =
209+ suspendCoroutine { continuation ->
210+ Amplify .Auth .resendUserAttributeConfirmationCode(
211+ key,
212+ { continuation.resume(AmplifyResult .Success (it)) },
213+ { continuation.resume(AmplifyResult .Error (it)) }
214+ )
215+ }
241216
242217 override suspend fun getCurrentUser () = suspendCoroutine { continuation ->
243218 Amplify .Auth .getCurrentUser(
@@ -282,13 +257,10 @@ internal class RealAuthProvider : AuthProvider {
282257 return AuthConfigurationResult .Valid (amplifyAuthConfiguration)
283258 }
284259
285- private fun getCognitoPlugin (): AWSCognitoAuthPlugin ? {
286- return try {
287- Amplify .Auth .getPlugin(" awsCognitoAuthPlugin" )
288- as AWSCognitoAuthPlugin
289- } catch (e: Throwable ) {
290- null
291- }
260+ private fun getCognitoPlugin (): AWSCognitoAuthPlugin ? = try {
261+ Amplify .Auth .getPlugin(" awsCognitoAuthPlugin" ) as AWSCognitoAuthPlugin
262+ } catch (e: Throwable ) {
263+ null
292264 }
293265
294266 private fun getSignInMethod (attributes : List <UsernameAttribute >) = when {
0 commit comments