我正在尝试使用 API Gateway 创建 Lambda 函数。我将 CORS 与 OPTIONS (具有正确的方法)一起使用,并将我的 zip 文件上传到 lambda。多次上传后我找不到此错误的修复方法。我希望你能帮我解决这个问题。
这是结构
Index.js
const { DynamoDBClient, QueryCommand } = require("@aws-sdk/client-dynamodb");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const client = new DynamoDBClient({ region: process.env.AWS_REGION });
exports.handler = async (event) => {
console.log("Received event:", JSON.stringify(event, null, 2));
if (!event.body) {
return {
statusCode: 400,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
},
body: JSON.stringify({ error: "Invalid request, no body found" }),
};
}
const { username, password } = JSON.parse(event.body);
const params = {
TableName: "users",
IndexName: "username-index",
KeyConditionExpression: "username = :username",
ExpressionAttributeValues: {
":username": { S: username },
},
};
try {
const command = new QueryCommand(params);
const result = await client.send(command);
if (result.Items.length === 0) {
console.log("No user found with username:", username);
return {
statusCode: 400,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
},
body: JSON.stringify({ message: "Invalid credentials" }),
};
}
const user = result.Items[0];
console.log("User found:", user);
const isPasswordValid = await bcrypt.compare(password, user.password.S);
console.log("Password is valid:", isPasswordValid);
if (!isPasswordValid) {
console.log("Invalid password for username:", username);
return {
statusCode: 400,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
},
body: JSON.stringify({ message: "Invalid credentials" }),
};
}
const token = jwt.sign({ userId: user.userId.S }, process.env.JWT_SECRET, {
expiresIn: "1h",
});
console.log("Generated token:", token);
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
},
body: JSON.stringify({ token }),
};
}
catch (error) {
console.error("Login error:", error);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE",
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
},
body: JSON.stringify({ error: "Could not log in user" }),
};
}
};
错误:
Login response:
{data: {…}, status: 200, statusText: '', headers: {…}, config: {…}, …}
Login.js:21 Login response data:
{statusCode: 400, headers: {…}, body: '{"error":"Invalid request, no body found"}'}
body
:
"{\"error\":\"Invalid request, no body found\"}"
headers
:
{Access-Control-Allow-Origin: '*', Access-Control-Allow-Methods: 'OPTIONS,POST,GET,PUT,DELETE', Access-Control-Allow-Headers: 'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'}
statusCode
:
400
[[Prototype]]
:
Object
package.json
{
"name": "my-lambda-function",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.587.0",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^9.0.2"
}
}
我还在 API 网关的 auth/login/ 中将映射模板添加到我的 POST 资源中,如下所示:
{
"body": $input.json('$')
}
最后,这是我的 Login.js,让您了解我如何处理请求。
登录.js
const handleLogin = async () => {
setLoading(true);
setError("");
try {
const response = await axios.post(
"/auth/login",
JSON.stringify({ username, password }), // Ensure body is a JSON string
{ headers: { "Content-Type": "application/json" } }
);
console.log("Login response:", response);
console.log("Login response data:", response.data);
if (response.data.token) {
localStorage.setItem("token", response.data.token);
onAuthSuccess();
} else {
setError("Invalid credentials");
}
} catch (error) {
console.error("Login error:", error);
setError("Invalid credentials");
}
setLoading(false);
};
经过一些修改,我让它工作了
将模板映射到 API 网关的 auth/login/ 中的 POST 资源:
{
"body": "$util.escapeJavaScript($input.body)"
}
我还需要向角色添加策略,即 loginUser-role-8nvnlq44
政策是:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:Query",
"dynamodb:PutItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem",
"dynamodb:GetItem"
],
"Resource": [
"arn:aws:dynamodb:eu-north-1:637423521822:table/users",
"arn:aws:dynamodb:eu-north-1:637423521822:table/users/index/username-index"
]
}
]
}