authentication - Strapi / Nuxt - 找不到自定义路线
问题描述
我用它在strapi和nuxt中设置auth: Auth with Strapi and Nuxt
我目前正在尝试检索特定于经过身份验证的用户的项目(已经检查了这个strapi - 限制用户只获取与他相关的数据)。为此,我在 Strapi (/api/routine/config/routes.json) 中创建了一个自定义路由:
{
"method": "GET",
"path": "/routines/me",
"handler": "Routine.me",
"config": {
"policies": []
}
}
和一个自定义控制器(/api/controllers/Routine.js):
module.exports = {
me: async (ctx) => {
const user = ctx.state.user;
if (!user) {
return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
}
const data = await strapi.services.routine.find({user:user.id});
if(!data){
return ctx.notFound();
}
ctx.send(data);
},
};
我已经通过 Strapi 管理员允许经过身份验证的用户访问“我”。当我从 Nuxt 到达端点时:
const 例程 = 等待 axios.get(
http://localhost:1337/routines/me
)
我收到此错误:
GET http://localhost:1337/routines/me 404(未找到)
为什么找不到自定义路由?我使用了错误的端点吗?
解决方案
也许您已经解决了它,但是您似乎忘记了将身份验证标头与请求一起发送。
const routines = await axios.get(
'http://localhost:1337/routines/me', {
headers: {
Authorization:
this.$auth.getToken('local'),
},
}
推荐阅读
- ffmpeg - 我尝试使用 ffmpeg 将屏幕流式传输到多个源并出现错误
- python-3.x - 如何主动向团队频道发送消息
- scala - 是否可以将演员生成到流中?
- python - 在抓取网络的循环内发出连接数据名
- wpf - 无论是否有焦点,如何始终显示选定行的蓝色?
- java - 有没有办法用方法创建/定义 ArrayCopy
- laravel - 登录 Laravel 后 Session ID 发生变化
- unity3d - 添加 Google Ads 中介时 Android 资源链接失败 (Unity)
- android - Android FragmentManager:自定义动画显示错误的退出片段
- php - 使用未定义的常量 admin - 假定为“admin”(这将在 PHP 的未来版本中引发错误)