Skip to content

Commit 1c32bab

Browse files
fix(auth): device metadata migration (#4503)
* merge main * chore: added a bracket and removed new keywords * chore: changed signature of deleteLegacyDeviceSecrets and regenerated pigeons. Changed legacysecrethandler.kt file to .txt to "comment" it out * chore: run dart format on generated file * chore: migrateLegacyDeviceSecrets now gets called before deleteLegacyCredentials. Cognito_device_secrets return type fixed * chore: add delete and fetch methods to the cognito swift file * fix: various issues with implementation * chore: fix lint issues * test: update test to include mfa and token refresh * chore: use coroutines, add plugin * chore: remove generate code in iOS * chore: fetch asf deviceId from shared prefs directly * chore: update error name * chore: remove native auth plugin * chore: remove old legacy handler * chore: remove check for null device metadata * chore: flatten nested if statements * chore: remove unused type --------- Co-authored-by: Kha Truong <64438356+khatruong2009@users.noreply.github.com>
1 parent 82b7676 commit 1c32bab

File tree

31 files changed

+987
-266
lines changed

31 files changed

+987
-266
lines changed

packages/amplify_native_legacy_wrapper/android/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ android {
4747
namespace 'com.amazonaws.amplify.amplify_native_legacy_wrapper'
4848
}
4949
dependencies {
50-
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.73.0'
51-
implementation 'com.amazonaws:aws-android-sdk-cognitoauth:2.73.0'
50+
implementation 'com.amplifyframework:core:1.38.6'
51+
implementation 'com.amplifyframework:core-kotlin:0.22.8'
52+
implementation 'com.amplifyframework:aws-auth-cognito:1.38.6'
5253
}

packages/amplify_native_legacy_wrapper/android/src/main/kotlin/com/amazonaws/amplify/amplify_native_legacy_wrapper/AmplifyNativeLegacyWrapperPlugin.kt

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,75 @@
44
package com.amazonaws.amplify.amplify_native_legacy_wrapper
55

66
import android.content.Context
7-
import com.amazonaws.mobile.client.AWSMobileClient
8-
import com.amazonaws.mobile.client.Callback
9-
import com.amazonaws.mobile.config.AWSConfiguration
7+
import com.amazonaws.amplify.amplify_native_legacy_wrapper.pigeons.LegacyNativePlugin
8+
import com.amplifyframework.AmplifyException
9+
import com.amplifyframework.auth.AuthException
10+
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
11+
import com.amplifyframework.kotlin.core.Amplify
12+
import com.amplifyframework.core.AmplifyConfiguration
1013
import io.flutter.embedding.engine.plugins.FlutterPlugin
1114
import kotlinx.coroutines.runBlocking
1215
import org.json.JSONObject
1316

1417

1518
/** AmplifyNativeLegacyWrapperPlugin */
16-
class AmplifyNativeLegacyWrapperPlugin: FlutterPlugin, LegacyNativePluginPigeon.LegacyNativePlugin {
19+
class AmplifyNativeLegacyWrapperPlugin: FlutterPlugin, LegacyNativePlugin {
1720

1821
private lateinit var context: Context
19-
private val awsMobileClient = AWSMobileClient.getInstance()
2022

2123
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
2224
context = flutterPluginBinding.applicationContext
23-
LegacyNativePluginPigeon.LegacyNativePlugin.setup(flutterPluginBinding.binaryMessenger, this)
25+
LegacyNativePlugin.setUp(flutterPluginBinding.binaryMessenger, this)
2426
}
2527

2628
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
27-
LegacyNativePluginPigeon.LegacyNativePlugin.setup(binding.binaryMessenger, null)
29+
LegacyNativePlugin.setUp(binding.binaryMessenger, null)
2830
}
2931

30-
override fun configure(config: String, result: LegacyNativePluginPigeon.Result<Void>) {
31-
val mobileClientConfig = JSONObject(config).getJSONObject("auth").getJSONObject("plugins").getJSONObject("awsCognitoAuthPlugin")
32-
awsMobileClient.initialize(
33-
context,
34-
AWSConfiguration(mobileClientConfig),
35-
ResultCallback(result)
36-
)
37-
}
38-
39-
override fun signOut(result: LegacyNativePluginPigeon.Result<Void>) {
40-
awsMobileClient.signOut(null, ResultCallback(result))
32+
override fun configure(config: String, callback: (Result<Unit>) -> Unit) {
33+
try {
34+
val configuration = AmplifyConfiguration.builder(JSONObject(config))
35+
.devMenuEnabled(false)
36+
.build()
37+
Amplify.addPlugin(AWSCognitoAuthPlugin())
38+
Amplify.configure(configuration, context)
39+
callback(Result.success(Unit))
40+
} catch (e: AmplifyException) {
41+
callback(Result.failure(e))
42+
}
4143
}
4244

43-
override fun signIn(
44-
username: String,
45-
password: String,
46-
result: LegacyNativePluginPigeon.Result<Void>
47-
) {
45+
override fun signIn(username: String, password: String, callback: (Result<Unit>) -> Unit) {
4846
runBlocking {
49-
awsMobileClient.signIn(
50-
username,
51-
password,
52-
HashMap(),
53-
ResultCallback(result)
54-
)
47+
try {
48+
Amplify.Auth.signIn(username, password)
49+
callback(Result.success(Unit))
50+
} catch (error: AuthException) {
51+
callback(Result.failure(error))
52+
}
5553
}
5654
}
5755

58-
}
59-
60-
class ResultCallback<T>(private val result: LegacyNativePluginPigeon.Result<Void>) : Callback<T> {
61-
override fun onResult(res: T) {
62-
this.result.success(null)
56+
override fun signOut(callback: (Result<Unit>) -> Unit) {
57+
runBlocking {
58+
try {
59+
Amplify.Auth.signOut()
60+
callback(Result.success(Unit))
61+
} catch (error: AuthException) {
62+
callback(Result.failure(error))
63+
}
64+
}
6365
}
6466

65-
override fun onError(e: Exception) {
66-
this.result.error(e)
67+
override fun rememberDevice(callback: (Result<Unit>) -> Unit) {
68+
runBlocking {
69+
try {
70+
Amplify.Auth.rememberDevice()
71+
callback(Result.success(Unit))
72+
} catch (error: AuthException) {
73+
callback(Result.failure(error))
74+
}
75+
}
6776
}
6877

6978
}

packages/amplify_native_legacy_wrapper/android/src/main/kotlin/com/amazonaws/amplify/amplify_native_legacy_wrapper/LegacyNativePluginPigeon.java

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)