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

标签 ruby-on-rails json authentication

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

相关文章:

javascript - 如何使用 keyup 处的 eventListener 输入值来过滤 JSON 数组

javascript - 通过 javascript 循环计算用户位置和许多目的地之间的驾驶距离

python - Tkinter 与 openweather

security - 使用证书 : Should the Certificates have a hostname? 的 SSL 身份验证

ruby-on-rails - Rails3 - 如何为嵌套资源制作提交按钮

Javascript window.open 返回未定义

javascript - 如何在 Rails 中将 javascript/jQuery 设置从一个页面传递到另一个页面?

ruby-on-rails - 连接表有两个复数词。如何正确命名模型和迁移;为了销毁连接表上的孤儿。事件记录

javascript - GAPI 客户端 404 错误

php - Lumen 中的用户身份验证