[英]Rails render auth_token to API response header
I have custom JSON authentication API (without devise, etc). 我有自定义JSON身份验证API(没有设计等)。 How do I render @user.authentication_token
to response header instead of body? 如何将@user.authentication_token
呈现给响应头而不是body?
in sessions_controller.rb
在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
Try the response.header["auth_token"] 试试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
There is a headers
object available in your controller action. 控制器操作中有一个headers
对象。 So you can do the following: 所以你可以做到以下几点:
headers['X-User-Token'] = @user.authentication_token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.