【问题标题】:Session data not being saved in Sinatra simple authentication会话数据未保存在 Sinatra 简单身份验证中
【发布时间】:2013-11-26 19:09:16
【问题描述】:

我为 Sinatra 创建了一个简单的身份验证,但是会话对象似乎正在清理所有自定义密钥。例如,当用户登录时:

session[:user_id] = current_user.id

这有效地存储在当前请求的会话对象中。当发生新请求时, session[:user_id] 不再存在。会话处于活动状态,cookie 已启用。我尝试了所有我无法弄清楚的问题(这里是所有相关代码:https://gist.github.com/ksiomelo/7656296)。

应用:

 use Rack::Session::Cookie , :secret => "82e042cd6fde2bf1764f777236db799e"

 enable :sessions # for flash messages

帮手:

 def require_auth
  unless session[:user_id]
    flash[:error] = "You need to be logged in."
    redirect to("/login")
  end
end

def current_user
  @current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end

登录:

   authorized_user = User.authenticate(params[:email],params[:password])
      if authorized_user
        # update session / redirect
        session[:user_id] = authorized_user.mongo_id.to_s 
        session.options[:expire_after] = 2592000 unless params[:remember].nil? # 30 days

        # redirect to the wizard
        flash[:info] = "Welcome back #{authorized_user.first_name}"
        redirect to("/home")

【问题讨论】:

  • 尝试为会话处理设置提供程序,例如。 g.:use Rack::Session::Pool, :expire_after => 2592000
  • 显然有效,我在文档中错过了。将此作为解决方案发布,我会接受!谢谢!

标签: ruby sinatra rack


【解决方案1】:

您可能应该尝试为会话处理设置提供程序,例如。 g.:

use Rack::Session::Pool, :expire_after => 2592000

很高兴为您提供帮助。

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2016-09-02
    • 1970-01-01
    相关资源
    最近更新 更多