[英]Rails Tutorial (M.Hartl) 3rd Edition, Chapter 8, How to unlock a database by closing a rogue `rails console`?
[英]Rails Tutorial (M.Hartl) 3rd Edition, Chapter 8, Why does the app fail to forget login status?
登录到应用程序,然后完全关闭浏览器后,我重新打开浏览器,预计将被注销。 但是我仍然登录吗?
我在8.2.3节中, 更改布局链接 ,除上述内容外,该应用程序的外观和运行均符合预期。 代码也与本教程中的一样。 我认为这些是相关文件:
应用程序/佣工/ sessions_helper.rb
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Returns the current logged-in user (if any).
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
end
应用程序/控制器/ sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
通常,关闭浏览器时session
清除session
,因此在这种情况下应自动注销。 但是,正如本脚注中所讨论的,有些浏览器仍然会记住您的会话,而这可能就是这里发生的情况。
登录到应用程序,然后完全关闭浏览器后,我重新打开浏览器,预计将被注销。 但是我仍然登录吗?
因为您仍在登录您的应用程序。 您的浏览器通过用于跟踪会话的cookie记住登录数据。
要注销,您可以使用本教程中说明的注销链接,也可以清理浏览器缓存。
因为您有一个cookie,所以您在每个页面会话中都拥有这样的方式,即创建方式。 它们被存储。 如果要删除,只需转到配置,删除cookie
根据Mhartl的回答,如果您使用的是Chrome浏览器,则关闭浏览器后,您将遇到未注销用户的情况(我不知道为什么会这样),但是当您通过firefox打开应用程序时,该用户将自动注销(再次) (不确定如何),如第8章所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.