我试图在一个简单的反应应用程序上使用Loopback学习身份验证。
我试图列出Pets
,我设置的只有在用户通过身份验证后才能访问。
pet.json
文件中的我的ACLS部分目前看起来像这样(这是遵循Loopback CLI的结果):
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
}
]
为了记录用户,我目前在客户端有以下代码:
handleLogin () {
const credentials = {
email: 'blah@email.com',
password: '123'
}
return axios.post('http://localhost:3000/api/Users/login', credentials)
.then((result)=> {
console.log('Login result:', result) // result object comes in just fine
const token = result.data.id
localStorage.setItem('jwtToken', token)
return axios.get('http://localhost:3000/api/Pets')
.then( res => {
console.log('res', res)
this.setState({pets: res.data})
})
})
}
我错过了什么吗?令牌是否必须在应用程序的其他位置设置?
答案 0 :(得分:1)
用于模型中的身份验证:
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
}
您还指定了属性:
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW",
"property": ["create"]
}
DENY EveryOne USE:
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
}
请参阅此链接:LoopBack authentication
您需要在标头中传递accessToken:
ACCESS_TOKEN=6Nb2ti5QEXIoDBS5FQGWIz4poRFiBCMMYJbYXSGHWuulOuy0GTEuGx2VCEVvbpBK
# Authorization Header
curl -X GET -H "Authorization: $ACCESS_TOKEN" \
http://localhost:3000/api/widgets
# Query Parameter
curl -X GET http://localhost:3000/api/widgets?access_token=$ACCESS_TOKEN