5

我正在使用带有 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();
}
4

4 回答 4

2

我正在遵循指南“serverless-stack”并提示相同的警告消息,我正确登录并正确注销,但不明白为什么会出现警告消息。就我而言,在Amplify.configure我跳过添加identity pool id, 这就是问题所在,User pools并且federated identities不一样。

(英语不是我的母语)

于 2018-05-30T16:37:06.750 回答
1

您是否尝试过检查您的登录请求被拒绝/容易出错的原因?

Auth.signIn(username, password)
    .then(user => console.log(user))
    .catch(err => console.log(err));

// If MFA is enabled, confirm user signing 
// `user` : Return object from Auth.signIn()
// `code` : Confirmation code  
// `mfaType` : MFA Type e.g. SMS, TOTP.
Auth.confirmSignIn(user, code, mfaType)
    .then(data => console.log(data))
    .catch(err => console.log(err));

你可以试试这个,这样你调试起来会更容易。

于 2018-05-21T10:56:50.463 回答
0

您必须在每次请求时使用您的凭证才能使用 AWS 服务:(示例代码角度)

登录

import Amplify from 'aws-amplify';
import Auth from '@aws-amplify/auth';

 Amplify.configure({
      Auth: {
        region: ****,
        userPoolId: *****,
        userPoolWebClientId: ******,
      }
    });

//sign in

Auth.signIn(email, password)

要求

import Auth from '@aws-amplify/auth';

from(Auth.currentCredentials())
   .pipe(
      map(credentials => {
        const documentClient = new AWS.DynamoDB.DocumentClient({
          apiVersion: '2012-08-10',
          region: *****,
          credentials: Auth.essentialCredentials(credentials)
       });

       return documentClient.query(params).promise()

      }),
      flatMap(data => {
          return data
       })
)
于 2018-08-08T09:04:40.900 回答
0

根据 aws-amplify问题跟踪器的建议,将匿名用户添加到您的 cognito 用户池并在您的应用程序中硬编码密码。似乎还有其他选择,但我认为这是最简单的。

于 2018-05-18T17:06:15.197 回答