问题描述
//代码
app.use('/login',{
async create(data,params) {
data.strategy = 'local';
return authentication.create(data,params);
}
});
// 配置
"local": {
"usernameField": "username","passwordField": "password"
}
我在登录 API 中使用 authentication.create 方法,尝试登录时收到此错误响应
{
"name": "NotAuthenticated","message": "Invalid login","code": 401,"className": "not-authenticated","errors": {}
}
//请求
curl --location --request POST 'http://localhost:3030/login'
--header 'Content-Type: application/json'
--data-raw '{
"username": "uname","password": "123456"
}'
用户名和密码正确,这在我的本地机器(macOS)中有效,仅当我在测试环境(AWS EC2 中的 ubuntu 20)中运行时才会引发错误。它们都指向 AWS EC2 实例内的同一个数据库(MysqL 5)。是什么导致了这个问题?
解决方法
在 Windows 和 PM2 中的 authentication configuration options 中提到,username
字段需要转义,因为它也是一个环境变量:
"local": {
"usernameField": "\\username","passwordField": "password"
}
在即将推出的第 5 版中将不再需要这样做。