【问题标题】:Where and how to get `identityId` using AWS Amplify?使用 AWS Amplify 在哪里以及如何获取“identityId”?
【发布时间】:2020-11-30 23:59:28
【问题描述】:
所以我正在尝试按照文档中的指南进行操作,但我被困在这里
Storage.get('test.txt', {
level: 'protected',
identityId: 'xxxxxxx' // the identityId of that user
})
.then(result => console.log(result))
.catch(err => console.log(err));
如何获得identityId?
【问题讨论】:
标签:
amazon-s3
amazon-cognito
aws-amplify
【解决方案1】:
我通过将identityId 保存到成功用户登录时的自定义属性,从Auth.currentUserCredentials() 发现了一个破解方法:
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);