Skip to content

Added Omniauth Google Oauth2 #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ gem 'jbuilder'
gem 'devise', '~> 4.7'
gem 'omniauth-github'
gem 'omniauth-twitter'
gem 'omniauth-google-oauth2'

gem 'chartkick'
gem 'groupdate'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ GEM
omniauth-github (1.4.0)
omniauth (~> 1.5)
omniauth-oauth2 (>= 1.4.0, < 2.0)
omniauth-google-oauth2 (0.8.0)
jwt (>= 2.0)
omniauth (>= 1.1.1)
omniauth-oauth2 (>= 1.6)
omniauth-oauth (1.1.0)
oauth
omniauth (~> 1.0)
Expand Down Expand Up @@ -460,6 +464,7 @@ DEPENDENCIES
launchy
nokogiri
omniauth-github
omniauth-google-oauth2
omniauth-twitter
paper_trail
pg
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The CFP App does not provide a public facing website for your conference, though
* Ruby 2.6.5
* Bundler (was installed with 1.17.3)
* PostgreSQL
* Yarn installed via NPM

Make sure you have Ruby and Postgres installed in your environment. Double check in the [Gemfile](../blob/master/Gemfile) for the exact supported version. This is a Rails 5 app and uses bundler to install all required gems. We are also making the assumption that you're familiar with how Rails apps are setup and deployed. If this is not the case then you'll want to refer to documentation that will bridge any gaps in the instructions below.

Expand Down Expand Up @@ -56,15 +57,22 @@ This will boot up using Foreman and allow the .env file to be read / set for use

### Environment variables

[Omniauth](https://github.com/omniauth/omniauth) is set up to use Twitter and GitHub for logins in production. You'll want to put your own key and secret in for both. Other environment variables will include your postgres user and Rails' secret\_token.
The app uses the following environment variables by default. Some of these may be populated by your service provider in the case of Heroku.

TIMEZONE (defaults to Pacific if not set)
POSTGRES_USER (dev/test only)
MAIL_HOST (production only - from host)
MAIL_FROM (production only - from address)
SECRET_TOKEN (production only)

### Oauth Configuration

[Omniauth](https://github.com/omniauth/omniauth) is set up to use GitHub, Google, or Twitter for login in production. You'll want to read up how to configure Omniauth elsewhere but here are the following key pairs the app will be looking for in your environment variables.

GITHUB_KEY
GITHUB_SECRET
GOOGLE_OAUTH_CLIENT_ID
GOOGLE_OAUTH_CLIENT_SECRET
TWITTER_KEY
TWITTER_SECRET

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def github
authenticate_with_hash
end

def google_oauth2
authenticate_with_hash
end

def failure
redirect_to new_user_session_url, danger: "There was an error authenticating you. Please try again."
end
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,16 @@ def staff_nav?
def admin_nav?
current_user.admin?
end

def twitter_oauth?
ENV['TWITTER_KEY'].present?
end

def github_oauth?
ENV['GITHUB_KEY'].present?
end

def google_oauth?
ENV['GOOGLE_OAUTH_CLIENT_ID'].present?
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :confirmable, #:validatable,
:omniauthable, omniauth_providers: [:twitter, :github]
:omniauthable, omniauth_providers: [:twitter, :github, :google_oauth2]

has_many :invitations, dependent: :destroy
has_many :teammates, dependent: :destroy
Expand Down
23 changes: 16 additions & 7 deletions app/views/devise/shared/_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@

<%- if devise_mapping.omniauthable? %>

<%= link_to user_twitter_omniauth_authorize_path, class: "btn btn-twitter" do %>
<i class="icon-twitter"></i>
<span class="btn-text-middle">Sign in with Twitter</span>
<% if twitter_oauth? %>
<%= link_to user_twitter_omniauth_authorize_path, class: "btn btn-twitter" do %>
<i class="icon-twitter"></i>
<span class="btn-text-middle">Sign in with Twitter</span>
<% end %>
<% end %>
<%= link_to user_github_omniauth_authorize_path, class: "btn btn-github" do %>
<i class="icon-github"></i>
<span class="btn-text-middle">Sign in with GitHub</span>
<% if github_oauth? %>
<%= link_to user_github_omniauth_authorize_path, class: "btn btn-github" do %>
<i class="icon-github"></i>
<span class="btn-text-middle">Sign in with GitHub</span>
<% end %>
<% end %>
<% if google_oauth? %>
<%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-default" do %>
<i class="icon-google"></i>
<span class="btn-text-middle">Sign in with Google</span>
<% end %>
<% end %>

<% end %>
</div>
1 change: 1 addition & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
# up on your models and hooks.
config.omniauth :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: 'user:email'
config.omniauth :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
config.omniauth :google_oauth2, ENV['GOOGLE_OAUTH_CLIENT_ID'], ENV['GOOGLE_OAUTH_CLIENT_SECRET']
config.omniauth :developer unless Rails.env.production?

# ==> Warden configuration
Expand Down