简体   繁体   中英

Ruby on Rails Remember Me Log In Not Working

I am following the following tutorial:

http://railscasts.com/episodes/274-remember-me-reset-password

And have just tried to add the 'remember me' functionality, so only up to around 4 minutes of the tutorial.

Everything seems fine but once I try to log in it just goes back to the home page and no one is logged in.

User.rb:

before_create { generate_token(:auth_token) 

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

sessions_controller:

def create
    user = User.find_by email: params[:email]
    if user and user.authenticate params[:password]
        # 'logging in' is performed by saving a user's id in the session variable
        #session[:user_id] = user.id
        #cookies.permanent[:auth_token] = user.auth_token
        if params[:remember_me]
          cookies.permanent[:auth_token] = user.auth_token
        else
          cookies[:auth_token] = user.auth_token
        end
        # redirect to last page or root_path (products/index.html.erb')
        redirect_to root_path
    else
        flash.now.notice = "Invalid user/password"
        render :new
    end
end


def destroy     
    # the act of logging out is performed by simply setting the key (:user_id)      
    # in the session hash to a value of nil (nothing)                   
    #session[:user_id] = nil
    cookies.delete(:auth_token)     
    # redirect to root_path (products/index.html.erb') 
    redirect_to root_path 
end

application_controller:

def authenticate_user
    # if session[:user_id]
        # @current_user = User.find session[:user_id]
    # else
        # @current_user = nil
    # end
    if cookies[:auth_token]
        @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) 
    else
        @current_user = nil
    end
end

def logged_in?      
    not session[:user_id].nil?  
end 

views/sessions/new.html.erb:

<%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
<!-- DRAW HTML CONTENT FOR PAGE -->
<%= form_tag sessions_create_path, :method => :get do %><div id='wrapper'>
    <div id="contentWrapper">
        <div id="contentWrapper">
            <h1 class="page_header">THOR CINEMAS:<div class="file_selector"><span class="darker">LOG IN</span></div></h1>
            <div id="content">
                <!-- FULL PAGE CATEGORY CONTAINER -->
                    <div class='category'>
                        <div class='large_panel'>
                           <%= image_tag "thor_hammer.jpg",:size => "900x250" %>
                           <h1>Log IN:</h1><table>
                           <tr>
                                <td>
                                    <table>
                                        <tr>
                                            <td width="300px">
                                                <div class='info'>
                                                    <%= label_tag :email,'EMAIL:' %>
                                                </div> 
                                            </td>
                                            <td>
                                               <div class='info'>
                                                   <%= label_tag :password, 'PASSWORD:' %>
                                               </div> 
                                           </td>
                                           <td></td>
                                           <td></td>
                                       </tr>

                                       <tr>
                                              <td style="padding-right:45px"><%= text_field_tag :email, params[:email] , :autofocus => true, :size => 42 %> </td>
                                              <td style="padding-right:45px"><%= password_field_tag :password, params[:password], :size => 42 %></td>
                                               <div class="field">
                                                  <%= check_box_tag :remember_me, 1, params[:remember_me] %>
                                                  <%= label_tag :remember_me %>
                                                </div>
                                       </tr>

                                    </table><br><p style="color:red"><%= flash[:notice] %></p>
                                </td>
                                <td>
                                    <table>
                                        <tr>
                                            <td> 
                                                <div class='info'>
                                                    OPTIONS:
                                                </div> 
                                            </td>
                                            <td>

                                            </td>
                                            <td></td>
                                            <td></td>
                                        </tr>

                                        <tr>
                                            <td><%= submit_tag "Login" %></br></td>
                                            <td><%= link_to "Register", new_user_path %></td>
                                        </tr>

                                    </table><br>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>  
    </div>
</div><% end %>
</body>   
</html>

Films_controller:

def index
    @films = Film.all
    if logged_in?
        @colour = Perference.find_by(user_id: session[:user_id]).colour
    end
end

Can someone please tell me what the problem is.

Your problem is that you are still utilizing the session[:user_id] throughout your controllers, you need to change the use session[:user_id] to cookie[:auth_token] . The places where you use the session[:user_id] for the current user's id, you can write:

user_id = User.find_by_auth_token!(cookies[:auth_token]).id

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