我正在使用带有 MobileHub 的 AWS Amplify 库。
我连接了一个 Cognito 用户池和一个 API 网关(与 Lambda 函数通信)。我希望我的用户在访问资源之前进行签名,因此我在 MobileHub 用户登录页面和云逻辑页面中启用了“强制登录”。
身份验证工作正常,但是当我向我的 API 发送 GET 请求时,我收到此错误:
"[WARN] 46:22.756 API - ensure credentials error": "cannot get guest credentials when mandatory signin enabled"
我了解 Amplify 会生成访客凭据,并将其放入我的 GET 请求中。由于我启用了“强制登录”,这不起作用。
但为什么要使用访客凭据?我已经登录——它不应该使用这些凭据吗?如何使用经过身份验证的用户信息?
干杯。
编辑:这是来自 Lambda 函数的代码:
拉姆达函数:
import { success, failure } from '../lib/response';
import * as dynamoDb from '../lib/dynamodb';
export const main = async (event, context, callback) => {
const params = {
TableName: 'chatrooms',
Key: {
user_id: 'user-abc', //event.pathParameters.user_id,
chatroom_id: 'chatroom-abc',
}
};
try {
const result = await dynamoDb.call('get', params);
if (result.Item) {
return callback(null, success(result.Item, 'Item found'));
} else {
return callback(null, failure({ status: false }, 'Item not found.'));
}
} catch (err) {
console.log(err);
return callback(null, failure({ status: false }), err);
}
}
还有这些小辅助函数:
响应.js:
export const success = (body, message) => buildResponse(200, body, message)
export const failure = (body, message) => buildResponse(500, body, message)
const buildResponse = (statusCode, body, message=null) => ({
statusCode: statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify({
...body,
message: message
})
});
dynamodb.js:
import AWS from 'aws-sdk';
AWS.config.update({ region: 'ap-southeast-2' });
export const call = (action, params) => {
const dynamoDb = new AWS.DynamoDB.DocumentClient();
return dynamoDb[action](params).promise();
}