"bcrypt" 并且我需要将 authenticity_tok-6ren">
gpt4 book ai didi

ruby-on-rails - 从 Rails 获取 authenticity_token

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:02 24 4
gpt4 key购买 nike

我在 Ruby 中进行了一些身份验证(来自 tutorial - 这使用 gem "bcrypt-ruby", :require => "bcrypt" 并且我需要将 authenticity_token 写入 json 文件。如何我可以得到它并创建这个文件吗?

更新:我写过:

  json.array!(@session) do |session|
json.extract! session, :csrf-token
json.url tag_url(session, format: :json)
end

但它不起作用。我需要从 html 写

<meta content="authenticity_token" name="csrf-param" /> <meta content="74Mhbpn9FF/tY/cgfuVmX7ribN4rOkkdUjSgbLNsces=" name="csrf-token" />

这个值:74Mhbpn9FF/tY/cgfuVmX7ribN4rOkkdUjSgbLNsces=

这是我的 session Controller :

class SessionsController < ApplicationController
def new
end

def index
@session
end

def create
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end

def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end

最佳答案

有一个名为 form_authenticity_token 的 View 助手,它可以像这样访问或分配 token

def form_authenticity_token
session[:_csrf_token] ||= SecureRandom.base64(32)
end

要在 Controller 中获取它,请使用 session[:_csrf_token]

关于ruby-on-rails - 从 Rails 获取 authenticity_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22821368/

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