Created
January 5, 2011 03:24
-
-
Save tinogomes/765885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Authentication | |
def logged_in? | |
current_user.is_a?(User) | |
end | |
def current_user | |
@current_user ||= User.find_by_id(session[:user_id]) || :false | |
end | |
def current_user=(new_user) | |
if (new_user && new_user.is_a?(User) | |
session[:user_id] = new_user.id | |
@current_user = new_user | |
else | |
session[:user_id] = nil | |
@current_user = :false | |
end | |
end | |
# Inclusion hook to make #current_user and #logged_in? | |
# available as ActionView helper methods. | |
def self.included(base) | |
base.send :helper_method, :current_user, :logged_in? | |
end | |
end | |
class ApplicationController < ActionController::Base | |
include Authentication | |
end | |
class SessionController < ApplicationController | |
def destroy | |
reset_session # teoricamente, nem precisaria, pois no current_user= eu atribuo nil para session[:user_id] | |
self.current_user = nil | |
redirect_to root_url | |
end | |
end | |
# Já tentei session[:user_id] = nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment