Omniauth Google Provider
Installs omniauth-google-oauth2 gem, adds google_oauth2 provider, run migration for users
Used 37 times
D
Dale Zak
Usage
1. Requires https://railsbytes.com/public/templates/xkjsK3 template to already be installed
2. Ensure that GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are set in your environment variables
2. Ensure that GOOGLE_CLIENT_ID and GOOGLE_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/V4YsP6"
Template Source
Review the code before running this template on your machine.
run 'bundle add omniauth-google-oauth2'
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 :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'] \n"
end
inject_into_file 'app/controllers/omniauth_controller.rb', after: "class OmniauthController < Devise::OmniauthCallbacksController\n" do <<-EOF
def google_oauth2
@user = User.from_omniauth request.env["omniauth.auth"]
if @user.present?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", kind: "Google"
sign_in_and_redirect root_url, event: :authentication
else
session["devise.google_data"] = request.env["omniauth.auth"].except(:extra)
redirect_to root_url, alert: "Unable to sign in via Google"
end
end
EOF
end
puts "IMPORTANT: Ensure that GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are set in your environment variables"