lib/ninsho/authentication.rb in ninsho-0.0.1 vs lib/ninsho/authentication.rb in ninsho-0.0.2

- old
+ new

@@ -1,37 +1,43 @@ module Ninsho + # + # Responsible for manage the authentication process with the + # omniauth hash + # class Authentication - PARENT_RESOURCE_NAME = Ninsho.parent_resource_name.to_s.downcase - def initialize(omniauth = nil) @omniauth = omniauth @provider = omniauth['provider'] @uid = omniauth['uid'] + @oauth_token = @omniauth.credentials.token @email = omniauth['info']['email'] end def authenticated? user.present? end + # Little method to check if the record is find by the provider and uid def from_oauth - Ninsho.resource_class.find_by_provider_and_uid(@provider, @uid) + Ninsho.resource_class.where(@omniauth.slice(:provider, :uid)).first_or_initialize.tap do |resource| + resource.provider = @provider + resource.uid = @uid + resource.oauth_token = @oauth_token + resource.save if resource.respond_to?(Ninsho.parent_resource_name.to_s.downcase.to_sym) && !resource.new_record? + end end + # Method to create an authentication record when user is find, + # otherwise creates a user with the authentication def from_user user = Ninsho.parent_resource_name.send :find_by_email, @email - if user - user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid) - user.send(:save) - user - else - user = Ninsho.parent_resource_name.send :new, { email: @email } - user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid) - user.send(:save) - user - end + user = Ninsho.parent_resource_name.send :new, { email: @email } unless user + user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid, oauth_token: @oauth_token) + user.send(:save) + user end + # Check if a parent record is returned def user from_oauth.try(Ninsho.parent_resource_name.to_s.downcase.to_sym) || from_user end end end