Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ApplicationRecord
- # Include default devise modules. Others available are:
- devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable,
- :omniauthable, :omniauth_providers => [:facebook,:twitter,:linkedin]
- has_many :organizations_users
- has_many :auth_providers
- has_many :organizations, through: :organizations_users
- accepts_nested_attributes_for :auth_providers
- def active_for_authentication?
- # Uncomment the below debug statement to view the properties of the returned self model values.
- super && self.active && self.exp_alert == false
- end
- def self.from_omniauth(auth)
- exist = where(email: auth.info.email).first
- if exist
- existing_user = exist["id"]
- Auth_provider.where(provider: auth.provider, social_uid: auth.uid).first_or_create do |auth_provider|
- auth_provider.provider = auth.provider
- auth_provider.social_uid = auth.uid
- auth_provider.social_token = auth.credentials.token
- auth_provider.user_id = existing_user
- end
- else
- #@organization.users.create(first_name:params[:first_name])
- names = auth.info.name.strip.split(" ")
- first_name = names[0]
- last_name = names[1]
- abc = User.new (
- email: 'joe@joe.com',password:'safdasfd23',
- auth_providers_attributes: [
- { provider: 'Kari' },
- { social_token: 'The egalitarian assumption of the modern citizen' },
- { social_uid: 'safsdfsf' } # this will be ignored
- ]
- )
- abc.save
- #users.auth_providers.create(first_name:first_name,email,email:auth.info.email,provider:auth.provider)
- # user = where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
- # user.first_name = first_name
- # user.last_name = last_name
- # user.email = auth.info.email
- # user.password = Devise.friendly_token[0,20]
- # user.active = 'true'
- # user.admin = 'false'
- # user.exp_alert = 'false'
- # user.skip_confirmation!
- #end
- #user
- end
- end
- def self.new_with_session(params, session)
- super.tap do |user|
- if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
- user.email = data["email"] if user.email.blank?
- end
- end
- end
- def self.find_or_create_from_auth_hash(auth_hash)
- user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|
- user.first_name = auth_hash.info.nickname
- user.active = 'true'
- user.admin=='false'
- user.exp_alert = 'false'
- user.password = Devise.friendly_token[0,20]
- user.token = auth_hash.credentials.token
- user.email = auth_hash.info.email
- user.secret = auth_hash.credentials.secret
- user.skip_confirmation!
- end
- user
- end
- def self.linkedin_hash(auth_hash)
- user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|
- user.first_name = auth_hash.info.first_name
- user.last_name = auth_hash.info.last_name
- user.active = 'true'
- user.admin = 'false'
- user.exp_alert = 'false'
- user.password = Devise.friendly_token[0,20]
- user.token = auth_hash.credentials.token
- user.email = auth_hash.info.email
- user.skip_confirmation!
- end
- user
- end
- def inactive_message
- "Your Account has not been active yet."
- end
- def after_confirmation
- super
- self.update_attribute(:active, true)
- end
- end
- class Auth_provider < ApplicationRecord
- devise :database_authenticatable
- belongs_to :user
- accepts_nested_attributes_for :user
- end
Add Comment
Please, Sign In to add comment