pelargir / auto-session-timeout

Provides automatic session timeout in a Rails application.
MIT License
126 stars 63 forks source link

undefined current_user #5

Closed Lattuca closed 8 years ago

Lattuca commented 8 years ago

Hi,

In installed the auto-session-timeout gem and getting this error from the application.html.erb where this code was inserted.

<% if current_user %> <%= auto_session_timeout_js %> <% end %>

ActionView::Template::Error (undefined local variable or method `current_user' for #<#Class:0x007fc05c25ecd8:0x007fc04615b658>):

is there a location where the current_user variable is defined. I used the instructions given to implement the timeout feature.

Thank you

Carmelo

pelargir commented 8 years ago

current_user is generally going to be provided by whatever gem you're using for authentication. For example, if you're on Rails you could use Devise for this purpose. What are you using?

Lattuca commented 8 years ago

Hi Matthew, Thank you for your response. I am not using Devise, I am using the following:

Use ActiveModel has_secure_password

gem 'bcrypt', '~> 3.1.7' is there another variable that I should then use? thanks again!  Carmelo

  From: Matthew Bass <notifications@github.com>

To: pelargir/auto-session-timeout auto-session-timeout@noreply.github.com Cc: Carmelo Lattuca lattuca@yahoo.com Sent: Sunday, October 11, 2015 9:06 AM Subject: Re: [auto-session-timeout] undefined current_user (#5)

current_user is generally going to be provided by whatever gem you're using for authentication. For example, if you're on Rails you could use Devise for this purpose. What are you using?— Reply to this email directly or view it on GitHub.

pelargir commented 8 years ago

You must be storing the logged in user in the session somewhere, right? (Otherwise there's no sense in using an auto session timeout plugin.) Just define a current_user method that retrieves the current user from the session. Here's an example:

http://stackoverflow.com/questions/12719958/rails-where-does-the-infamous-current-user-come-from

Lattuca commented 8 years ago

Hi Matthew, I tried what was suggested in stackoverflow and not luck. This is what I have, hope this gives you more insight.

application.html.erb

<!DOCTYPE html>

  M2w   <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>   <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>   <%= csrf_meta_tags %> <%= render "layouts/header" %>   <% if current_user %>     <%= auto_session_timeout_js %>   <% end %> <%= yield %> <%= render "layouts/footer" %>

application controller class ApplicationController < ActionController::Base   # Prevent CSRF attacks by raising an exception.   # For APIs, you may want to use :null_session instead.   #  time out sessions   auto_session_timeout 5.minutes

  protect_from_forgery with: :exception

  # load global functions   require 'm2w'

  before_action :authorize

  protected

  def authorize     unless User.find_by(id: session[:user_id])       redirect_to login_path, notice: "Please log in"     else       @current_user = User.find_by(id: session[:user_id])       @user_full_name = "#{@current_user.first_name} #{@current_user.last_name}"     end   end

  def current_user    return unless session[:user_id]    @current_user ||= User.find_by(session[:user_id])  end

end  the following is the error: ActionView::Template::Error (undefined local variable or method current_user' for #<#<Class:0x007fc46fd48b78>:0x007fc46fd26af0>):     12: <%= render "layouts/header" %>     13:     14:     15:   <% if current_user %>     16:     <%= auto_session_timeout_js %>     17:   <% end %>     18:   app/views/layouts/application.html.erb:15:in_app_views_layouts_application_html_erb__981594431037048864_70240832482640'

Thank you :-) Carmelo

  From: Matthew Bass <notifications@github.com>

To: pelargir/auto-session-timeout auto-session-timeout@noreply.github.com Cc: Carmelo Lattuca lattuca@yahoo.com Sent: Sunday, October 11, 2015 9:59 AM Subject: Re: [auto-session-timeout] undefined current_user (#5)

You must be storing the logged in user in the session somewhere, right? (Otherwise there's no sense in using an auto session timeout plugin.) Just define a current_user method that retrieves the current user from the session. Here's an example:http://stackoverflow.com/questions/12719958/rails-where-does-the-infamous-current-user-come-from— Reply to this email directly or view it on GitHub.

pelargir commented 8 years ago

I'm sorry but what you're needing help with is beyond the scope of my simple plugin. I'd suggest getting in touch with a Ruby expert in your area who can walk you through the basics of setting up session-based login for your app. Sometimes connecting with a meetup group can be helpful too.

Lattuca commented 8 years ago

Hi Matthew, No problem, if I do not include the current user logic, the plugin works. Thank you Carmelo

  From: Matthew Bass <notifications@github.com>

To: pelargir/auto-session-timeout auto-session-timeout@noreply.github.com Cc: Carmelo Lattuca lattuca@yahoo.com Sent: Sunday, October 11, 2015 10:41 AM Subject: Re: [auto-session-timeout] undefined current_user (#5)

I'm sorry but what you're needing help with is beyond the scope of my simple plugin. I'd suggest getting in touch with a Ruby expert in your area who can walk you through the basics of setting up session-based login for your app. Sometimes connecting with a meetup group can be helpful too.— Reply to this email directly or view it on GitHub.