Corrects the logic dealing with confirmations of users via oauth

This commit is contained in:
kikito
2016-01-25 12:13:25 +01:00
parent 248bff712c
commit 98f99954e7
4 changed files with 23 additions and 9 deletions

View File

@@ -30,10 +30,16 @@ class Users::RegistrationsController < Devise::RegistrationsController
def do_finish_signup def do_finish_signup
current_user.registering_with_oauth = false current_user.registering_with_oauth = false
current_user.validate
should_send_confirmation = current_user.errors.include? :email
if current_user.update(sign_up_params) 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 sign_in_and_redirect current_user, event: :authentication
else else
render :finish_signup render :finish_signup

View File

@@ -51,15 +51,17 @@ class User < ActiveRecord::Base
# Get the existing user by email if the provider gives us a verified email. # Get the existing user by email if the provider gives us a verified email.
def self.first_or_initialize_for_oauth(auth) def self.first_or_initialize_for_oauth(auth)
auth_email = auth.info.email if auth.info.verified || auth.info.verified_email oauth_email = auth.info.email
auth_email_user = User.find_by(email: auth_email) if auth_email.present? 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, username: auth.info.name || auth.uid,
email: auth_email, email: oauth_email,
confirmed_oauth_email: confirmed_oauth_email,
password: Devise.friendly_token[0,20], password: Devise.friendly_token[0,20],
terms_of_service: '1', terms_of_service: '1',
confirmed_at: auth_email.present? ? DateTime.now : nil confirmed_at: confirmed_oauth_email.present? ? DateTime.now : nil
) )
end end

View File

@@ -0,0 +1,5 @@
class AddConfirmedOauthEmailToUser < ActiveRecord::Migration
def change
add_column :users, :confirmed_oauth_email, :string
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -398,6 +398,7 @@ ActiveRecord::Schema.define(version: 20160122153329) do
t.integer "notifications_count", default: 0 t.integer "notifications_count", default: 0
t.string "locale" t.string "locale"
t.boolean "registering_with_oauth", default: false t.boolean "registering_with_oauth", default: false
t.string "confirmed_oauth_email"
end end
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree