mardi 12 janvier 2016

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".

Aucun commentaire:

Enregistrer un commentaire