Skip to content

Instantly share code, notes, and snippets.

@jsheridanwells
Last active December 5, 2017 18:20
Show Gist options
  • Save jsheridanwells/d08918e5660e4ecfd2ca2dc92dbce0b7 to your computer and use it in GitHub Desktop.
Save jsheridanwells/d08918e5660e4ecfd2ca2dc92dbce0b7 to your computer and use it in GitHub Desktop.
Setting up OmniAuth for Google Login
[From this tutorial](https://medium.com/@ajayramesh/social-login-with-omniauth-and-rails-5-0-ad2bbd2a998e)
# 1. Register your app with Google, get credentials and secret
# Authorized redirect should be: http://localhost:3000/auth/google_oauth2/callback
# enable Google+ API and Contacts API
# 2. Include OmniAuth gem in gemfile:
# gem ‘omniauth-google-oauth2’
# 3. Configure OmniAuth:
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, "[Client ID]", "[Client Secret]"
end
# 4. Create the User model
# $ rails g model User provider uid email first_name last_name picture
# 5. migrate the db: $ rails db:migrate
# 6. Use hash returned from Google to look up user in the db:
# models/user.rb:
class User < ApplicationRecord
def self.find_or_create_from_auth_hash(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.picture = auth.info.image
user.save!
end
end
end
# 7. Configure the routes. routes.rb
Rails.application.routes.draw do
get 'login', to: redirect('/auth/google_oauth2'), as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'home', to: 'home#show'
get 'me', to: 'me#show', as: 'me'
root to: "home#show"
end
# 8. Create Home and Me controllers (Session controller will be created later w/o the view)
# home_controller.rb
class HomeController < ApplicationController
def show
end
end
# me_controller.rb
class MeController < ApplicationController
before_action :authenticate
def show
end
end
# 9. Create sessions controller only: $ touch app/views/controllers/sessions_controller.rb
# add the following:
class SessionsController < ApplicationController
def create
@user = User.find_or_create_from_auth_hash(auth_hash)
session[:user_id] = @user.id
redirect_to :me
end
def destroy
session[:user_id] = nil
redirect_to root_path
end
protected
def auth_hash
request.env['omniauth.auth']
end
end
# 10. Add the following methods to application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
def authenticate
redirect_to :login unless user_signed_in?
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def user_signed_in?
# converts current_user to a boolean by negating the negation
!!current_user
end
end
# 11. Create Views:
# home/show.html.erb:
<h1> Welcome, please login to continue </h1>
<a href="/login">Sign in with Google</a>
# me/show.html.erb
<% if current_user %>
<% current_user.attributes.each do |k, v| %>
<b><%= k %>:</b> <%= v %> <br>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment