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
1 Answer
Reset to default 0Use 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.
本文标签:
版权声明:本文标题:vuejs3 - How to list users from amazon cognito pool using logged in users role and aws-sdk in amplify vue app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282711a1926731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论