Skip to content

Commit f882e5e

Browse files
authored
Merge pull request #127 from Zero-Config-Rails/fix-name-mismatch-google-oauth
Fix name mismatch between omniauth strategy and callback
2 parents 531f61e + b2e2301 commit f882e5e

12 files changed

Lines changed: 30 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## master (unreleased)
44

5-
* Update Tailwind generator to use tailwindcss-rails gem instead of manually installing everything via npm
5+
* Update Tailwind generator to use tailwindcss-rails gem instead of manually installing everything via npm ([@coolprobn][])
6+
* Fixed callback method name for Google OAuth2 Omniauth ([@coezbek][] & [@coolprobn][])
67

78
## 0.15.0 (Oct 22nd, 2024)
89

@@ -120,3 +121,4 @@
120121
[@aadil]: https://github.com/AdilRT
121122
[@mausamp]: https://github.com/mausamp
122123
[@TheZero0-ctrl]: https://github.com/TheZero0-ctrl
124+
[@coezbek]: https://github.com/coezbek

lib/generators/boring/oauth/base_generator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
module Boring
66
module Oauth
77
module BaseGenerator
8+
class MissingDeviseConfigurationError < StandardError; end
9+
10+
def add_omniauth_rails_csrf_protection_gem
11+
check_and_install_gem("omniauth-rails_csrf_protection")
12+
end
13+
814
def add_provider_and_uuid_user_details
915
say "Adding migration to add provider and uuid columns to users", :green
1016
Bundler.with_unbundled_env do

lib/generators/boring/oauth/facebook/install/install_generator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# frozen_string_literal: true
22

33
require 'bundler'
4+
require 'boring_generators/generator_helper'
45
require 'generators/boring/oauth/base_generator'
56

67
module Boring
78
module Oauth
89
module Facebook
910
class InstallGenerator < Rails::Generators::Base
11+
include BoringGenerators::GeneratorHelper
1012
include Boring::Oauth::BaseGenerator
1113

12-
class MissingDeviseConfigurationError < StandardError; end
13-
1414
desc "Adds facebook OmniAuth to the application"
1515
source_root File.expand_path("templates", __dir__)
1616

@@ -22,6 +22,7 @@ def add_facebook_omniauth_gem
2222
end
2323

2424
def invoke_common_generator_methods
25+
add_omniauth_rails_csrf_protection_gem
2526
@oauth_name = :facebook
2627
add_provider_and_uuid_user_details
2728
configure_devise_omniauth

lib/generators/boring/oauth/facebook/install/templates/omniauth_callbacks_controller.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
2-
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
3-
skip_before_action :verify_authenticity_token, only: :facebook
4-
52
def facebook
63
# You need to implement the method below in your model (e.g. app/models/user.rb)
74
@user = User.from_omniauth(request.env["omniauth.auth"])

lib/generators/boring/oauth/github/install/install_generator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# frozen_string_literal: true
22

33
require 'bundler'
4+
require 'boring_generators/generator_helper'
45
require 'generators/boring/oauth/base_generator'
56

67
module Boring
78
module Oauth
89
module Github
910
class InstallGenerator < Rails::Generators::Base
11+
include BoringGenerators::GeneratorHelper
1012
include Boring::Oauth::BaseGenerator
1113

12-
class MissingDeviseConfigurationError < StandardError; end
13-
1414
desc "Adds GitHub OmniAuth to the application"
1515
source_root File.expand_path("templates", __dir__)
1616

@@ -22,6 +22,7 @@ def add_github_omniauth_gem
2222
end
2323

2424
def invoke_common_generator_methods
25+
add_omniauth_rails_csrf_protection_gem
2526
@oauth_name = :github
2627
add_provider_and_uuid_user_details
2728
configure_devise_omniauth

lib/generators/boring/oauth/github/install/templates/omniauth_callbacks_controller.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
2-
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
3-
skip_before_action :verify_authenticity_token, only: :github
4-
52
def github
63
# You need to implement the method below in your model (e.g. app/models/user.rb)
74
@user = User.from_omniauth(request.env["omniauth.auth"])

lib/generators/boring/oauth/google/install/install_generator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# frozen_string_literal: true
22

33
require 'bundler'
4+
require 'boring_generators/generator_helper'
45
require 'generators/boring/oauth/base_generator'
56

67
module Boring
78
module Oauth
89
module Google
910
class InstallGenerator < Rails::Generators::Base
11+
include BoringGenerators::GeneratorHelper
1012
include Boring::Oauth::BaseGenerator
1113

12-
class MissingDeviseConfigurationError < StandardError; end
13-
1414
desc "Adds Google OmniAuth to the application"
1515
source_root File.expand_path("templates", __dir__)
1616

@@ -22,6 +22,7 @@ def add_google_omniauth_gem
2222
end
2323

2424
def invoke_common_generator_methods
25+
add_omniauth_rails_csrf_protection_gem
2526
@oauth_name = :google_oauth2
2627
add_provider_and_uuid_user_details
2728
configure_devise_omniauth

lib/generators/boring/oauth/google/install/templates/README

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ Some setup you must do manually if you haven't yet:
1515
after registering the Rails application on: https://code.google.com/apis/console/
1616
For example:
1717

18-
config.omniauth :google_auth, "APP_ID", "APP_SECRET"
18+
config.omniauth :google_oauth2, "APP_ID", "APP_SECRET"
1919

2020
3. Your omniauth callback URL will be: `<host>/users/auth/google_oauth2/callback`. Please update
2121
callback URL in Google after registering your omniauth application.
22+
e.g. https://boringgenerators.com/users/auth/google_oauth2/callback
2223

2324
===============================================================================
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
2-
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
3-
skip_before_action :verify_authenticity_token, only: :google_auth
4-
5-
def google_auth
2+
def google_oauth2
63
# You need to implement the method below in your model (e.g. app/models/user.rb)
7-
@user = User.from_omniauth(request.env["omniauth.auth"])
4+
@user = User.from_omniauth(request.env['omniauth.auth'])
85

96
if @user.persisted?
10-
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
11-
set_flash_message(:notice, :success, kind: "Google") if is_navigational_format?
7+
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
8+
sign_in_and_redirect @user, event: :authentication
129
else
13-
session["devise.google_data"] = request.env["omniauth.auth"].except(:extra) # Removing extra as it can overflow some session stores
14-
redirect_to new_user_registration_url
10+
session['devise.google_data'] = request.env['omniauth.auth'].except('extra') # Removing extra as it can overflow some session stores
11+
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
1512
end
1613
end
17-
18-
def failure
19-
redirect_to root_path
20-
end
2114
end

lib/generators/boring/oauth/twitter/install/install_generator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# frozen_string_literal: true
22

33
require 'bundler'
4+
require 'boring_generators/generator_helper'
45
require 'generators/boring/oauth/base_generator'
56

67
module Boring
78
module Oauth
89
module Twitter
910
class InstallGenerator < Rails::Generators::Base
11+
include BoringGenerators::GeneratorHelper
1012
include Boring::Oauth::BaseGenerator
1113

12-
class MissingDeviseConfigurationError < StandardError; end
13-
1414
desc "Adds Twitter OmniAuth to the application"
1515
source_root File.expand_path("templates", __dir__)
1616

@@ -22,6 +22,7 @@ def add_twitter_omniauth_gem
2222
end
2323

2424
def invoke_common_generator_methods
25+
add_omniauth_rails_csrf_protection_gem
2526
@oauth_name = :twitter
2627
add_provider_and_uuid_user_details
2728
configure_devise_omniauth

0 commit comments

Comments
 (0)