我有自定义 JSON 身份验证 API(没有设计等)。如何将 @user.authentication_token
渲染到响应 header 而不是正文?
在 sessions_controller.rb
def create
if @user && @user.authenticate(params[:user][:password])
@user.sign_in_count += 1
@user.save!
render status: 200,
json: { success: true,
info: "Logged in sucessfully.",
data: { auth_token: @user.authentication_token } }
else
render status: :unprocessable_entity,
json: { success: false,
info: "Login failed." }
end
end
最佳答案
试试 response.header["auth_token"]
def create
if @user && @user.authenticate(params[:user][:password])
@user.sign_in_count += 1
@user.save!
render status: 200,
json: { success: true,
info: "Logged in sucessfully."}
response.headers["auth_token"] = @user.authentication_token
else
render status: :unprocessable_entity,
json: { success: false,
info: "Login failed." }
end
end
关于ruby-on-rails - Rails 将 auth_token 渲染到 API 响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361206/