Skip to content

Commit 932c4f8

Browse files
authoredFeb 7, 2024
chore(auth): Cleanup RealAWSCognitoAuthPluginTest (#2690)
1 parent 2ee5eb2 commit 932c4f8

File tree

3 files changed

+433
-822
lines changed

3 files changed

+433
-822
lines changed
 

‎.idea/codeStyles/Project.xml

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt

+32-14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import aws.sdk.kotlin.services.cognitoidentityprovider.model.AttributeType
2727
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChallengeNameType
2828
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChangePasswordRequest
2929
import aws.sdk.kotlin.services.cognitoidentityprovider.model.DeviceRememberedStatusType
30+
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ForgetDeviceRequest
3031
import aws.sdk.kotlin.services.cognitoidentityprovider.model.GetUserAttributeVerificationCodeRequest
3132
import aws.sdk.kotlin.services.cognitoidentityprovider.model.GetUserRequest
3233
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ListDevicesRequest
@@ -1291,16 +1292,21 @@ internal class RealAWSCognitoAuthPlugin(
12911292
onError: Consumer<AuthException>
12921293
) {
12931294
authStateMachine.getCurrentState { authState ->
1294-
when (val authState = authState.authNState) {
1295+
when (val authNState = authState.authNState) {
12951296
is AuthenticationState.SignedIn -> {
1296-
if (device.id.isEmpty()) {
1297-
GlobalScope.launch {
1298-
val deviceKey = authEnvironment.getDeviceMetadata(authState.signedInData.username)
1299-
?.deviceKey
1300-
updateDevice(deviceKey, DeviceRememberedStatusType.NotRemembered, onSuccess, onError)
1297+
GlobalScope.launch {
1298+
try {
1299+
if (device.id.isEmpty()) {
1300+
val deviceKey = authEnvironment.getDeviceMetadata(authNState.signedInData.username)
1301+
?.deviceKey
1302+
forgetDevice(deviceKey)
1303+
} else {
1304+
forgetDevice(device.id)
1305+
}
1306+
onSuccess.call()
1307+
} catch (e: Exception) {
1308+
onError.accept(CognitoAuthExceptionConverter.lookup(e, "Failed to forget device."))
13011309
}
1302-
} else {
1303-
updateDevice(device.id, DeviceRememberedStatusType.NotRemembered, onSuccess, onError)
13041310
}
13051311
}
13061312
is AuthenticationState.SignedOut -> {
@@ -1313,6 +1319,16 @@ internal class RealAWSCognitoAuthPlugin(
13131319
}
13141320
}
13151321

1322+
private suspend fun forgetDevice(alternateDeviceId: String?) {
1323+
val tokens = getSession().userPoolTokensResult
1324+
authEnvironment.cognitoAuthService.cognitoIdentityProviderClient?.forgetDevice(
1325+
ForgetDeviceRequest.invoke {
1326+
accessToken = tokens.value?.accessToken
1327+
deviceKey = alternateDeviceId
1328+
}
1329+
)
1330+
}
1331+
13161332
override fun fetchDevices(
13171333
onSuccess: Consumer<List<AuthDevice>>,
13181334
onError: Consumer<AuthException>
@@ -1342,12 +1358,14 @@ internal class RealAWSCognitoAuthPlugin(
13421358
accessToken = tokens.value?.accessToken
13431359
}
13441360
)
1345-
val _devices = response?.devices
1346-
val authdeviceList = mutableListOf<AuthDevice>()
1347-
_devices?.forEach {
1348-
authdeviceList.add(AuthDevice.fromId(it.deviceKey ?: ""))
1349-
}
1350-
onSuccess.accept(authdeviceList)
1361+
1362+
val devices = response?.devices?.map { device ->
1363+
val id = device.deviceKey ?: ""
1364+
val name = device.deviceAttributes?.find { it.name == "device_name" }?.value
1365+
AuthDevice.fromId(id, name)
1366+
} ?: emptyList()
1367+
1368+
onSuccess.accept(devices)
13511369
} catch (e: Exception) {
13521370
onError.accept(CognitoAuthExceptionConverter.lookup(e, "Fetch devices failed."))
13531371
}

0 commit comments

Comments
 (0)