admin管理员组

文章数量:1122846

I'm trying to list all users in cognito poool in my vue amplify gen1 app.

I use aws-sdk and want that logged in user can perform above operation. I can make it work by adding accessKeyId and secretAccessKey, but i want to avoid putting these values in my code, and want to leverage users role. User has assigned role which has got attached correct policies.

I just cant seem to figure out how to get the access keys from user itself. If I pass it with the commented out credentials (see code below), it obviously work, but I dont want to pass my access keys like that. If I pass it through the 'fromCognitoIdentity' - I get error "Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1"

Is there somewhitng wrong in how i create the parameters in fromCognitoIdentity?

  const cognito = new AWS.CognitoIdentityServiceProvider({
  credentials: fromCognitoIdentity(
                { 
                  identityId: 'ap-southeast:ap-southeast-2_Wc***Fww',
                  customRoleArn: 'arn:aws:iam::89***6347:role/No***e_admin_role',
                  region:'ap-southeast-2'
                }
              ),
  // credentials: {
  //    accessKeyId: 'AKIA47CR***XRCDT',
  //    secretAccessKey: '56IUFMiThv0W****mJ5e+VdmiULlpLx1fL',
  // },
  region:'ap-southeast-2'

});
const params = {
    UserPoolId: 'ap-southeast-2_Wc***ww',

};

try {
    const data = await cognito.listUsers(params).promise();
    users.value = data.Users;
    realData.value = transformUsersToArray(data.Users!);
    origData = transformUsersToArray(data.Users!);
    console.log("real data is :");
    console.log(realData);
    console.log(data.Users);
    loadingRef.value = false;
} catch (error) {
    console.error('Error fetching users:', error);
    loadingRef.value = false;
}

I'm trying to list all users in cognito poool in my vue amplify gen1 app.

I use aws-sdk and want that logged in user can perform above operation. I can make it work by adding accessKeyId and secretAccessKey, but i want to avoid putting these values in my code, and want to leverage users role. User has assigned role which has got attached correct policies.

I just cant seem to figure out how to get the access keys from user itself. If I pass it with the commented out credentials (see code below), it obviously work, but I dont want to pass my access keys like that. If I pass it through the 'fromCognitoIdentity' - I get error "Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1"

Is there somewhitng wrong in how i create the parameters in fromCognitoIdentity?

  const cognito = new AWS.CognitoIdentityServiceProvider({
  credentials: fromCognitoIdentity(
                { 
                  identityId: 'ap-southeast:ap-southeast-2_Wc***Fww',
                  customRoleArn: 'arn:aws:iam::89***6347:role/No***e_admin_role',
                  region:'ap-southeast-2'
                }
              ),
  // credentials: {
  //    accessKeyId: 'AKIA47CR***XRCDT',
  //    secretAccessKey: '56IUFMiThv0W****mJ5e+VdmiULlpLx1fL',
  // },
  region:'ap-southeast-2'

});
const params = {
    UserPoolId: 'ap-southeast-2_Wc***ww',

};

try {
    const data = await cognito.listUsers(params).promise();
    users.value = data.Users;
    realData.value = transformUsersToArray(data.Users!);
    origData = transformUsersToArray(data.Users!);
    console.log("real data is :");
    console.log(realData);
    console.log(data.Users);
    loadingRef.value = false;
} catch (error) {
    console.error('Error fetching users:', error);
    loadingRef.value = false;
}
Share Improve this question asked yesterday benihamalubenihamalu 214 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use Amplify SDK for signIn. This automatically handles credential management - after authentication, Amplify will automatically sign requests with short-term credentials from the Cognito Identity Pool that expire, rotate and refresh automatically.

import { signIn } from 'aws-amplify/auth';

const handleSignIn = async ({
  username,
  password
}) => {
  const {
    isSignedIn,
    nextStep
  } = await signIn({ username, password });
}

If you want to access credentials, you can access them as per below.

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

const getSession = async () => {
  try {
    const {
        tokens,
        credentials,
        identityId,
        userSub
    } = await fetchAuthSession();

Note: However, once you use Amplify SDK or Authenticator component to login the user, then you don't have to manually add Identity Pool credentials while using AWS SDK. The Amplify SDK will take care of that.

Just make sure, the Identity Pool role has sufficient permissions.

Refer this Migrate from v5 to v6 to learn a lot about useful methods.

Also read Under the hood to learn more.

本文标签: