首页 > 解决方案 > JWT 身份验证尝试返回 404

问题描述

大家好,我正在尝试使用 react 前端从我的 rails 后端应用程序获取身份验证令牌。我收到 404 错误,当我使用邮递员获取令牌时,同样的事情正在工作

反应代码

fetch('http://localhost:3000/authenticate', {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: {"email":"abc@mail.com","password":"password"}
})

导轨代码

#app/controllers/application_controller.rb
class ApplicationController < ActionController::API




  before_action :authenticate_request
     attr_reader :current_user

  before_action :set_headers
     private def authenticate_request
       @current_user = AuthorizeApiRequest.call(request.headers).result
       render json: { error: 'Not Authorized' }, status: 401 unless @current_user
     end

     private def set_headers
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Expose-Headers'] = 'ETag'
      headers['Access-Control-Allow-Methods'] = 'GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD'
      headers['Access-Control-Allow-Headers'] = '*,x-requested-with,Authorization,Content-Type,If-Modified-Since,If-None-Match'
      headers['Access-Control-Max-Age'] = '86400'
    end
   end

请帮我解决这个问题,我都是新手。

邮递员截图 邮递员截图

标签: ruby-on-railsrubyreactjs

解决方案


推荐阅读