在哪里以及如何使用AWS Amplify获取`identityId`?

问题描述

因此,我试图按照文档中的指南进行操作,并且被困@R_404_6329@

Storage.get('test.txt',{ 
  level: 'protected',identityId: 'xxxxxxx' // the identityId of that user
})
.then(result => console.log(result))
.catch(err => console.log(err));

一个人如何得到identityId

解决方法

通过在用户成功登录后将Auth.currentUserCredentials()保存到自定义属性,我发现了identityId的一个黑客:

const CUSTOM_IDENTITY_FIELD = "custom:identityId";

if (attributes && !attributes[CUSTOM_IDENTITY_FIELD]) {
    await Auth.updateUserAttributes(currUser,{
      [CUSTOM_IDENTITY_FIELD]: (await Auth.currentUserCredentials())
        .identityId,});
    onInfo("Welcome to Tippify," + currUser.username);
}

有了这个,我还可以实现Firebase首次登录功能。 稍后,当我想从Storage查询用户图像时:

Storage.get(user.picture,{
    level: "protected",identityId: user[CUSTOM_IDENTITY_FIELD],})
  .then(setPicture)
  .catch(onError);