Created
August 22, 2013 22:46
-
-
Save jjasonclark/6313736 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 Logic | |
class Authentication | |
def initialize session_hash, model = ::Authentication | |
@session = session_hash | |
@model = model | |
end | |
def signed_in? | |
!user.nil? | |
end | |
def setup_for key | |
clear_state if key.nil? || session[:session_key] != key | |
session[:session_key] = key | |
end | |
def logout | |
setup_for nil | |
end | |
def has_authentication? | |
!session[:session_key].to_s.empty? | |
end | |
def user | |
return nil unless has_authentication? | |
@current_user ||= @authentication.try(:user) | |
@current_user ||= model.find_user_from_session(session[:session_key]) | |
end | |
def authentication | |
return nil unless has_authentication? | |
@authentication ||= model.for_session(session[:session_key]) | |
end | |
def authenticate_session params, auth_tokens | |
new_auth = find_or_create_authentication(params, auth_tokens) | |
if new_auth | |
new_auth.save_new_session_key! | |
setup_for new_auth.session_key | |
@authentication = new_auth | |
@current_user = new_auth.user | |
new_auth | |
else | |
logout | |
nil | |
end | |
end | |
private | |
attr_accessor :session, :model | |
def clear_state | |
@current_user = nil | |
@authentication = nil | |
end | |
def find_authentication_from_params params | |
return nil if params.nil? || params[:auth_key].to_s.empty? | |
model.find_from_identity_email(params[:auth_key]) | |
end | |
def find_or_create_authentication params, auth_tokens | |
new_authentication = find_authentication_from_params(params) | |
new_authentication ||= model.continue_setup_from_omniauth(auth_tokens) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment