I am using omniauth to let users sign in with Facebook and Twitter accounts in a Ruby on Rails application. As Twitter request.env["omniauth.auth"] does not give the account’s email I construct it (the email cannot be NULL in the database). I have this piece of code:
if params[:provider] == 'facebook'
email = omniauth[:info]["email"]
elsif params[:provider] == 'twitter'
email = omniauth[:info]["name"] + '@example.com'
end
user = User.new(:email => email)
If I sign in with Facebook account it works perfect. But if I try with Twitter account the applicacion gives me this error:
SQLite3::ConstraintException: users.email may not be NULL [SQL query]
I checked if the email was NULL several times in case it was badly constructed or something, but is not NULL. omniauth-twitter and omniauth-facebook gems are installed.
Does anyone know why?
Let me know if you need more code or information.
I don’t like any of theses approaches to be honest. A name isn’t a unique constraint and a Twitter user can change his nickname at any time. What would happen if a user changed his nickname? Would he no longer be able to access his account? And what if another person changed his nickname to the now available nickname? Would he be able to access the other user’s account?
Two better solutions would be either:
Follow Ryan Bates’s approach and redirect the user to a form where he can type his email. Or
Override Devise’s method email_required?. Something like:
This way you wouldn’t introduce an extra step into the login process.