ruby-on-rails - 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
请帮我解决这个问题,我都是新手。
解决方案
推荐阅读
- ios - 获取错误线程 1: EXC_BAD_ACCESS (code=1, address=0x20)
- android - BottomSheetBehavior 填充
- c# - dotnet watch run 命令中的绝对路径不起作用
- terminal - 使用 python 以编程方式更改 linux 终端中的字体大小
- three.js - 三.js setHSL() 设置意想不到的颜色
- code-first - 相关实体查询
- c - 在 windows 上添加 openssl c 语言库
- mongodb - MongoDB - 同时找到 3 个平均值
- dynamics-crm - Dynamics CRM 2013 组织状态待定
- c# - Page_Load() 保存到数据库