【问题标题】:Strapi API gives 401 for authenticated user with Nuxt/Strapi moduleStrapi API 使用 Nuxt/Strapi 模块为经过身份验证的用户提供 401
【发布时间】:2020-11-24 10:18:02
【问题描述】:

只是想在这里学习另一个框架:D

我将 Nuxt 与 Strapi 和 MongoDB 一起使用。为了偷工减料(不要讨厌播放器..)我已经安装了 Nuxt/Strapi 模块 => https://strapi.nuxtjs.org/

当我没有通过身份验证时,我可以从 Strapi 获取集合数据,但是当用户通过身份验证(基本 strapi 角色)时,Strapi 会为集合数据引发 401 错误。该集合对公众以及角色和权限中经过身份验证的用户开放。所以 Strapi 端不应该有问题。

我从 Strapi 获得并可以记录 JWT 令牌,并且用户已正确登录/验证。据我所知(它的文档有点糟糕)Nuxt/Strapi 模块应该在标头中传递 JWT 令牌,并且身份验证应该开箱即用地进行 REST 调用?至少代码在未通过身份验证时有效:

async fetch() {
    this.building = await this.$strapi.find('building');
},
data() {
  return {
    building: []
  }
}

通过身份验证后,控制台会记录以下错误:

index.js?d823:434 GET http://localhost:1337/building 401 (Unauthorized)

fetch.client.js?2293:75 Error in fetch(): HTTPError: Schema hasn't been registered for model "user".
Use mongoose.model(name, schema)

我是这个堆栈的新手,所以我有点搞不清楚出了什么问题。

我能找到的唯一与此问题密切相关的是这个线程: Mongoose Schema hasn't been registered for model

这是非常基本的功能,必须经过测试,所以我认为这种错误不会溜进模块。更有可能是我做错了什么。

提前致谢!

【问题讨论】:

  • 所以评论我自己的问题,看来问题出在 MongoDB 上。我在 MariaDB 上安装了相同的结构,一切似乎都运行良好。

标签: node.js mongodb nuxt.js strapi


【解决方案1】:

您必须首先进行身份验证,或者使用公共用户,对于第一次身份验证调用/auth/local,您将获得一个令牌作为响应,在获取的标头中使用它。 检查这个:

import axios from 'axios';

const { data } = await axios.post('http://localhost:1337/auth/local', {
//your user
  identifier: 'reader@strapi.io',
  password: 'strapi',
});

console.log(data);

获取

import axios from 'axios';

const { data } = await axios.get('http://localhost:1337/articles', {
  headers: {
    Authorization:
      'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTc2OTM4MTUwLCJleHAiOjE1Nzk1MzAxNTB9.UgsjjXkAZ-anD257BF7y1hbjuY3ogNceKfTAQtzDEsU',
  },
});

更改之前保存的令牌的长令牌

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2017-12-24
    • 2019-11-03
    • 2020-11-15
    • 2015-04-11
    相关资源
    最近更新 更多