Warm tip: This article is reproduced from serverfault.com, please click

authentication-Strapi / Nuxt

(authentication - Strapi / Nuxt)

发布于 2020-11-30 17:16:16

我已经用它来在trapi和nuxt中设置身份验证:使用Strapi和Nuxt进行身份 验证

我目前正在尝试检索特定于经过身份验证的用户的项目(已经签出了该bandi-限制用户仅提取与他有关的数据)。为此,我在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 admin授予了经过身份验证的用户访问“我”的权限。当我从Nuxt到达端点时:

const例程=等待axios.get(http://localhost:1337/routines/me

我收到此错误:

GET http:// localhost:1337 / routines / me 404(未找到)

为什么找不到自定义路线?我使用了错误的端点吗?

Questioner
joedoesnotknow
Viewed
11
joedoesnotknow 2020-12-08 07:16:28

这是我的Strapi路由配置中的故障。答案是通过非常有用的Strapi论坛提供的: 从Nuxt调用自定义控制器时禁止403

这是问题所在:

{
  "method": "GET",
  "path": "/routines/:id",
  "handler": "routine.findOne",
  "config": {
    "policies": []
  }
},
{
  "method": "GET",
  "path": "/routines/me",
  "handler": "routine.me",
  "config": {
    "policies": []
  }

因此,基本上你现在就在第一条路线上,并且假设我实际上是:id。Koa正在使用正则表达式进行验证,因此在这种情况下,它会采用第一个匹配的路由。使用/:id将具有/ me的路线移至该路线之上