Bug in calling Auth Admin queries API
In this doc https://docs.amplify.aws/javascript/build-a-backend/auth/admin-actions/, the guide shows that calling API as below:
async function listEditors(limit){
let apiName = 'AdminQueries';
let path = '/listUsersInGroup';
let options = {
queryStringParameters: {
"groupname": "Editors",
"limit": limit,
},
headers: {
'Content-Type' : 'application/json',
Authorization: `${(await fetchAuthSession()).tokens.accessToken.payload}`
}
}
const response = await get({apiName, path, options});
return response;
}
But I think it missed Bearer keyword, specifically
Authorization: `Bearer ${(await fetchAuthSession()).tokens.accessToken.payload}`
And, please add tutorial to change auth function role from local env
Try this:
Authorization: Bearer ${(await fetchAuthSession())?.tokens?.accessToken}
It worked for me.
It took me awhile to find this in the webs. The docs page and tutorials definitely needs to be updated, the "copy" code is incorrect.
"I've been working on setting up admin actions in my React app for the past few days. I used some example code from the documentation, but I'm getting a CORS error. I tried testing a method from API Gateway, but I'm getting a 401 unauthorized error. Can you suggest how I can solve these issues?"
The doc hasn't been updated yet, took me a whole day to find this chat after so much searching, deleting, and recreating the AdminQueries and this was the only issue and it worked after updating to:
Authorization: Bearer ${(await fetchAuthSession())?.tokens?.accessToken}
Thanks, guys