您查看的第一个工作流程是 Registration 终端节点。在此终端节点,新用户通过提供登录信息(例如用户名和密码)及其角色的属性(例如身高和体重)进行注册。
当您查看这些终端节点时,终端节点调用的相关代码片段会显示在此指南中。
只需向 /users 终端节点发出 POST 请求,您的注册终端节点即可使用。您可以在 application/app.js 文件的中间部分看到此终端节点的逻辑:
// Create user
app.post('/users', wrapAsync(async (req, res) => {
const validated = validateCreateUser(req.body)
if (!validated.valid) {
throw new Error(validated.message)
}
await createCognitoUser(req.body.username, req.body.password, req.body.email)
const user = await createUser(req.body.username, req.body.height, req.body.weight)
res.json(user)
}))
您的处理程序首先对请求验证传入的负载主体。如果成功,处理程序会在 Amazon Cognito 中创建用户,然后在数据库中创建用户。
您已在 Amazon Cognito 模块中了解了 createCognitoUser 函数,我们在此不再赘述。我们来看一下 createUser 函数,您可在 application/data/createUser.js 中找到此函数。
const { executeWriteSql } = require('./utils')
const createUser = async (username, height, weight) => {
sql = `INSERT INTO users (user_id, username, height, weight) \
VALUES (DEFAULT, :username, :height, :weight) \
RETURNING user_id, username, height, weight`
parameters = [
{
name: 'username',
value: { stringValue: username }
},
{
name: 'height',
value: { longValue: height}
},
{
name: 'weight',
value: { longValue: weight}
}
]
const result = await executeWriteSql(sql, parameters)
return result[0]
}
module.exports = createUser
在此方法中,您编写一些参数化的 SQL 和参数,并将其传递给 executeWriteSql 帮助程序函数。
尝试调用您的 Registration 终端节点以创建新用户。在终端中运行以下命令:
curl -X POST ${BASE_URL}/users \
-H 'Content-Type: application/json' \
-d '{
"username": "bonecrusher",
"password": "Mypassword1",
"email": "test@hello.com",
"height": 75,
"weight": 350
}'
{"user_id":51,"username":"bonecrusher","height":75,"weight":350}
很好! 您已成功创建用户。现在,我们来登录并接收 ID 令牌。