2014-11-06 18:30:53 +01:00
|
|
|
module SessionsHelper
|
|
|
|
|
|
|
|
def log_in(user)
|
|
|
|
session[:user_id] = user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
@current_user ||= User.find_by(id: session[:user_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def logged_in?
|
|
|
|
!current_user.nil?
|
|
|
|
end
|
2014-11-06 18:56:00 +01:00
|
|
|
|
|
|
|
def log_out
|
|
|
|
session.delete(:user_id)
|
|
|
|
@current_user = nil
|
|
|
|
end
|
2014-11-06 22:53:11 +01:00
|
|
|
|
|
|
|
# Redirects to stored location (or to the default).
|
|
|
|
def redirect_back_or(default)
|
|
|
|
redirect_to(session[:forwarding_url] || default)
|
|
|
|
session.delete(:forwarding_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Stores the URL trying to be accessed.
|
|
|
|
def store_location
|
|
|
|
session[:forwarding_url] = request.url if request.get?
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def current_user?(user)
|
|
|
|
user == current_user
|
|
|
|
end
|
2014-11-06 18:56:00 +01:00
|
|
|
|
2014-11-06 18:30:53 +01:00
|
|
|
end
|