Skip to content

Commit

Permalink
Add Google as an OAuth provider / strategy to the project.
Browse files Browse the repository at this point in the history
Users can now sign in with Google as well.
  • Loading branch information
StevenXL committed Dec 14, 2015
1 parent d151b39 commit 5c0824a
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem 'active_hash'
gem 'sanitize'
gem 'gmaps4rails'
gem 'geocoder'
gem 'omniauth-google-oauth2'
gem 'omniauth-meetup'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
Expand Down
9 changes: 8 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,19 @@ GEM
omniauth-github (1.1.2)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.1)
omniauth-google-oauth2 (0.2.10)
addressable (~> 2.3)
jwt (~> 1.0)
multi_json (~> 1.3)
omniauth (>= 1.1.1)
omniauth-oauth2 (~> 1.3.1)
omniauth-meetup (0.0.7)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.0)
omniauth-oauth (1.1.0)
oauth
omniauth (~> 1.0)
omniauth-oauth2 (1.4.0)
omniauth-oauth2 (1.3.1)
oauth2 (~> 1.0)
omniauth (~> 1.2)
omniauth-twitter (1.2.1)
Expand Down Expand Up @@ -371,6 +377,7 @@ DEPENDENCIES
newrelic_rpm
omniauth-facebook
omniauth-github
omniauth-google-oauth2
omniauth-meetup
omniauth-twitter
pg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def all
redirect_to new_user_registration_path
end
end
[:facebook, :twitter, :github, :meetup].each do |provider|
[:facebook, :twitter, :github, :meetup, :google_oauth2].each do |provider|
alias_method provider, :all
end
end
end
17 changes: 16 additions & 1 deletion app/services/omniauth_providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def self.provider_data
key: :meetup,
name: 'Meetup',
icon: 'fa-calendar'
}
},
{
key: :google_oauth2,
name: 'Google',
icon: 'fa-google'
},
]
end

Expand Down Expand Up @@ -48,6 +53,8 @@ def self.user_attributes_from_omniauth(omniauth)
self.github_omniauth_attributes(omniauth)
when 'meetup'
self.meetup_omniauth_attributes(omniauth)
when 'google_oauth2'
self.google_oauth2_omniauth_attributes(omniauth)
else
raise 'Unknown Provider'
end
Expand Down Expand Up @@ -86,4 +93,12 @@ def self.github_omniauth_attributes(omniauth)
def self.meetup_omniauth_attributes(omniauth)
self.split_name(omniauth['info']['name'])
end

def self.google_oauth2_omniauth_attributes(omniauth)
{
email: omniauth['info']['email'],
first_name: omniauth['info']['first_name'],
last_name: omniauth['info']['last_name']
}
end
end
1 change: 1 addition & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
config.omniauth :twitter, ENV['TWITTER_OAUTH_KEY'], ENV['TWITTER_OAUTH_SECRET']
config.omniauth :github, ENV['GITHUB_OAUTH_KEY'], ENV['GITHUB_OAUTH_SECRET']
config.omniauth :meetup, ENV['MEETUP_OAUTH_KEY'], ENV['MEETUP_OAUTH_SECRET']
config.omniauth :google_oauth2, ENV['GOOGLE_OAUTH_KEY'], ENV['GOOGLE_OAUTH_SECRET']

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
39 changes: 38 additions & 1 deletion spec/features/omniauth_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@
end
end

context "with a valid google_oauth2 auth" do
let(:google_oauth2_response) { OmniauthResponses.google_oauth2_response }

before do
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new(google_oauth2_response)
end

it 'creates a user and authentication if the user does not exist' do
visit user_omniauth_authorize_path(:google_oauth2)

within '#sign-up' do
click_on 'Sign up'
end

user = User.last
expect(user).to be_valid
expect(user.first_name).to eq(google_oauth2_response[:info][:first_name])
expect(user.last_name).to eq(google_oauth2_response[:info][:last_name])
expect(user.email).to eq(google_oauth2_response[:info][:email])

authentication = user.authentications.first
expect(authentication.provider).to eq('google_oauth2')
expect(authentication.uid).to eq(google_oauth2_response[:uid])
end

it 'creates a new authentication if the user already exists' do
user = create(:user)
sign_in_as user

visit user_omniauth_authorize_path(:google_oauth2)

authentication = user.authentications.first
expect(authentication.provider).to eq('google_oauth2')
expect(authentication.uid).to eq(google_oauth2_response[:uid])
end
end

context "with a valid twitter auth" do
let(:twitter_response) { OmniauthResponses.twitter_response }

Expand Down Expand Up @@ -193,4 +230,4 @@

expect(page).to have_content('Admin Dashboard')
end
end
end
50 changes: 49 additions & 1 deletion spec/services/omniauth_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,52 @@ def self.github_response
}
}
end
end

def self.google_oauth2_response
{
:provider => 'google_oauth2',
:uid => '123456789101112130122',
:info => {
email: 'joe@bloggs.com',
first_name: 'Joe',
image: "https://robohash.org/sitsequiquia.png?size=512x512",
last_name: "Bloggs",
name: "Joe Bloggs",
urls: { Google: "https://plus.google.com/494850544950524948535348525457565050575557" }
},
:credentials => {
expires: true,
expires_at: 1450062184,
refresh_token: "XYZW...",
token: "ABCDEF..."
},
:extra => {
id_info: {
at_hash: 'abcdefg',
aud: "50906091245.apps.googleusercontent.com",
azp: "50906091245.apps.googleusercontent.com",
email: "joe@bloggs.com",
email_verified: true,
exp: 1450062184,
iat: 1450058584,
iss: "accounts.google.com",
sub: "102612410550469822979"
},
id_token: "abcdefghijklmnopqrstuvwxyz",
raw_info: {
email: "joe@bloggs.com",
email_verified: "true",
family_name: "Bloggs",
gender: "male",
given_name: "Joe",
kind: "plus#personOpenIdConnect",
locale: "en",
name: "Joe Bloggs",
picture: "https://robohash.org/sitsequiquia.png?size=512x512",
profile: "https://plus.google.com/494850544950524948535348525457565050575557",
sub: "494850544950524948535348525457565050575557"
},
}
}
end
end

0 comments on commit 5c0824a

Please sign in to comment.