Skip to content

Instantly share code, notes, and snippets.

@vquaiato
Forked from schleg/01. Gemfile
Created September 13, 2012 04:08
Show Gist options
  • Save vquaiato/3711796 to your computer and use it in GitHub Desktop.
Save vquaiato/3711796 to your computer and use it in GitHub Desktop.
Setup for Devise + Omniauth (mongoid and facebook-only)
#add this to your gemfile
gem 'mongoid'
gem 'devise'
gem "omniauth-facebook"
rails g devise:install
#after installing devise look at the configs it needs (will be shown in the console output)
rails g devise user
rails g model authorization provider:string uid:string user_id:integer token:string secret:string name:string link:string
# app/models/authorization.rb
class Authorization
include Mongoid::Document
field :provider, type: String
field :uid, type: String
field :user_id, type: Integer
field :token, type: String
field :secret, type: String
field :name, type: String
field :link, type: String
#simply add this, the above code will be generated for you XD
belongs_to :user
end
# app/config/initializers/devise.rb
Devise.setup do |config|
...
config.omniauth :facebook, "KEY", "SECRET"
...
end
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
devise_for :users, :path => "accounts", :controllers => { :omniauth_callbacks => "omniauth_callbacks"}
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