Omniauth Twitter Provider

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

Usage
1. Requires https://railsbytes.com/public/templates/xkjsK3 template to already be installed
2. Ensure that TWITTER_API_KEY and TWITTER_API_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/XE5sQy"
Template Source

Review the code before running this template on your machine.

run 'bundle add omniauth-twitter'

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 :twitter, ENV['TWITTER_API_KEY'], ENV['TWITTER_API_SECRET'] \n"    
end

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

puts "IMPORTANT: Ensure that TWITTER_API_KEY and TWITTER_API_SECRET are set in your environment variables"
Comments

Sign up or Login to leave a comment.