Table name: authentications
id :integer not null, primary key user_id :integer provider :string(255) not null uid :string(255) not null created_at :datetime updated_at :datetime university_id :integer email :string(255) auth :text
To create ominauth object based on provider (i.e. facebook/linkedin) to get its profile data in hash object.
# File app/models/authentication.rb, line 95 def self.create_omniauth_object(provider, token, uid) if provider == 'facebook' @graph = Koala::Facebook::API.new(token) begin u = @graph.get_object('me?fields=email,first_name,last_name,name,picture.type(large),link,verified,gender,locale,timezone', {}, api_version: "v2.2") rescue => e return JSON.parse(e.response_body) end OmniAuth::AuthHash.new( credentials: OmniAuth::AuthHash.new(token: token), extra: OmniAuth::AuthHash.new( raw_info: OmniAuth::AuthHash.new( email: u['email'], first_name: u['first_name'], gender: u['gender'], id: u['id'], last_name: u['last_name'], link: u['link'], locale: u['locale'], name: u['name'], timezone: u['timezone'] ) ), info: OmniAuth::AuthHash::InfoHash.new( email: u['email'], first_name: u['first_name'], image: u['picture']['data']['url'], last_name: u['last_name'], name: u['name'], urls: OmniAuth::AuthHash.new(:Facebook => u['link']), verified: u['verified'] ), provider: 'facebook', uid: u['id'] ) end end
Create authentication with ominauth using auth object.
# File app/models/authentication.rb, line 40 def self.create_with_omniauth(auth) create(uid: auth['uid'], provider: auth['provider'], email: auth.info.email, auth: auth) end
Find whether the authentication exists else it will create new one.
# File app/models/authentication.rb, line 27 def self.find_or_create_with_omniauth(auth, skip_auth=false) authentication = find_by(uid: auth['uid'], provider: auth['provider']) if authentication.present? if !skip_auth and auth.present? authentication.update_columns(auth: auth) end else authentication = create_with_omniauth(auth) end authentication end
Build user with omniauth using auth details from authentication object.
# File app/models/authentication.rb, line 45 def build_user_with_omniauth(email = nil, invitation_token = nil) user = User.find_by(email: email || auth.info.email) unless user self.build_user(type: 'Student').tap do |u| u.invitation_token = invitation_token unless invitation_token.blank? u.email = email || auth.info.email # u.password = nil ## DO NOT SET PASSWORD, OR ELSE ACTIVATION EMAIL WILL GO. u.name = auth.info.first_name u.last_name = auth.info.last_name avatar_uri = if auth.extra.raw_info.pictureUrls and auth.extra.raw_info.pictureUrls._total > 0 URI.parse(auth.extra.raw_info.pictureUrls['values'].first) elsif auth.info['image'] URI.parse(auth.info['image']) end if avatar_uri avatar_uri.scheme = 'https' u.build_profile_image(image: avatar_uri) end u.headline = auth.info.try(:headline) u.location = auth.info.try(:location) u.industry = auth.info.try(:industry) if auth.extra.raw_info.educations && auth.extra.raw_info.educations._total > 0 auth.extra.raw_info.educations['values'].each do |e| u.educations.build(field_of_study: e.try(:fieldOfStudy), degree: e.try(:degree), school_name: e.try(:schoolName), start_date: (Date.new(e.try(:startDate).try(:year)) rescue nil), end_date: (Date.new(e.try(:endDate).try(:year)) rescue nil)) end end if auth.extra.raw_info.positions && auth.extra.raw_info.positions._total > 0 auth.extra.raw_info.positions['values'].each do |p| u.positions.build(title: p.try(:title), university_name: p.try(:university).try(:name), description: p.try(:summary), is_current: p.try(:isCurrent), start_date: (Date.new(p.try(:startDate).try(:year), p.try(:startDate).try(:month)) rescue nil), end_date: (Date.new(p.try(:endDate).try(:year), p.try(:endDate).try(:month)) rescue nil)) end end if auth.extra.raw_info.honorsAwards && auth.extra.raw_info.honorsAwards._total > 0 auth.extra.raw_info.honorsAwards['values'].each do |a| u.awards.build(name: a.try(:name), issuer: a.try(:issuer), date_received: (Date.new(a.try(:date).try(:year), a.try(:date).try(:month)) rescue nil), description: a.try(:description)) end end if u.email == auth.info.email u.activation_state = 'active' u.activation_token = nil else u.send(:setup_activation) end end else self.user = user end self end
to check whether user has valid email Id or not.
# File app/models/authentication.rb, line 134 def has_valid_user_email? self.valid? return false unless self.user self.user.errors.messages[:email].blank? end