Search code examples
ruby-on-railsruby-on-rails-4google-oauthoauth-2.0

How do I prevent a duplicate user from getting created in my Rails application?


I’m using Rails 4.2.3. I want to use Google and Facebook to authenticate (and create) users in my system. So I have this in my app/models/user.rb file …

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end

The problem with the above is if I already have a row in my database, like below

 id |   provider    |          uid          |     name      | oauth_token | oauth_expires_at |         created_at         |         updated_at         |          email          
----+---------------+-----------------------+---------------+-------------+------------------+----------------------------+----------------------------+-------------------------
  5 | google-oauth2 | 777711643329020555465 | Dave A        |             |                  | 2016-05-10 20:08:59.182744 | 2016-05-10 20:08:59.182744 | [email protected]

upon logging in with Google and invoking the above method, a second row is created with identical data. Does anyone know what I can do to tweak things such that if there is already a row in my database for the user, invoking the above method won’t create a second, duplicate user.

Thanks, - Dave

Edit: Here is what the db looks like after logging in via Google OAuth

 id |   provider    |          uid          |     name      |                                 oauth_token                                 |  oauth_expires_at   |         created_at         |         updated_at         |          email          
----+---------------+-----------------------+---------------+-----------------------------------------------------------------------------+---------------------+----------------------------+----------------------------+-------------------------
  5 | google-oauth2 | 777711643329020555465 | Dave A        |                                                                             |                     | 2016-05-10 20:08:59.182744 | 2016-05-10 20:08:59.182744 | [email protected]
  7 | google_oauth2 | 777711643329020555465 | D. A.         | ya29.CjLeAiJ--FTY1UhMwXdb7yk74hhdCH4O6sz3ewbG8wBNdmRHAJWZ0esoS0yZLp5fXmS60Q | 2016-05-10 21:49:55 | 2016-05-10 20:49:57.277341 | 2016-05-10 20:49:57.277341 | [email protected]

Solution

  • You shouldn't use save, first_or_create will save it for you if it won't find the match, see http://apidock.com/rails/ActiveRecord/Relation/first_or_create

    So just remove this: user.save!