【发布时间】:2018-05-12 09:16:52
【问题描述】:
我正在使用带有 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 函数的代码:
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);
}
}
还有这些小辅助函数:
response.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();
}
【问题讨论】:
-
你能发布一些API网关调用的代码吗?
-
我已经更新了主帖。一些细节是硬编码的,用于调试。 AWS 是否要求响应采用我不遵守的格式?
-
我猜这之前没有经过身份验证(即使您认为自己是),当您打开强制身份验证时,您发现您的 API 调用从未经过身份验证。 github.com/aws/aws-amplify/blob/master/packages/aws-amplify/src/…
-
如何确保我进行了身份验证?我运行
Auth.signIn('username', 'password'),我似乎收到了正确的对象(没有错误或任何东西)。我应该执行其他登录方法吗? -
我明白了。这里似乎发布了一些选项github.com/aws/aws-amplify/issues/432
标签: amazon-web-services aws-mobilehub aws-amplify