I’m using omniauth-twitter and I have everything set up:
user.rb:
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.email = auth["info"]["email"]
# To pass password validation
user.password = user.password_confirmation = SecureRandom.urlsafe_base64(n=6)
end
end
sessions_controller.rb:
def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
The problem is that you can’t retrieve an email address from Twitter, so the login fails because of my validation rules:
user.rb:
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
I want to enable users to enter an email so they can login without failing the validation (only with Twitter, since I have Facebook and Google login too).
Does anyone have any suggestion?
EDIT:
users_controller.rb:
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
if params[:form_name] == "enter_email"
render 'enter_email'
else
render 'new'
end
end
end
users/enter_email.html.erb:
<% provide(:title, 'Enter your email') %>
<h1>Enter your email</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.hidden_field :provider, value: params[:oprovider] %>
<%= f.hidden_field :provider, value: params[:oprovider] %>
<%= f.hidden_field :uid, value: params[:ouid] %>
<%= f.hidden_field :name, value: params[:oname] %>
<%= f.hidden_field :password , value: params[:opassword] %>
<%= f.hidden_field :password_confirmation, value: params[:opassword_confirmation] %>
<% # To know to which form to redirect in case of validation error %>
<%= hidden_field_tag 'form_name', 'enter_email' %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
When the user is created it will be invalid since it will be missing the required email. So when the user save fails, you should put the information you retrieved from Twitter in the session and redirect the user to a registration page where he can input his email. Basically, this:
You’ll notice that we are discarding the
extrahash because it usually contains a lot of information that we don’t need. If you do need it, be careful because the session has a certain size limit and sometimes you can’t fit everything in there.Now you need to create a registrations controller with the
newaction (to show a page where the user will input his email) and thecreateaction (where you will use the Twitter info to further customize your new user).Finally, if the user is valid and saved then we can safely destroy the Twitter info from the session.