Cole Phiper
Cole Phiper

Reputation: 349

How do I retrieve/iterate over and save the users positions to a model w/LinkedIn? omniauth-linkedin-oauth2 Rails 5.2

I've been able to complete the oauth2 login process and grant the current_user access to the application with Devise. My main goal was to iterate over the user's positions from LinkedIn and create a Position.create(..) object for each individual position & then save their user_id to the object after a successful LinkedIn login attempt. Afterwards, I would just display the information inside of the app. It should check for a Position where its first_or_create, to avoid duplicating the data on each login attempt. How can I get the user's positions and company hash (JSON object) to store inside of the database?

I created a position model with a company JSON serializable attribute. I attempted to retrieve the user's profile using the auth.extra.info.positions fields. Afterwards, I tried to retrieve the information from the user in session. I could not succeed in getting an array of Positions back.

create_positions.rb | migration

class CreatePositions < ActiveRecord::Migration[5.2]
  def change
    create_table :positions do |t|
      t.string :title
      t.text :summary
      t.date :start_date
      t.date :end_date
      t.boolean :is_current
      t.json :company
      t.references :user, foreign_key: true

      t.timestamps
    end
  end
end

user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :omniauthable, :trackable, :confirmable,
         :omniauth_providers => [:twitter, :facebook, :linkedin, :google_oauth2,
                                 *(:developer if Rails.env.development?)]

  has_many :positions

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.name = auth.info.name
      user.password = Devise.friendly_token[0, 20]
      user.provider = auth.provider
      user.uid = auth.uid
    end
  end

  protected
  def confirmation_required?
    false
  end
end

position.rb

class Position < ApplicationRecord
  serialize :company, JSON
  belongs_to :user, dependent: :destroy
 end

omniauth_callbacks_controller.rb

# frozen_string_literal: true

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  skip_before_action :verify_authenticity_token

  def sign_in_with(provider_name)
    @user = User.from_omniauth(request.env['omniauth.auth'])
    sign_in_and_redirect @user, event: :authentication
    set_flash_message(:notice, :success, kind: provider_name) if is_navigational_format?
  end

  def save_user_positions
     Position.create(:title => auth.extra.info.positions['title'], :summary => auth.extra.info.positions['summary'], :start_date => auth.extra.info.positions['start-date'], :end_date => auth.extra.info.positions['end-date'], :is_current => auth.extra.info.positions['is-current'], company: {id: auth.extra.info.positions.company['id']})

end def facebook sign_in_with 'Facebook' end

  def linkedin
    sign_in_with 'LinkedIn'
  end

  def twitter
    sign_in_with 'Twitter'
  end

  def google_oauth2
    sign_in_with 'Google'
  end

  def developer
    sign_in_with 'Developer'
  end
end

Expected Results: When User logs into LinkedIn through omniauth, their positions/companies are saved to the rails database.

Actual Results: Only user email/name/basic attributes are retrievable.

Upvotes: 0

Views: 47

Answers (0)

Related Questions