How to get and pass deviceKey in social login using Cognito and AWS Amplify?

人盡茶涼 提交于 2019-12-13 03:12:20

问题


I am trying to use the function setDeviceStatusRemembered from Cognito in the social login, but it needs a deviceKey, which is not available in the user object returned.

The project is made using React. I already tried some responses from StackOverflow and docs, but no success.

The user is handled and created by Amazon via OAuth in social login, and after login, I am getting the user data with this:

const cognitoUser = await Auth.currentAuthenticatedUser()

After, the object cognitoUser shows the deviceKey null, and if I try the following code, I get the error message "1 validation error detected: Value null at 'deviceKey' failed to satisfy constraint: Member must not be null"

cognitoUser.setDeviceStatusRemembered({
   onSuccess: function (result) {
     console.log('call result: ' + result);
   },

   onFailure: function(err) {
     console.log('call error', err);
   }
})

How can I solve this problem? Am I missing something? Thanks.


回答1:


First of all, make sure that your user pool is configured to remember devices. If you set up Auth through Amplify this is probably off by default. Here's how it is enabled in the AWS console:

You can get the device key from the user object:

Auth.currentAuthenticatedUser({
}).then(user => {
  user.getCachedDeviceKeyAndPassword(); // without this line, the deviceKey is null
  console.log(user.deviceKey);
});


来源:https://stackoverflow.com/questions/56451317/how-to-get-and-pass-devicekey-in-social-login-using-cognito-and-aws-amplify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!