所以我试图按照文档中的指南进行操作,但我被困在这里
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);
如果有人仍在寻找答案,这对我有用(在 Amplify V6 中):
import { fetchAuthSession } from 'aws-amplify/auth'
...
const credentials = await fetchAuthSession()
console.log(credentials.identityId)