omniauth / omniauth-identity

A simple login and password strategy for OmniAuth.
MIT License
345 stars 98 forks source link

Enable user to sign in using mobile no. or email both. #55

Closed aman199002 closed 11 years ago

aman199002 commented 11 years ago

I am using email address for signing in a user. I want to enable user to sign in via both email or mobile no.

In omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :identity, :fields => [:name, :email, :mobile_no], :on_failed_registration => IdentitiesController.action(:new)    
  OmniAuth.config.on_failure = Proc.new { |env|
    OmniAuth::FailureEndpoint.new(env).redirect_to_failure
  }
end

In signup form

= form_tag '/auth/identity/register', :class => 'form-stacked' do
  = text_field_tag :name, params[:name], :placeholder => 'Enter Your Name ... ', :required => true,:class=>'uneditable-input.span6'
  = text_field_tag :email,  params[:email], :placeholder => 'Enter Your Email Address ... ', :required => true ,:class=>'uneditable-input.span6'
  = text_field_tag :mobile_no,  params[:mobile_no], :placeholder => 'Enter Your Mobile No ... ', :required => true ,:class=>'uneditable-input.span6'
  = password_field_tag :password,  nil, :placeholder => 'Create Your Password ... ', :required => true ,:class=>'uneditable-input.span6'
  = password_field_tag :password_confirmation,  nil, :placeholder => 'Confirm Your Password ... ', :required => true ,:class=>'uneditable-input.span12'
    .actions style="width: 250px;"
  = submit_tag 'Sign Up', :class => 'btn btn-primary uneditable-input.span6 '

And when i recieve auth hash in session controller, mobile no is not present

env["omniauth.auth"]["info"] = #<OmniAuth::AuthHash::InfoHash email="abc@example.com" name="abc">

Is there any way to signin using both email and mobile no. And also get mobile no. parameter in auth hash to save in associated user record.

aman199002 commented 11 years ago

I have solved the problem by overriding default authenticate method in identity model.

In identity.rb

def self.authenticate(auth_key_param, password)    
  return nil if auth_key_param.nil? or password.nil?
  if auth_key_param.match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
    self.find_by_email(auth_key_param).try(:authenticate, password)
  else
    self.find_by_mobile_no(auth_key_param).try(:authenticate, password)
  end
end

It will check if the auth_key parameter is email or mobile_no using regular expression and then will try to authenticate according to parameter type .

Mobile no. can also be received within auth_hash. The auth_hash schema is mentioned here:

https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema

It has an info hash containing information about the user which is having a key named phone. So i have created an alias attribute.

alias_attribute :phone, :mobile_no

Now the mobile no can also be recieved inside auth_hash as key phone that can be saved in associated user record.

auth['info']['phone'] = "1234567891"
aman199002 commented 11 years ago

Problem resolved. Closing the issue.