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 | |
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) |
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/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 |
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
# execute from terminal | |
rails g controller 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
# 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/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 %> |
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
#execute from terminal | |
bundle install |
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, omniauth_providers: [: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
# Gemfile.rb | |
gem 'omniauth-oauth2', '1.3.1' | |
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
# config/routes.rb | |
root 'cities#index' |
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/application_controller.rb | |
before_action :authenticate_user! |