@@ -27,6 +27,7 @@ import aws.sdk.kotlin.services.cognitoidentityprovider.model.AttributeType
27
27
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChallengeNameType
28
28
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChangePasswordRequest
29
29
import aws.sdk.kotlin.services.cognitoidentityprovider.model.DeviceRememberedStatusType
30
+ import aws.sdk.kotlin.services.cognitoidentityprovider.model.ForgetDeviceRequest
30
31
import aws.sdk.kotlin.services.cognitoidentityprovider.model.GetUserAttributeVerificationCodeRequest
31
32
import aws.sdk.kotlin.services.cognitoidentityprovider.model.GetUserRequest
32
33
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ListDevicesRequest
@@ -1291,16 +1292,21 @@ internal class RealAWSCognitoAuthPlugin(
1291
1292
onError : Consumer <AuthException >
1292
1293
) {
1293
1294
authStateMachine.getCurrentState { authState ->
1294
- when (val authState = authState.authNState) {
1295
+ when (val authNState = authState.authNState) {
1295
1296
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." ))
1301
1309
}
1302
- } else {
1303
- updateDevice(device.id, DeviceRememberedStatusType .NotRemembered , onSuccess, onError)
1304
1310
}
1305
1311
}
1306
1312
is AuthenticationState .SignedOut -> {
@@ -1313,6 +1319,16 @@ internal class RealAWSCognitoAuthPlugin(
1313
1319
}
1314
1320
}
1315
1321
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
+
1316
1332
override fun fetchDevices (
1317
1333
onSuccess : Consumer <List <AuthDevice >>,
1318
1334
onError : Consumer <AuthException >
@@ -1342,12 +1358,14 @@ internal class RealAWSCognitoAuthPlugin(
1342
1358
accessToken = tokens.value?.accessToken
1343
1359
}
1344
1360
)
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)
1351
1369
} catch (e: Exception ) {
1352
1370
onError.accept(CognitoAuthExceptionConverter .lookup(e, " Fetch devices failed." ))
1353
1371
}
0 commit comments