gpt4 book ai didi

ruby-on-rails - Rails 将 auth_token 渲染到 API 响应 header

转载 作者:行者123 更新时间:2023-12-04 06:31:56 25 4
gpt4 key购买 nike

我有自定义 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com