简体   繁体   中英

how to achieve remember me functionality in rails 3 devise with a Sign Out option after Logged in to the rails 3 application?

sessions_controller.rb

def create
  user = User.find_by_email(params[:email])


  if user && user.authenticate(params[:password])
    if params[:remember_me]
      cookies.permanent[:auth_token] = user.auth_token
    else
      cookies[:auth_token] = user.auth_token
    end
    redirect_to root_url, :notice => "Logged in!"
  else
    flash.now.alert = "Invalid email or password"
    render "new"
  end
end

def destroy
  cookies.delete(:auth_token)
  redirect_to root_url, :notice => "Logged out!"
end

application_controller.rb

def current_user
  @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if 
  cookies[:auth_token]
end

user.rb

 def generate_token(column)
   begin
     self[column] = SecureRandom.urlsafe_base64
   end while User.exists?(column => self[column])
 end

and so on....... I have followed the Remember me Part from "Railcasts Episode #274 Remember Me & Reset Password".

configure routes

devise_for :users, path: "auth", path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }

and restart server.

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