Skip to content

Commit f6f8407

Browse files
authored
Merge pull request #962 from amatsuda/pure_omniauth
Decouple OmniAuth from Devise
2 parents ffc932d + 481b531 commit f6f8407

File tree

9 files changed

+52
-41
lines changed

9 files changed

+52
-41
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def after_sign_in_path_for(user)
2626
session[:pending_invite_accept_url]
2727
elsif !user.complete?
2828
edit_profile_path
29-
elsif (referer = request.referer).present? && (URI.parse(referer).host == request.host) && (referer != new_user_session_url) && !referer.start_with?(edit_password_url(current_user)) && (referer != user_developer_omniauth_authorize_url)
29+
elsif (referer = request.referer).present? && (URI.parse(referer).host == request.host) && (referer != new_user_session_url) && !referer.start_with?(edit_password_url(current_user)) && (referer != omniauth_authorize_url(provider: :developer))
3030
referer
3131
elsif session[:target]
3232
session.delete(:target)

app/controllers/users/omniauth_callbacks_controller.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
1+
class Users::OmniauthCallbacksController < ApplicationController
22
class AuthHash < SimpleDelegator
33
def account_name
44
dig('info', 'nickname') || dig('extra', 'raw_info', 'screen_name')
@@ -9,24 +9,30 @@ def provider_name
99
end
1010
end
1111

12-
before_action :link_identity_to_current_user, only: [:twitter, :github, :developer], if: -> { current_user }
13-
skip_forgery_protection only: :developer
12+
before_action :link_identity_to_current_user, only: :callback, if: -> { current_user }
13+
skip_forgery_protection only: :callback
14+
skip_before_action :current_event
1415

15-
def twitter
16-
authenticate_with_hash
17-
end
18-
19-
def github
20-
authenticate_with_hash
16+
def callback
17+
case params[:provider]
18+
when 'twitter'
19+
authenticate_with_hash
20+
when 'github'
21+
authenticate_with_hash
22+
when 'developer'
23+
user = User.find_by(email: auth_hash.uid)
24+
authenticate_with_hash(user)
25+
else
26+
failure
27+
end
2128
end
2229

23-
def developer
24-
user = User.find_by email: auth_hash.uid
25-
authenticate_with_hash user
30+
def passthru
31+
render plain: 'Not Found', status: :not_found
2632
end
2733

2834
def failure
29-
redirect_to new_user_session_url, danger: "There was an error authenticating you. Please try again."
35+
redirect_to new_user_session_path, flash: {danger: 'There was an error authenticating you. Please try again.'}
3036
end
3137

3238
private
@@ -59,15 +65,15 @@ def authenticate_with_hash(user = nil)
5965
@user = user || User.from_omniauth(auth_hash, session[:pending_invite_email])
6066

6167
if @user.persisted?
62-
flash.now[:info] = "You have signed in with #{auth_hash.provider_name}."
68+
flash[:info] = "You have signed in with #{auth_hash.provider_name}."
6369
logger.info "Signing in user #{@user.inspect}"
6470

6571
@user.confirmed_at = Time.current
6672
sign_in @user
6773

6874
redirect_to after_sign_in_path_for(@user)
6975
else
70-
redirect_to new_user_session_url, danger: "There was an error authenticating via Auth provider: #{params[:provider]}."
76+
redirect_to new_user_session_path, flash: {danger: "There was an error authenticating via Auth provider: #{params[:provider]}."}
7177
end
7278
end
7379

app/models/user.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ class User < ApplicationRecord
22
# Include default devise modules. Others available are:
33
# :confirmable, :lockable, :timeoutable
44
devise :database_authenticatable, :registerable,
5-
:recoverable, :rememberable, :trackable, :confirmable, #:validatable,
6-
:omniauthable, omniauth_providers: [:twitter, :github, :developer]
5+
:recoverable, :rememberable, :trackable, :confirmable #:validatable,
76

87
has_many :identities, dependent: :destroy
98
has_many :invitations, dependent: :destroy

app/views/devise/shared/_links.html.erb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@
2121

2222
<hr />
2323

24-
<%- if devise_mapping.omniauthable? %>
25-
26-
<%= button_to user_twitter_omniauth_authorize_path, data: {turbo: false}, class: "btn btn-twitter", form: {style: "display: inline-block"} do %>
27-
<i class="bi bi-twitter"></i>
28-
<span class="btn-text-middle">Sign in with Twitter</span>
29-
<% end %>
30-
<%= button_to user_github_omniauth_authorize_path, data: {turbo: false}, class: "btn btn-github", form: {style: "display: inline-block"} do %>
31-
<i class="bi bi-github"></i>
32-
<span class="btn-text-middle">Sign in with GitHub</span>
33-
<% end %>
34-
24+
<%= button_to omniauth_authorize_path(provider: :twitter), data: {turbo: false}, class: "btn btn-twitter", form: {style: "display: inline-block"} do %>
25+
<i class="bi bi-twitter"></i>
26+
<span class="btn-text-middle">Sign in with Twitter</span>
27+
<% end %>
28+
<%= button_to omniauth_authorize_path(provider: :github), data: {turbo: false}, class: "btn btn-github", form: {style: "display: inline-block"} do %>
29+
<i class="bi bi-github"></i>
30+
<span class="btn-text-middle">Sign in with GitHub</span>
3531
<% end %>
3632
</div>

app/views/profiles/edit.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
= "Connected via #{name}"
7373
- else
7474
.service.mb-2
75-
= button_to send("user_#{provider}_omniauth_authorize_path"), data: {turbo: false}, class: "btn btn-#{provider}", form: {style: 'display: inline-block'} do
75+
= button_to omniauth_authorize_path(provider: provider), data: {turbo: false}, class: "btn btn-#{provider}", form: {style: 'display: inline-block'} do
7676
%i{class: "bi bi-#{provider}"}
7777
= "Connect #{name}"
7878

config/initializers/devise.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,6 @@
241241
# ==> OmniAuth
242242
# Add a new OmniAuth provider. Check the wiki for more information on setting
243243
# up on your models and hooks.
244-
config.omniauth :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: 'user:email'
245-
config.omniauth :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
246-
config.omniauth :developer unless Rails.env.production?
247244

248245
# ==> Warden configuration
249246
# If you want to use other strategies, that are not supported by Devise, or

config/initializers/omniauth.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Rails.application.config.middleware.use OmniAuth::Builder do
2+
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope: 'user:email'
3+
provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
4+
provider :developer unless Rails.env.production?
5+
end
6+
7+
OmniAuth.config.path_prefix = '/users/auth'
8+
OmniAuth.config.allowed_request_methods = [:post]

config/routes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Rails.application.routes.draw do
2+
# OmniAuth
3+
get '/users/auth/:provider/callback', to: 'users/omniauth_callbacks#callback', as: :omniauth_callback
4+
post '/users/auth/:provider', to: 'users/omniauth_callbacks#passthru', as: :omniauth_authorize
5+
get '/users/auth/failure', to: 'users/omniauth_callbacks#failure', as: :omniauth_failure
6+
27
root 'home#show'
3-
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
8+
devise_for :users
49
mount ActionCable.server => '/cable'
510

611
resource :profile, only: [:show, :edit, :update] do

spec/controllers/users/omniauth_callbacks_controller_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121

2222
it 'allows twitter login for new user' do
23-
get :twitter
23+
post :callback, params: {provider: 'twitter'}
2424
expect(response).to redirect_to(edit_profile_url)
2525
expect(User.last.identities.find_by(provider: 'twitter')).to be_present
2626
end
@@ -37,7 +37,7 @@
3737
let!(:identity) { user.identities.create!(provider: 'github', uid: github_auth_hash.uid) }
3838

3939
it 'finds user via identity and signs in' do
40-
expect { get :github }.not_to change { User.count }
40+
expect { post :callback, params: {provider: 'github'} }.not_to change { User.count }
4141
expect(controller.current_user).to eq(user)
4242
end
4343
end
@@ -46,7 +46,7 @@
4646
let!(:user) { create(:user, provider: 'github', uid: github_auth_hash.uid) }
4747

4848
it 'finds user via legacy lookup and signs in' do
49-
expect { get :github }.not_to change { User.count }
49+
expect { post :callback, params: {provider: 'github'} }.not_to change { User.count }
5050
expect(controller.current_user).to eq(user)
5151
end
5252
end
@@ -63,7 +63,7 @@
6363
user = create(:user)
6464
sign_in(user)
6565

66-
expect { get :github }.to change { user.identities.count }.by(1)
66+
expect { post :callback, params: {provider: 'github'} }.to change { user.identities.count }.by(1)
6767
expect(response).to redirect_to(edit_profile_path)
6868
expect(flash[:info]).to eq('Successfully connected GitHub to your account.')
6969
end
@@ -75,7 +75,7 @@
7575
user.identities.create!(provider: 'github', uid: github_auth_hash.uid)
7676
sign_in(user)
7777

78-
expect { get :github }.not_to change { Identity.count }
78+
expect { post :callback, params: {provider: 'github'} }.not_to change { Identity.count }
7979
expect(response).to redirect_to(edit_profile_path)
8080
expect(flash[:info]).to eq('GitHub is already connected to your account.')
8181
end
@@ -89,7 +89,7 @@
8989
user = create(:user)
9090
sign_in(user)
9191

92-
expect { get :github }.not_to change { Identity.count }
92+
expect { post :callback, params: {provider: 'github'} }.not_to change { Identity.count }
9393
expect(response).to redirect_to(edit_profile_path)
9494
expect(flash[:danger]).to eq('This GitHub account is already connected to another user.')
9595
end
@@ -101,7 +101,7 @@
101101
user = create(:user)
102102
sign_in(user)
103103

104-
get :github
104+
post :callback, params: {provider: 'github'}
105105

106106
expect(response).to redirect_to(merge_profile_path)
107107
expect(session[:pending_merge_user_id]).to eq(legacy_user.id)

0 commit comments

Comments
 (0)