Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do construct remember me after login ?

Minimalstics asked in Rails

In my login-in form in have a params 'remember me' . How do i read its value in controller . biggest problem i m facing right now.

<div class="row">
  <div class="col-md-offset-3 col-md-7" style="text-align: center;">
    <h2><b>Log In</b></h2>
  </div>
</div>
<div class="row">
  <div  class="col-md-offset-3 col-md-7">
    <%= form_with url: sessions_path do |form| %>
      <div class="form-group">
          <%= form.label :email %>
          <%= form.text_field :email, class: "form-control" %>
      </div>
      <div class="form-group">
          <%= form.label :password %>
          <%= form.text_field :password, class: "form-control" %>
      </div>
      <div class="field">
          <%= form.label :"remember_me" %>
          <%= form.check_box :remember_me %>
      </div>

     <%= form.submit "Login", class: "btn btn-primary btn-block" %>
    <% end %>
  </div>
</div>

Sessions Controoler

class SessionsController < ApplicationController
    def new
    end

  def create
     user = User.find_by_email(params[:email])
     if user && user.authenticate(params[:password])

       log_in user
       if params[:rememeber_me] == 1
               auth_token = SecureRandom.urlsafe_base64
                user.authentication_token = auth_token
                cookies.permanent[:auth_token] = auth_token
             else
                user.authentication_token = nil
                cookies.permanent[:auth_token] = nil
             end
       redirect_to user
     else
       flash.now[:danger] = 'Invalid email/password combination'
       render 'new'
     end
   end

   def destroy
    #  @current_user = nil
    #  reset_session
       cookies.delete(:auth_token)
       redirect_to root_path
   end

end

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 79,047+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.