gpt4 book ai didi

ruby-on-rails - Rails 教程(M.Hartl)第 3 版,第 8 章,为什么应用程序无法忘记登录状态?

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

登录到应用程序然后完全关闭浏览器后,我重新打开浏览器,希望被注销。但是我仍然登录?

我在 8.2.3 Changing the layout links 部分 除上述情况外,该应用程序的外观和运行均符合预期。代码也与教程中的一样。我认为这些是相关文件:

app/helpers/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

app/controllers/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 ,如 this footnote 中所述。 ,这可能就是这里发生的事情。

关于ruby-on-rails - Rails 教程(M.Hartl)第 3 版,第 8 章,为什么应用程序无法忘记登录状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34948994/

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