From 98f99954e7e88ac58f1f7420e40f448906f44fc9 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 25 Jan 2016 12:13:25 +0100 Subject: [PATCH] Corrects the logic dealing with confirmations of users via oauth --- app/controllers/users/registrations_controller.rb | 12 +++++++++--- app/models/user.rb | 12 +++++++----- ...160125100637_add_confirmed_oauth_email_to_user.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20160125100637_add_confirmed_oauth_email_to_user.rb diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 3a564f232..ad5f6abf7 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -30,10 +30,16 @@ class Users::RegistrationsController < Devise::RegistrationsController def do_finish_signup current_user.registering_with_oauth = false - current_user.validate - should_send_confirmation = current_user.errors.include? :email if current_user.update(sign_up_params) - current_user.send_confirmation_instructions if should_send_confirmation + + if current_user.confirmed_oauth_email != current_user.email + current_user.update(confirmed_at: nil) + current_user.send_confirmation_instructions + end + if current_user.confirmed_oauth_email.present? + current_user.update(confirmed_oauth_email: nil) + end + sign_in_and_redirect current_user, event: :authentication else render :finish_signup diff --git a/app/models/user.rb b/app/models/user.rb index 5fe0e77ea..44b13e52a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -51,15 +51,17 @@ class User < ActiveRecord::Base # Get the existing user by email if the provider gives us a verified email. def self.first_or_initialize_for_oauth(auth) - auth_email = auth.info.email if auth.info.verified || auth.info.verified_email - auth_email_user = User.find_by(email: auth_email) if auth_email.present? + oauth_email = auth.info.email + confirmed_oauth_email = oauth_email if auth.info.verified || auth.info.verified_email + oauth_user = User.find_by(email: confirmed_oauth_email) if confirmed_oauth_email.present? - auth_email_user || User.new( + oauth_user || User.new( username: auth.info.name || auth.uid, - email: auth_email, + email: oauth_email, + confirmed_oauth_email: confirmed_oauth_email, password: Devise.friendly_token[0,20], terms_of_service: '1', - confirmed_at: auth_email.present? ? DateTime.now : nil + confirmed_at: confirmed_oauth_email.present? ? DateTime.now : nil ) end diff --git a/db/migrate/20160125100637_add_confirmed_oauth_email_to_user.rb b/db/migrate/20160125100637_add_confirmed_oauth_email_to_user.rb new file mode 100644 index 000000000..660735ab2 --- /dev/null +++ b/db/migrate/20160125100637_add_confirmed_oauth_email_to_user.rb @@ -0,0 +1,5 @@ +class AddConfirmedOauthEmailToUser < ActiveRecord::Migration + def change + add_column :users, :confirmed_oauth_email, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 963cce889..2fdd31706 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160122153329) do +ActiveRecord::Schema.define(version: 20160125100637) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -398,6 +398,7 @@ ActiveRecord::Schema.define(version: 20160122153329) do t.integer "notifications_count", default: 0 t.string "locale" t.boolean "registering_with_oauth", default: false + t.string "confirmed_oauth_email" end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree