Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix users sign in #192

Merged
merged 1 commit into from
Feb 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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