Omniauth Facebook Provider

Installs omniauth-facebook gem, adds facebook provider, run migration for users
Icons/chart bar
Used 20 times
Created by
D Dale Zak

Usage
1. Requires https://railsbytes.com/public/templates/xkjsK3 template to already be installed
2. Ensure that FACEBOOK_APP_ID and FACEBOOK_APP_SECRET are set in your environment variables

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/zyvsjm"
Template Source

Review the code before running this template on your machine.

run 'bundle add omniauth-facebook'

run 'bundle install'

inject_into_file 'config/initializers/devise.rb', after: "# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'\n" do
  "  config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'] \n"    
end

inject_into_file 'app/controllers/omniauth_controller.rb', after: "class OmniauthController < Devise::OmniauthCallbacksController\n" do <<-EOF
  
  def facebook
    @user = User.from_omniauth request.env["omniauth.auth"]
    if @user.present?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", kind: "Facebook"
      sign_in_and_redirect root_url, event: :authentication
    else
      redirect_to root_url, alert: "Unable to sign in via Facebook"
    end
  end
  EOF
end

puts "IMPORTANT: Ensure that FACEBOOK_APP_ID and FACEBOOK_APP_SECRET are set in your environment variables"
Comments

Sign up or Login to leave a comment.