简体   繁体   中英

How to implement a “Remember Me” function in Rails 3?

What are the best practices to implement a "Remember Me" function in Rails 3 application ?

I store session information (session id + user id) in the database when user logs in, and I don't want to use any plugins at this moment.

Any pointers or code samples will be much appreciated.

You can just set the expiration on a signed cookie to accomplish this. (Signed cookies are tamper-proof just like the Rails-provided session cookie.)

class SessionsController < ApplicationController

  def create
    ...
    user = User.authenticate(params[:email_address], params[:password])

    if params[:remember_me]
      cookies.signed[:user_id] = { value: user.id, expires: 2.weeks.from_now }
    else
      # expires at the end of the browser session
      cookies.signed[:user_id] = user.id
    end
  end

  def destroy
    cookies.delete :user_id
  end
end 

class ApplicationController < ActionController::Base
  ...
  def current_user
    User.find(cookies.signed[:user_id])
  end     
end

Railscasts has an episode on achieving this as well as well as a great HOWTO on implementing those features via BDD with RSpec and Capybara .

I store session information (session id + user id) in the database when user logs in

I believe that's one approach and the casts above does the same with cookies by issuing each User account a unique authentication token.

Have been reading the Rails tutorial book and it has an implementation for Remember Me

You can check for some hints (The implementation may be different from yours)

http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sec:remember_me

This is how I implemented remember_me (the below snippet is from my example Rails app on authentication ):

class SessionsController < ApplicationController
  skip_before_filter :login_required, :only => [:new, :create]

  def new
  end

  def create
    @current_user = User.authenticate(params[:email], params[:password])

    if @current_user
      @current_user.track_on_login(request)

      if params[:remember_me]
        cookies[:remember_token] = { :value => @current_user.remember_token, :expires => 24.weeks.from_now }
      else
        cookies[:remember_token] = @current_user.remember_token
      end

      redirect_to dashboard_url, :notice => "Logged in successfully."
    else
      flash.now[:alert] = "Invalid login or password."
      render 'new'
    end
  end

  def destroy
    current_user.track_on_logout
    current_user.reset_remember_token_and_save  # can't rely on the 'save_current_user_if_dirty' after_filter here

    cookies.delete(:remember_token)
    reset_session

    redirect_to root_url, :notice => "You have been logged out."
  end
end

Just an example without salt:

class ApplicationController < ActionController::Base

  protected

  def signin!(user_id)
    return unless user_id
    @current_user = User.find(user_id)
    self.session_user_id = @current_user.id
    self.permanent_user_id = @current_user.id if session[:accept_remember_me]
  end

  def signout!
    self.session_user_id = nil
    self.permanent_user_id = nil
    session[:accept_remember_me] = nil
    @current_user = nil
  end

  def remember_me
    session[:accept_remember_me] = true
  end

  private

  def permanent_user_id
    cookies.signed[:permanent_user_id]
  end

  def permanent_user_id= value
    cookies.permanent.signed[:permanent_user_id] = value
  end

  def session_user_id
    session[:user_id]
  end

  def session_user_id= value
    session[:user_id] = value
  end

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM