Last active
December 15, 2015 22:08
-
-
Save raphaelcm/5330436 to your computer and use it in GitHub Desktop.
Integrating with LinkedIn using Rails, Mongoid, Devise, Omniauth, LinkedIn Gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/devise.rb | |
# ==> OmniAuth | |
# Add a new OmniAuth provider. Check the wiki for more information on setting | |
# up on your models and hooks. | |
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo' | |
require "omniauth-linkedin" | |
config.omniauth :linkedin, "your-app-id", "your-app-secret", :scope => 'r_basicprofile r_emailaddress rw_nus' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gemfile | |
gem "linkedin" | |
gem "omniauth" | |
gem "omniauth-linkedin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LinkedinFactory.add_share(user, something_to_share) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user/linkedin_connection.rb | |
class User::LinkedinConnection | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
embedded_in :user | |
field :token | |
field :secret | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/linkedin_factory.rb | |
class LinkedinFactory | |
def self.authorize_user(user) | |
token = user.user_linkedin_connection.token | |
secret = user.user_linkedin_connection.secret | |
client = LinkedIn::Client.new(LinkedIn.token, LinkedIn.secret) | |
client.authorize_from_access(token, secret) | |
client | |
end | |
def self.add_share(user, update) | |
client = self.authorize_user(user) | |
client.add_share(update) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/omniauth.rb | |
OmniAuth.config.logger = Rails.logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/omniauth_callbacks_controller.rb | |
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def linkedin | |
if current_user | |
connect_to_linkedin | |
else | |
sign_in_via_linkedin | |
end | |
end | |
private | |
def connect_to_linkedin | |
if current_user.connect_to_linkedin(request.env["omniauth.auth"]) | |
set_flash_message(:notice, :success, :kind => "LinkedIn") | |
else | |
set_flash_message(:notice, :failure, :kind => "LinkedIn") | |
end | |
redirect_to root_url | |
end | |
def sign_in_via_linkedin | |
@user = User.find_for_linkedin_oauth(request.env["omniauth.auth"]) | |
if @user | |
@user.connect_to_linkedin(request.env["omniauth.auth"]) | |
sign_in_and_redirect @user, :event => :authentication | |
set_flash_message(:notice, :success, :kind => "LinkedIn") if is_navigational_format? | |
else | |
flash[:notice] = "Couldn't find a user connected to your LinkedIn account. Please sign in and then connect your account to LinkedIn." | |
redirect_to new_user_session_url | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
devise :omniauthable | |
## Omniauthable | |
field :provider | |
field :uid | |
embeds_one :user_linkedin_connection, :class_name => 'User::LinkedinConnection' | |
def connect_to_linkedin(auth) | |
self.provider = auth.provider | |
self.uid = auth.uid | |
self.user_linkedin_connection = User::LinkedinConnection.new(:token => auth["extra"]["access_token"].token, :secret => auth["extra"]["access_token"].secret) | |
unless self.save | |
return false | |
end | |
true | |
end | |
def disconnect_from_linkedin! | |
self.provider = nil | |
self.uid = nil | |
self.user_linkedin_connection = nil | |
self.save! | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= link_to "Connect to LinkedIn", user_omniauth_authorize_path(:linkedin) | |
= link_to "Sign in with LinkedIn", user_omniauth_authorize_path(:linkedin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment