Created
July 4, 2012 14:44
-
-
Save Irio/3047718 to your computer and use it in GitHub Desktop.
OAuth2 with Devise
This file contains hidden or 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
class AddFacebookUidToUsers < ActiveRecord::Migration | |
def change | |
add_column :users, :facebook_uid, :string | |
end | |
end |
This file contains hidden or 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
# OAuth | |
config.omniauth :facebook, "306897372733415", "178333649c7a3ae4842038a110e36e19", | |
{:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}} |
This file contains hidden or 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
class OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def facebook | |
@user = User.find_or_create_from_auth_hash(request.env["omniauth.auth"]) | |
if @user.persisted? | |
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" | |
sign_in_and_redirect @user, :event => :authentication | |
else | |
session["devise.facebook_data"] = request.env["omniauth.auth"] | |
redirect_to new_user_registration_url | |
end | |
end | |
end |
This file contains hidden or 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
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" } |
This file contains hidden or 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
def self.find_or_create_from_auth_hash(auth) | |
user = self.where(facebook_uid: auth.uid).first | |
unless user | |
user = User.find_or_create_by_email( | |
facebook_uid: auth.uid, | |
email: auth.info.email, | |
password: Devise.friendly_token[0,20], | |
name: auth.info.name, | |
bio: auth.info.description, | |
city: auth.info.location, | |
facebook_link: "https://www.facebook.com/#{auth.info.nickname}" | |
) | |
user.remote_avatar_url = "https://graph.facebook.com/#{auth.info.nickname}/picture?type=large" | |
user.save | |
end | |
user | |
end |
This file contains hidden or 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
<%- if devise_mapping.omniauthable? %> | |
<ul> | |
<%- resource_class.omniauth_providers.each do |provider| %> | |
<li><%= link_to "#{t('app.devise.shared.links.sign.with')} #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider), class: "btn-login #{provider.to_s.downcase}" %></li> | |
<% end -%> | |
</ul> | |
<% end -%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment