Skip to content

Commit

Permalink
Fix users sign in (#192)
Browse files Browse the repository at this point in the history
Email should be optional. Some social networks do not provide an email address.

Issue: #171
  • Loading branch information
vitallium authored Feb 20, 2018
1 parent 0fa558d commit 422ddc6
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ class User < ApplicationRecord

phony_normalize :phone, as: :normalized_phone, default_country_code: "RU"

validates :phone, presence: true, if: :sms_reminders?
validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :phone, presence: true, phony_plausible: { country_code: "RU" }, if: :sms_reminders?
validates :email, uniqueness: { allow_blank: true, case_sensitive: false }
validates :role, presence: true
validates_plausible_phone :phone, country_code: "RU"

before_save :nullify_empty_email
before_save :downcase_email
Expand All @@ -45,26 +44,24 @@ class User < ApplicationRecord
scope :developers, -> { joins(:groups).where("groups.kind = 1") }

def self.from_omniauth!(auth)
social = SocialAccount.includes(:user).find_or_initialize_by(uid: auth.uid, provider: auth.provider)
auth_info = auth.info
user = User.find_or_initialize_by(email: auth_info.email)

if user.new_record?
user.email = auth_info.email
user.name = auth_info.name
user.first_name = auth_info.first_name
user.last_name = auth_info.last_name
user.remote_avatar_url = avatar_from_auth(auth)
end

social = SocialAccount.includes(:user).find_or_initialize_by(uid: auth.uid, provider: auth.provider)
if social.new_record?
social.link = SocialAccount.link_for(auth)
social.save
user.social_accounts << social
end

auth_info = auth.info
user = User.find_by(email: auth_info.email)

unless user.present?
user = social.build_user do |user|
user.email = auth_info.email
user.name = auth_info.name
user.first_name = auth_info.first_name
user.last_name = auth_info.last_name
user.remote_avatar_url = avatar_from_auth(auth)
end
user.save
end
user.save
user
end

Expand Down

0 comments on commit 422ddc6

Please sign in to comment.