module OmniAuth::MultiProvider::OmniAuthenticable

Public Class Methods

_oamp() click to toggle source
# File lib/omniauth/multiprovider/models/concerns/omni_authenticable.rb, line 12
def _oamp
  @_omniauth_multiprovider_config ||= OmniAuth::MultiProvider::Config.new
end
create_from_oauth(attributes, oauth_data) click to toggle source

Can be customized in each model

# File lib/omniauth/multiprovider/models/concerns/omni_authenticable.rb, line 52
def create_from_oauth(attributes, oauth_data)
  raise OmniAuth::MultiProvider::EmailTakenError if exists?(email: attributes[:email])
  create!(attributes)
end
from_oauth(omniauth_data, signed_in_resource=nil) click to toggle source
# File lib/omniauth/multiprovider/models/concerns/omni_authenticable.rb, line 16
def from_oauth(omniauth_data, signed_in_resource=nil)
  auth = _oamp.authentication_klass.normalize(omniauth_data)
  provider_name = auth.provider
  access_token = auth.credentials.token
  authentication = _oamp.authentication_klass.find_by(provider: provider_name, uid: auth.uid)

  resource = authentication.try :resource

  if resource and signed_in_resource
    signed_in_resource.oauth_already_bound resource, auth
    return signed_in_resource
  end
  unless resource
    attributes = oauth_to_attributes auth
    if signed_in_resource
      signed_in_resource.update_from_oauth attributes, auth
      resource = signed_in_resource
    else
      resource = create_from_oauth attributes, auth
    end
    _oamp.authentication_klass.from(auth, resource)
  end
  resource
end
oauth_to_attributes(oauth_data) click to toggle source

Can be customized in each model

# File lib/omniauth/multiprovider/models/concerns/omni_authenticable.rb, line 42
def oauth_to_attributes(oauth_data)
  attrs = {
    email: oauth_data.info[:email] || mock_email(oauth_data.provider, oauth_data.uid),
    password: Devise.friendly_token[0,20]
  }
  attrs[:password_confirmation] = attrs[:password]
  return attrs
end

Public Instance Methods

[](provider) click to toggle source
# File lib/omniauth/multiprovider/models/concerns/omni_authenticable.rb, line 64
def [](provider)
  find_by(provider: provider)
end
oauth_already_bound(other, oauth_data) click to toggle source

Handler for the case when there is already an Authentication Can be customized in each model

# File lib/omniauth/multiprovider/models/concerns/omni_authenticable.rb, line 72
def oauth_already_bound(other, oauth_data)
  if self == other
    # update
    self.update_from_oauth self.class.oauth_to_attributes(oauth_data), oauth_data
  else
    # raise error if the oauth data is bound to another resource
    raise MultiProvider::AlreadyBoundError.new self, other
  end
end
update_from_oauth(new_attrs, oauth) click to toggle source

Handler for the case when a signed_in_resource exists and is being authenticated Can be customized in each model

# File lib/omniauth/multiprovider/models/concerns/omni_authenticable.rb, line 84
def update_from_oauth(new_attrs, oauth)
  unless self.email.blank? || new_attrs[:email] == self.email
    # refusing to change existing email
    email = new_attrs.delete(:email)
    # but check if someone else is using it anyway
    raise OmniAuth::MultiProvider::EmailTakenError if User.exists?(email: email)
  end
  self.update!(new_attrs)
end