从JavaScript向Rails发送请求并向Authorization
头提供令牌的情况在我的Rails API上始终显示为空头。
我的所有API控制器都有以下基本代码:
module Api
class BaseController < ActionController::API
before_action :require_login
private
def require_login
unless signed_in?
head :unauthorized
end
end
def signed_in?
current_user.present?
end
def current_user
if request.headers['Authorization'].present?
User.find_by(token: request.headers['Authorization'])
else
nil
end
end
end
结束
在javascript端执行我的提取请求,如下所示:
fetch(`/api/clients?page=${page}`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
'Authorization': AUTH_TOKEN
},
credentials: 'same-origin',
})
从Authorization
获取值request.headers
总是为零。
有人知道怎么了吗?