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

问题描述 投票:0回答:2

所以我试图按照文档中的指南进行操作,但我被困在这里

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
2个回答
2
投票

我通过在成功用户登录时将

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);

0
投票

如果有人仍在寻找答案,这对我有用(在 Amplify V6 中):

import { fetchAuthSession } from 'aws-amplify/auth'

...

const credentials = await fetchAuthSession()
console.log(credentials.identityId)
© www.soinside.com 2019 - 2024. All rights reserved.