gpt4 book ai didi

ruby-on-rails - 使用 Rails 自动登录?

转载 作者:行者123 更新时间:2023-12-04 07:37:19 25 4
gpt4 key购买 nike

我正在尝试使用 Rails 的 Restful-Authentication 插件建立一个简单的身份验证系统,我只是想知道它是如何工作的,b/c 我似乎无法弄清楚 cookie 的要求是什么,以及如何使它如此浏览器始终记得您(6 个月以上)。

几个问题:

1)你如何为ruby的restful_authentication做remember_me's?我似乎找不到一个好的单线来解决这个问题......

如果用户注册并选中“记住我”,那么 Rails 应用程序如何在用户不做任何事情的情况下获取 session /cookie,只是在他们下次访问该页面时访问该页面,比如说 3 个月后?

2)我是否需要向服务器发送某种信息,例如他们的 IP 地址之类的?什么是cookies[:auth_token] ,在哪里定义的?

目标是:我不希望他们再次输入他们的电子邮件/密码,就像 StackOverflow 的工作方式一样:)

最佳答案

这是我们正在做的事情(主要来自经过身份验证的系统)......这是处理我们正在运行的登录的 Controller 方法......

def login
if logged_in?
flash[:notice] = "You are already logged in."
redirect_to "/" and return
end
unless request.post?
render :layout => 'task' and return
end
self.current_user = User.authenticate(params[:login], params[:password])
if logged_in?
if params[:remember_me].to_i == 1
self.current_user.remember_me
cookies[:auth_token] = {:domain => "#{DOMAIN}", :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
else
self.current_user.forget_me
cookies.delete(:auth_token, :domain => "#{DOMAIN}")
cookies[:auth_token] = nil
end
current_user.last_seen_at = Time.now
current_user.save
session[:notice] = "You logged in successfully"
flash[:notice] = "You logged in successfully"
redirect_back_or_default(:controller => 'dashboard') and return
#redirect_back_or_default(:controller => 'index', :action => 'index') and return
else
if $failed_login_counter.add_attempt(params[:login]) > MAXIMUM_LOGIN_ATTEMPTS
logger.info("login rate limiter kicking in, #{MAXIMUM_LOGIN_ATTEMPTS} login attempts failed")
redirect_to "/denied.html" and return
end
flash[:error] = "Unable to authenticate username and password"
render(:layout => 'task') and return
end
end

并使用它来注销
def logout
current_user.last_seen_at = Time.now
current_user.save
self.current_user.forget_me if logged_in?
cookies.delete(:auth_token, :domain => "#{DOMAIN}")
reset_session
flash[:notice] = "You have been logged out."
#redirect_to :back
redirect_back_or_default(:controller => 'index', :action => 'index') and return
end

然后 - 在您的 application.rb 中,您将需要以下内容:
before_filter :login_from_cookie

def login_from_cookie
return unless cookies[:auth_token] && !logged_in?
user = User.find_by_remember_token(cookies[:auth_token])
if user && user.remember_token?
user.remember_me
self.current_user = user
cookies[:auth_token] = { :domain => "#{DOMAIN}", :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
flash[:notice] = "#{self.current_user.login}, you have logged in successfully"
end
end

并且 - 在您的 User 模型中有一些这样的方法:
# Encrypts some data with the salt.
def self.encrypt(password, salt)
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end

# Encrypts the password with the user salt
def encrypt(password)
self.class.encrypt(password, salt)
end

def remember_token?
remember_token_expires_at && Time.now.utc < remember_token_expires_at
end

# These create and unset the fields required for remembering users between browser closes
def remember_me
self.remember_token_expires_at = 2.weeks.from_now.utc
self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
save(false)
end

def forget_me
self.remember_token_expires_at = nil
self.remember_token = nil
save(false)
end

关于ruby-on-rails - 使用 Rails 自动登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1746155/

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