Skip to content

Instantly share code, notes, and snippets.

View drKreso's full-sized avatar

Kresimir Bojcic drKreso

View GitHub Profile
# app/models/user.rb
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
def self.from_omniauth(auth)
# app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
# execute from terminal
rails g controller users/omniauth_callbacks
# config/routes.rb
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
<%-# app/views/cities/index.html.erb -%>
<% if user_signed_in? %>
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
<% else %>
<%= link_to('Login', new_user_session_path) %>
<% end %>
#execute from terminal
bundle install
# app/models/user.rb
devise :omniauthable, omniauth_providers: [:facebook]
# Gemfile.rb
gem 'omniauth-oauth2', '1.3.1'
gem 'omniauth-facebook'
# config/routes.rb
root 'cities#index'
# app/controllers/application_controller.rb
before_action :authenticate_user!