Omniauth Github Provider

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

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

Review the code before running this template on your machine.

run 'bundle add omniauth-github'

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 :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET'], scope: 'read:user,user:email' \n"    
end

inject_into_file 'app/controllers/omniauth_controller.rb', after: "class OmniauthController < Devise::OmniauthCallbacksController\n" do <<-EOF
  def github
    auth = request.env["omniauth.auth"]
    @user = User.from_omniauth(auth)
    if @user.present?
      #@user.slug = auth.info.nickname
      #@user.name = auth.info.name
      #@user.image_remote_url = auth.info.image
      #@user.title = auth.extra.raw_info.bio
      #@user.location = auth.extra.raw_info.location
      #@user.homepage_uri = auth.extra.raw_info.blog if auth.extra.raw_info.blog.present?
      #@user.github_uri = "https://github.com/\#{auth.info.nickname}"
      #@user.save if @user.changed?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", kind: "Github"
      sign_in(resource_name, @user)
      redirect_to root_url, event: :authentication
    else
      redirect_to root_url, alert: "Unable to sign in via Github"
    end
  end
  EOF
end

puts "IMPORTANT: Ensure that GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET are set in your environment variables"
Comments

Sign up or Login to leave a comment.