-
-
Save vquaiato/3711796 to your computer and use it in GitHub Desktop.
Setup for Devise + Omniauth (mongoid and facebook-only)
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
#add this to your gemfile | |
gem 'mongoid' | |
gem 'devise' | |
gem "omniauth-facebook" |
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/config/initializers/devise.rb | |
Devise.setup do |config| | |
... | |
config.omniauth :facebook, "KEY", "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
class OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def facebook | |
oauthorize "Facebook" | |
end | |
private | |
def oauthorize(kind) | |
@user = find_for_ouath(kind, env["omniauth.auth"], current_user) | |
if @user | |
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => kind | |
session["devise.#{kind.downcase}_data"] = env["omniauth.auth"] | |
sign_in_and_redirect @user, :event => :authentication | |
end | |
end | |
def find_for_ouath(provider, access_token, resource=nil) | |
user, email, name, uid, auth_attr = nil, nil, nil, {} | |
case provider | |
when "Facebook" | |
uid = access_token['uid'] | |
email = access_token['info']['email'] | |
auth_attr = { :uid => uid, :token => access_token['credentials']['token'], :secret => nil, :name => access_token['info']['name'], :link => access_token['info']['link'] } | |
else | |
raise 'Provider #{provider} not handled' | |
end | |
if resource.nil? | |
if email | |
user = find_for_oauth_by_email(email, resource) | |
else | |
raise "Email not provided" | |
end | |
else | |
user = resource | |
end | |
auth = user.authorizations.select{|a| a.provider == provider} | |
if auth.nil? or auth.blank? | |
auth = user.authorizations.build(:provider => provider) | |
user.authorizations << auth | |
user.save | |
end | |
auth.update_attributes auth_attr | |
return user | |
end | |
def find_for_oauth_by_email(email, resource=nil) | |
if user = User.find_by(email: email) | |
user | |
else | |
user = User.new(:email => email, :password => Devise.friendly_token[0,20]) | |
user.password_confirmation = user.password | |
user.save! | |
end | |
return user | |
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
devise_for :users, :path => "accounts", :controllers => { :omniauth_callbacks => "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
class User | |
include Mongoid::Document | |
devise :database_authenticatable, | |
:trackable, :validatable, :omniauthable | |
## Database authenticatable | |
field :name, :type => String, :default => "" | |
field :email, :type => String, :default => "" | |
field :encrypted_password, :type => String, :default => "" | |
validates_presence_of :email | |
validates_presence_of :encrypted_password | |
## Trackable | |
field :sign_in_count, :type => Integer, :default => 0 | |
field :current_sign_in_at, :type => Time | |
field :last_sign_in_at, :type => Time | |
field :current_sign_in_ip, :type => String | |
field :last_sign_in_ip, :type => String | |
has_many :authorizations, :dependent => :destroy | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment