我是AWS的新手。我正在尝试使用Serverless和Cognito注册新用户。我正在使用Apollo / GraphQL(并且从前端看到了我想要的所有信息),但是我已对所有内容进行了硬编码以消除任何请求错误。在我的一生中,我无法弄清楚为什么无法让新用户注册:
const { ApolloServer, gql } = require('apollo-server-lambda');
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
global.fetch = require('node-fetch');
const USER_POOL_ID = 'us-east-1_XXXXXXXXX';
const USER_POOL_APP_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXX';
const POOL_PARAMS = {
UserPoolId: USER_POOL_ID,
ClientId: USER_POOL_APP_ID
}
const UserPool = new AmazonCognitoIdentity.CognitoUserPool(POOL_PARAMS);
const typeDefs = gql`
type UserSignup {
email: String!
name: String!
password: String!
}
type Mutation {
signup(email: String!, name: String!, password: String!): UserSignup
}
`;
const resolvers = {
Mutation: {
signup: (parentVal, { email, name, password }) => {
console.log('I can alway see this log');
const attributeList = [];
const attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'email', Value: 'email@email.com' });
const attributeName = new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'name', Value: 'mike' });
attributeList.push(attributeEmail);
attributeList.push(attributeName);
console.log('I can see these too ', attributeList);
console.log('This will return a function ', UserPool.signUp);
UserPool.signUp('email@email.com', 'P@ssw0rd!', attributeList, null, function(poolErr, poolResult) {
console.log('I never see a console.log from in here');
if (poolErr) {
console.log('ERROR: ', poolErr);
}
console.log('RESULT: ', poolResult);
return { email, name };
})
}
}
};
const server = new ApolloServer({ typeDefs, resolvers });
exports.graphql = server.createHandler();
运行sls logs
时,我再也看不到UserPool.signUp
函数内部有任何返回值。它总是在线上之前。以下是我从this SO post获得的serverless.yml
文件的信息。
service: AppName
provider:
name: aws
runtime: nodejs8.10
functions:
graphql:
handler: handler.graphql
events:
- http:
path: graphql
method: post
cors: true
integration: lambda
authorizer:
name: authorizer
arn: arn:aws:cognito-idp:us-east-1:XXXXXXXXXXXX:userpool/us-east-1_XXXXXXXXX
claims:
- email
我能够通过curl请求创建一个新用户,所以我知道我已经在AWS端设置了它。已经过了几天晚上,我要放弃Cognito并使用JWT。