omniauth-generator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/README.md +75 -0
- data/Rakefile +4 -0
- data/lib/omniauth/generator/install_generator.rb +69 -0
- data/lib/omniauth/generator/templates/_devise.rb +37 -0
- data/lib/omniauth/generator/templates/_social_buttons.html.erb +156 -0
- data/lib/omniauth/generator/templates/_user.rb +26 -0
- data/lib/omniauth/generator/templates/omniauth_callbacks_controller.rb +40 -0
- data/lib/omniauth/generator/version.rb +7 -0
- data/lib/omniauth/generator.rb +4 -0
- data/sig/omniauth/generator.rbs +6 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fedc3d26b48573ab55051ab223a168898f9275106024defd0b848ce0c0e87a62
|
4
|
+
data.tar.gz: 21b52f3c5a812a77e3ca02ededa20288d4b4e6ff37f78413fb9d7e27c6d98ade
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70b1937a1444eaac817e631c9e25bd4c879eed85e2e93fc923cfa06b52396309541a3ac1c9bff96aca659a14e5eba20731f60fed40a629c71f57ea3aded17e16
|
7
|
+
data.tar.gz: 412c340cc7d8a4c8d3c6ed048c73e7459c22a24f40c0bc7636cfb51fcce5d24ff69982da7ef81f19288c5f433859f6708682ef1b111d231d2cebc3ee8c4422b7
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# omniauth-generator
|
2
|
+
|
3
|
+
Rails Generator to install Omniauth for Devise via `rails g devise:omniauth:install`
|
4
|
+
|
5
|
+
## Motivation
|
6
|
+
|
7
|
+
I needed way more time to get Devise to work with common Omniauth providers than I expected, primarily because there were no pretty buttons anywhere to use which match the 2024 design guidelines of the most important social logins. Next, there were a lot of important little caveats not considered in various tutorials or [other generators](https://github.com/abhaynikam/boring_generators/tree/main/lib/generators/boring/oauth).
|
8
|
+
|
9
|
+
This project contains helpers for the following social login buttons:
|
10
|
+
|
11
|
+
- [Google](https://developers.google.com/identity/branding-guidelines) - [omniauth-google-oauth2](https://github.com/zquestz/omniauth-google-oauth2)
|
12
|
+
- [Microsoft](https://learn.microsoft.com/en-us/entra/identity-platform/howto-add-branding-in-apps)
|
13
|
+
- [Apple](https://developer.apple.com/design/human-interface-guidelines/sign-in-with-apple)
|
14
|
+
- Facebook
|
15
|
+
- LinkedIn
|
16
|
+
- [GitHub](https://github.com/logos) (they don't have a guideline for how to style their login button)
|
17
|
+
|
18
|
+
The buttons have basic styling and should look pretty when `<buttons>` are already styled. This is how they look out of the box with [PicoCSS](https://picocss.com/):
|
19
|
+
|
20
|
+
<img src="https://github.com/user-attachments/assets/9df431de-74cb-44c6-bc08-31437dffbaef" width="456" />
|
21
|
+
|
22
|
+
The following aspects of the branding guidelines were considered:
|
23
|
+
|
24
|
+
- Google wants Roboto font (you have to serve it yourself)
|
25
|
+
- Microsoft wants Segeo UI (you have to serve it yourself)
|
26
|
+
- Facebook prefers that you write "Continue with Facebook" or "Log in with Facebook". All others don't care or seem fine with "Sign Up" and "Sign In"
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
This Gem expects that you already have:
|
31
|
+
|
32
|
+
- a working Devise setup (`rails devise:install`) without Omniauth
|
33
|
+
- a Devise resource called User
|
34
|
+
- routes set up via `devise_for :users`
|
35
|
+
- at least one oauth provider configured (API client id/secret) and callback URLs whitelisted.
|
36
|
+
|
37
|
+
```
|
38
|
+
# Install Gem into development (or globally, it is not need in production)
|
39
|
+
bundle add omniauth-generator --group development
|
40
|
+
|
41
|
+
# Install at least one omniauth gem/strategy, e.g. of the following
|
42
|
+
bundle add omniauth-google-oauth2
|
43
|
+
bundle add omniauth-facebook
|
44
|
+
bundle add omniauth-linkedin
|
45
|
+
bundle add omniauth-github
|
46
|
+
bundle add omniauth-azure-activedirectory-v2
|
47
|
+
bundle add omniauth-apple
|
48
|
+
|
49
|
+
# Run Generator (will bundle install for you)
|
50
|
+
rails devise:omniauth:install
|
51
|
+
|
52
|
+
# For any Gem which you want to use, you must set matching credentials in `rails credentials:edit` (or see below for ENV), e.g.:
|
53
|
+
google:
|
54
|
+
client_id: YOUR_GOOGLE_CLIENT_ID
|
55
|
+
client_secret: YOUR_GOOGLE_CLIENT_SECRET
|
56
|
+
# Add entries for facebook, linkedin, github, microsoft, apple (all lowercase)
|
57
|
+
|
58
|
+
# After the generator is done, review all changes
|
59
|
+
rails db:migrate
|
60
|
+
|
61
|
+
rails server
|
62
|
+
```
|
63
|
+
|
64
|
+
## Features
|
65
|
+
|
66
|
+
- The name and paths of all strategies are sane (e.g. using 'microsoft' rather than 'azure_auth' or 'google' rather than 'google_oauth2'). Sane names are used for flash as well.
|
67
|
+
- Proper error messages are returned and set in the flash.
|
68
|
+
- All social logos were optimized/minified using https://jakearchibald.github.io/svgomg/
|
69
|
+
- All styling and CSS is inlined in the `Helper` so this Gem should work with any js and css bundler.
|
70
|
+
- Put all the social buttons into a separate renderer and separate them from `app/views/devise/shared/_links.html.erb`
|
71
|
+
|
72
|
+
## Limitations
|
73
|
+
|
74
|
+
- The generator is able to support multiple `omniauth-xxx` gems at the same time, but users can not log into the same account from multiple providers when those providers provide the same email back. Compare this answer on SO: https://stackoverflow.com/a/22126562/278842
|
75
|
+
- The generator is using secrets from the Rails credentials file. If you prefer to use ENV you need to adjust the code in `config/initializers/devise.rb` accordingly.
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Omniauth
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
|
7
|
+
desc "Adds OmniAuth to an application using Devise"
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
9
|
+
|
10
|
+
def dependencies
|
11
|
+
|
12
|
+
omniauth_strategies = {
|
13
|
+
google: :GoogleOauth2,
|
14
|
+
facebook: :Facebook,
|
15
|
+
linkedin: :LinkedIn,
|
16
|
+
github: :GitHub,
|
17
|
+
microsoft: :AzureActivedirectoryV2,
|
18
|
+
apple: :Apple
|
19
|
+
}
|
20
|
+
if !omniauth_strategies.any? { |_, strategy_class| Gem.loaded_specs.has_key?("omniauth-#{strategy_class.to_s.underscore.gsub('_', '-')}") }
|
21
|
+
say_error("Omniauth-generator: Need at least one OmniAuth strategy gem to be installed. Obvious candidates not found.")
|
22
|
+
ask("Would you like to continue? (y/n)") do |answer|
|
23
|
+
Kernel.exit(1) unless answer.downcase == 'y'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
gem "omniauth-rails_csrf_protection"
|
28
|
+
end
|
29
|
+
|
30
|
+
def database_update
|
31
|
+
generate :migration, "AddOmniauthToUsers provider:string uid:string"
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_application
|
35
|
+
inject_into_class 'app/models/user.rb', 'User', File.read(find_in_source_paths('_user.rb')).indent(2)
|
36
|
+
|
37
|
+
# Configure Omniauth strategies in `config/initializers/devise.rb`
|
38
|
+
inject_into_file 'config/initializers/devise.rb', File.read(find_in_source_paths('_devise.rb')).indent(2), before: /^end/
|
39
|
+
|
40
|
+
# Update Devise routes in `config/routes.rb`
|
41
|
+
gsub_file 'config/routes.rb', 'devise_for :users', <<~RUBY.strip.indent(2)
|
42
|
+
devise_for :users,
|
43
|
+
controllers: {
|
44
|
+
omniauth_callbacks: "users/omniauth_callbacks"
|
45
|
+
}
|
46
|
+
RUBY
|
47
|
+
|
48
|
+
# Create `Users::OmniauthCallbacksController` to handle callbacks
|
49
|
+
copy_file 'omniauth_callbacks_controller.rb', 'app/controllers/users/omniauth_callbacks_controller.rb'
|
50
|
+
end
|
51
|
+
|
52
|
+
def update_views
|
53
|
+
gsub_file 'app/views/devise/shared/_links.html.erb', /<%- if devise_mapping\.omniauthable\?.*?\n<% end %>/m, ''
|
54
|
+
|
55
|
+
Dir.glob("app/views/devise/{registrations,sessions}/new.html.erb").each { |f|
|
56
|
+
inject_into_file f, '<%= render "devise/shared/social_buttons" %>' + "\n\n", before: '<%= render "devise/shared/links" %>'
|
57
|
+
}
|
58
|
+
|
59
|
+
copy_file '_social_buttons.html.erb', 'app/views/devise/shared/_social_buttons.html.erb'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Add all the Omniauth strategies which are present in the credentials file
|
2
|
+
omniauth_strategies = {
|
3
|
+
google: :GoogleOauth2,
|
4
|
+
facebook: :Facebook,
|
5
|
+
linkedin: :LinkedIn,
|
6
|
+
github: :GitHub,
|
7
|
+
microsoft: :AzureActivedirectoryV2,
|
8
|
+
apple: :Apple
|
9
|
+
}
|
10
|
+
|
11
|
+
omniauth_strategies.each do |strategy, strategy_class|
|
12
|
+
if Rails.application.credentials[strategy].present?
|
13
|
+
begin
|
14
|
+
clazz_name = "OmniAuth::Strategies::#{strategy_class}"
|
15
|
+
clazz = Kernel.const_get(clazz_name)
|
16
|
+
rescue NameError
|
17
|
+
require 'active_support/inflector'
|
18
|
+
puts "Gem for OmniAuth strategy '#{strategy}' not found. Please add to your Gemfile. Likely named 'omniauth-#{strategy_class.to_s.underscore.gsub('_', '-')}' or ...-oauth2."
|
19
|
+
Kernel.exit(1)
|
20
|
+
end
|
21
|
+
|
22
|
+
config.omniauth strategy,
|
23
|
+
client_id: Rails.application.credentials[strategy][:client_id],
|
24
|
+
client_secret: Rails.application.credentials[strategy][:client_secret],
|
25
|
+
strategy_class: clazz,
|
26
|
+
name: strategy # Even though this is the same as the provider key above, otherwise routes don't match
|
27
|
+
else
|
28
|
+
begin
|
29
|
+
clazz_name = "OmniAuth::Strategies::#{strategy_class}"
|
30
|
+
clazz = Kernel.const_get(clazz_name)
|
31
|
+
puts "Can't start without #{strategy} API credentials. Please run 'rails credentials:edit --environment xxx'."
|
32
|
+
# Kernel.exit(1)
|
33
|
+
rescue NameError
|
34
|
+
# Do nothing - Credentials not found but Gem also not there
|
35
|
+
end if Rails.env.development?
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
<%- resource_class.omniauth_providers.each do |provider| -%>
|
2
|
+
<%- case provider -%>
|
3
|
+
<%- when :google -%>
|
4
|
+
<%= button_to omniauth_authorize_path(resource_name, provider), data: { turbo: false }, method: :post, style: "
|
5
|
+
display: flex;
|
6
|
+
align-items: center;
|
7
|
+
justify-content: center;
|
8
|
+
background-color: white;
|
9
|
+
border: 1px solid #747775;
|
10
|
+
border-radius: 4px;
|
11
|
+
font-family: 'Roboto', Arial, sans-serif;
|
12
|
+
color: #1f1f1f;
|
13
|
+
font-weight: 500;
|
14
|
+
box-shadow: 0 1px 2px rgba(60, 64, 67, 0.30), 0 1px 3px rgba(60, 64, 67, 0.15);
|
15
|
+
outline: none;" do %>
|
16
|
+
<svg xmlns="http://www.w3.org/2000/svg" style="width:20px;height:20px;margin-right:12px" viewBox="0 0 48 48">
|
17
|
+
<path fill="#EA4335" d="M24 10c4 0 7 1 9 3l7-7c-4-4-10-6-16-6C15 0 7 5 3 13l8 6c1-5 7-9 13-9z"/>
|
18
|
+
<path fill="#4285F4" d="M47 25v-5H24v9h13c-1 3-2 6-5 7l8 6c4-4 7-10 7-17z"/>
|
19
|
+
<path fill="#FBBC05" d="m11 29-1-5 1-5-8-6a24 24 0 0 0 0 22l8-6z"/>
|
20
|
+
<path fill="#34A853" d="M24 48c6 0 12-2 16-6l-8-6c-2 2-5 2-8 2-6 0-12-4-13-9l-8 6c4 8 12 13 21 13z"/>
|
21
|
+
<path fill="none" d="M0 0h48v48H0z"/>
|
22
|
+
</svg>
|
23
|
+
<span>
|
24
|
+
Sign <%= controller_name == 'registrations' ? 'up' : 'in' %> with <%= OmniAuth::Utils.camelize(provider) %>
|
25
|
+
</span>
|
26
|
+
<% end %>
|
27
|
+
|
28
|
+
<%- when :microsoft -%>
|
29
|
+
<%= button_to omniauth_authorize_path(resource_name, provider), data: { turbo: false }, method: :post, style: "
|
30
|
+
display: flex;
|
31
|
+
align-items: center;
|
32
|
+
justify-content: center;
|
33
|
+
background-color: #FFFFFF;
|
34
|
+
border: 1px solid #8C8C8C;
|
35
|
+
border-radius: 4px;
|
36
|
+
font-family: 'Segoe UI', Arial, sans-serif;
|
37
|
+
color: #5E5E5E;
|
38
|
+
font-weight: 600;
|
39
|
+
box-shadow: 0 1px 2px rgba(60, 64, 67, 0.30), 0 1px 3px rgba(60, 64, 67, 0.15);
|
40
|
+
outline: none;
|
41
|
+
" do %>
|
42
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 21 21" style="width: 20px; height: 20px; margin-right: 12px;">
|
43
|
+
<rect x="1" y="1" width="9" height="9" fill="#f25022"/>
|
44
|
+
<rect x="1" y="11" width="9" height="9" fill="#00a4ef"/>
|
45
|
+
<rect x="11" y="1" width="9" height="9" fill="#7fba00"/>
|
46
|
+
<rect x="11" y="11" width="9" height="9" fill="#ffb900"/>
|
47
|
+
</svg>
|
48
|
+
<span style="margin-bottom: 0.1em;">
|
49
|
+
Sign <%= controller_name == 'registrations' ? 'up' : 'in' %> with <%= OmniAuth::Utils.camelize(provider) %>
|
50
|
+
</span>
|
51
|
+
<% end %>
|
52
|
+
|
53
|
+
<%- when :facebook -%>
|
54
|
+
<%= button_to omniauth_authorize_path(resource_name, provider), data: { turbo: false }, method: :post, style: "
|
55
|
+
display: flex;
|
56
|
+
align-items: center;
|
57
|
+
justify-content: center;
|
58
|
+
background-color: #1877F2;
|
59
|
+
border: none;
|
60
|
+
border-radius: 4px;
|
61
|
+
color: #FFFFFF;
|
62
|
+
font-weight: 600;
|
63
|
+
box-shadow: 0 1px 2px rgba(60, 64, 67, 0.30), 0 1px 3px rgba(60, 64, 67, 0.15);
|
64
|
+
outline: none;
|
65
|
+
" do %>
|
66
|
+
<svg xmlns="http://www.w3.org/2000/svg" style="width:30px;height:30px;margin-right:12px" viewBox="0 0 666.7 666.7">
|
67
|
+
<defs>
|
68
|
+
<clipPath id="a" clipPathUnits="userSpaceOnUse">
|
69
|
+
<path d="M0 700h700V0H0Z"/>
|
70
|
+
</clipPath>
|
71
|
+
</defs>
|
72
|
+
<g clip-path="url(#a)" transform="matrix(1.3333333 0 0 -1.3333333 -133 800)">
|
73
|
+
<path d="M0 0a250 250 0 1 1-310-243v167h-52V0h52v33c0 85 38 124 122 124 15 0 43-3 54-6V82l-29 1c-41 0-57-16-57-56V0h82l-14-76h-68v-172A250 250 0 0 1 0 0" style="fill:#fff;fill-opacity:1;fill-rule:nonzero;stroke:none" transform="translate(600 350)"/>
|
74
|
+
<path d="m0 0 14 76h-82v27c0 41 16 56 57 56l29-1v70c-11 3-38 6-54 6-84 0-122-40-122-125V76h-52V0h52v-166a251 251 0 0 1 90-6V0Z" style="fill:#1877f2;fill-opacity:1;fill-rule:nonzero;stroke:none" transform="translate(448 274)"/>
|
75
|
+
</g>
|
76
|
+
</svg>
|
77
|
+
<span>
|
78
|
+
<%= controller_name == 'registrations' ? 'Continue' : 'Log in' %> with <%= OmniAuth::Utils.camelize(provider) %>
|
79
|
+
</span>
|
80
|
+
<% end %>
|
81
|
+
|
82
|
+
<%- when :github -%>
|
83
|
+
<%= button_to omniauth_authorize_path(resource_name, provider), data: { turbo: false }, method: :post, style: "
|
84
|
+
display: flex;
|
85
|
+
align-items: center;
|
86
|
+
justify-content: center;
|
87
|
+
background-color: #000000;
|
88
|
+
border: none;
|
89
|
+
border-radius: 4px;
|
90
|
+
color: #FFFFFF;
|
91
|
+
font-weight: 600;
|
92
|
+
box-shadow: 0 1px 2px rgba(60, 64, 67, 0.30), 0 1px 3px rgba(60, 64, 67, 0.15);
|
93
|
+
outline: none;
|
94
|
+
" do %>
|
95
|
+
<svg xmlns="http://www.w3.org/2000/svg" style="width:30px;height:30px;margin-right:12px" viewBox="0 0 96 98">
|
96
|
+
<path fill="#fff" fill-rule="evenodd" d="M48.9 0a49.2 49.2 0 0 0-15.4 96c2.3.4 3.2-1.2 3.2-2.5v-9c-13.6 2.9-16.5-6-16.5-6-2.2-5.7-5.4-7.1-5.4-7.1-4.4-3 .3-3 .3-3 5 .3 7.5 5 7.5 5 4.4 7.5 11.5 5.4 14.3 4 .4-3 1.7-5.3 3-6.5-10.8-1.1-22.2-5.4-22.2-24.3 0-5.4 2-9.8 5-13.2-.5-1.2-2.2-6.3.5-13 0 0 4.1-1.3 13.4 5A47 47 0 0 1 49 23.8c4 0 8.3.6 12.2 1.6 9.3-6.3 13.4-5 13.4-5 2.7 6.7 1 11.8.5 13 3.1 3.4 5 7.8 5 13.2 0 19-11.4 23-22.3 24.3 1.7 1.5 3.3 4.5 3.3 9.1v13.5c0 1.3.8 2.9 3.2 2.4a49.2 49.2 0 0 0 33.4-46.7A49 49 0 0 0 49 0z" clip-rule="evenodd"/>
|
97
|
+
</svg>
|
98
|
+
<span>
|
99
|
+
Sign <%= controller_name == 'registrations' ? 'up' : 'in' %> with GitHub
|
100
|
+
</span>
|
101
|
+
<% end %>
|
102
|
+
|
103
|
+
<%- when :linkedin -%>
|
104
|
+
<%= button_to omniauth_authorize_path(resource_name, provider), data: { turbo: false }, method: :post, style: "
|
105
|
+
display: flex;
|
106
|
+
align-items: center;
|
107
|
+
justify-content: center;
|
108
|
+
background-color: #0077B5;
|
109
|
+
border: none;
|
110
|
+
border-radius: 4px;
|
111
|
+
color: #FFFFFF;
|
112
|
+
font-weight: 600;
|
113
|
+
box-shadow: 0 1px 2px rgba(60, 64, 67, 0.30), 0 1px 3px rgba(60, 64, 67, 0.15);
|
114
|
+
outline: none;
|
115
|
+
" do %>
|
116
|
+
<svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px;margin-right:12px" viewBox="0 0 72 72">
|
117
|
+
<g fill="none" fill-rule="evenodd">
|
118
|
+
<path fill="#FFF" d="M8 72h56c4 0 8-4 8-8V8c0-4-4-8-8-8H8C4 0 0 4 0 8v56c0 4 4 8 8 8Z"/>
|
119
|
+
<path fill="#007EBB" d="M62 62H51V44c0-5-2-8-6-8s-6 3-6 8v18H29V27h10v5s3-6 10-6c8 0 13 5 13 14v22ZM16 23c-3 0-6-3-6-7 0-3 3-6 6-6 4 0 7 3 7 6 0 4-3 7-7 7Zm-5 39h11V27H11v35Z"/>
|
120
|
+
</g>
|
121
|
+
</svg>
|
122
|
+
<span>
|
123
|
+
Sign <%= controller_name == 'registrations' ? 'up' : 'in' %> with <%= OmniAuth::Utils.camelize(provider) %>
|
124
|
+
</span>
|
125
|
+
<% end %>
|
126
|
+
|
127
|
+
<%- when :apple -%>
|
128
|
+
|
129
|
+
<%= button_to omniauth_authorize_path(resource_name, provider), data: { turbo: false }, method: :post, style: "
|
130
|
+
display: flex;
|
131
|
+
align-items: center;
|
132
|
+
justify-content: center;
|
133
|
+
background-color: #000000;
|
134
|
+
border: none;
|
135
|
+
border-radius: 4px;
|
136
|
+
color: #FFFFFF;
|
137
|
+
font-weight: 600;
|
138
|
+
box-shadow: 0 1px 2px rgba(60, 64, 67, 0.30), 0 1px 3px rgba(60, 64, 67, 0.15);
|
139
|
+
outline: none;" do %>
|
140
|
+
<svg xmlns="http://www.w3.org/2000/svg" style="width:30px;height:30px;margin-right:5px" viewBox="13 13 30 30">
|
141
|
+
<g fill="none" fill-rule="evenodd">
|
142
|
+
<path fill="#000" d="M6 6h44v44H6z"/>
|
143
|
+
<path fill="#FFF" fill-rule="nonzero" d="M28.2 20.4c.9 0 1.9-.6 2.5-1.4a4 4 0 0 0 1-2.6V16c-1 0-2 .6-2.8 1.4-.5.7-1 1.6-1 2.6v.4h.3Zm-3 14.6c1.2 0 1.7-.8 3.2-.8 1.4 0 1.7.8 3 .8 1.2 0 2-1.2 2.8-2.4 1-1.3 1.3-2.6 1.3-2.7 0 0-2.5-1-2.5-3.8 0-2.4 2-3.5 2-3.6a4.3 4.3 0 0 0-3.6-2c-1.4 0-2.6 1-3.3 1-.7 0-1.8-.9-3-.9-2.3 0-4.6 2-4.6 5.7 0 2.3.9 4.7 2 6.3.8 1.3 1.6 2.4 2.8 2.4Z"/>
|
144
|
+
</g>
|
145
|
+
</svg>
|
146
|
+
<span>
|
147
|
+
Sign <%= controller_name == 'registrations' ? 'up' : 'in' %> with <%= OmniAuth::Utils.camelize(provider) %>
|
148
|
+
</span>
|
149
|
+
<% end %>
|
150
|
+
|
151
|
+
<%- else -%>
|
152
|
+
|
153
|
+
<%= button_to "Sign #{controller_name == 'registrations' ? "up" : "in" } with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), data: { turbo: false } %>
|
154
|
+
|
155
|
+
<%- end -%>
|
156
|
+
<%- end -%>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
if Devise.omniauth_providers&.size > 0
|
3
|
+
devise :omniauthable, omniauth_providers: Devise.omniauth_providers
|
4
|
+
|
5
|
+
def self.from_omniauth(auth)
|
6
|
+
where(provider: auth.provider, uid: auth.uid)
|
7
|
+
.or(where(uid: nil, provider: nil, email: auth.info.email.downcase.strip))
|
8
|
+
.first_or_create do |user|
|
9
|
+
user.email = auth.info.email
|
10
|
+
user.password ||= Devise.friendly_token[0, 20]
|
11
|
+
|
12
|
+
# if auth.info.last_name.present? && auth.info.first_name.present?
|
13
|
+
# user.nickname = "#{auth.info.first_name} #{auth.info.last_name[0]}."
|
14
|
+
# elsif auth.info.name.present?
|
15
|
+
# user.nickname = auth.info.name
|
16
|
+
# end
|
17
|
+
|
18
|
+
# user.name = auth.info.name # assuming the user model has a name
|
19
|
+
# user.image = auth.info.image # assuming the user model has an image
|
20
|
+
|
21
|
+
# If you are using confirmable and the provider(s) you use validate emails,
|
22
|
+
# uncomment the line below to skip the confirmation emails.
|
23
|
+
# user.skip_confirmation!
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
2
|
+
|
3
|
+
Devise.omniauth_providers.each do |provider|
|
4
|
+
define_method(provider) do
|
5
|
+
handle_auth(provider.to_s.capitalize)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def handle_auth(kind)
|
10
|
+
@user = User.from_omniauth(request.env['omniauth.auth'])
|
11
|
+
|
12
|
+
if @user.persisted?
|
13
|
+
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: kind
|
14
|
+
sign_in_and_redirect @user, event: :authentication
|
15
|
+
else
|
16
|
+
# Can join("\n") errors if your frontend can't handle multiple errors
|
17
|
+
flash[:alert] = @user.errors.full_messages if is_navigational_format?
|
18
|
+
redirect_to new_user_registration_url
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Fix for error in OmniauthCallbacksController
|
23
|
+
# OAuth2::Error does not have `error_reason` and `error` methods
|
24
|
+
def failure_message
|
25
|
+
exception = request.respond_to?(:get_header) ? request.get_header("omniauth.error") : request.env["omniauth.error"]
|
26
|
+
error = exception.error_reason if exception.respond_to?(:error_reason)
|
27
|
+
error ||= exception.description if exception.respond_to?(:description)
|
28
|
+
return error.to_s if error # Description and Error_reason are normal text
|
29
|
+
|
30
|
+
error ||= exception.error if exception.respond_to?(:error)
|
31
|
+
error ||= exception.code if exception.respond_to?(:code)
|
32
|
+
error ||= (request.respond_to?(:get_header) ? request.get_header("omniauth.error.type") : request.env["omniauth.error.type"]).to_s
|
33
|
+
error.to_s.humanize if error
|
34
|
+
end
|
35
|
+
|
36
|
+
def failure
|
37
|
+
# Might want to log this/instrument/notify
|
38
|
+
redirect_to root_path
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christopher Oezbek
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Rails generator to create an Omniauth installation for Devise including
|
14
|
+
social login buttons.
|
15
|
+
email:
|
16
|
+
- c.oezbek@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- CHANGELOG.md
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/omniauth/generator.rb
|
25
|
+
- lib/omniauth/generator/install_generator.rb
|
26
|
+
- lib/omniauth/generator/templates/_devise.rb
|
27
|
+
- lib/omniauth/generator/templates/_social_buttons.html.erb
|
28
|
+
- lib/omniauth/generator/templates/_user.rb
|
29
|
+
- lib/omniauth/generator/templates/omniauth_callbacks_controller.rb
|
30
|
+
- lib/omniauth/generator/version.rb
|
31
|
+
- sig/omniauth/generator.rbs
|
32
|
+
homepage: https://github.com/coezbek/omniauth-generator
|
33
|
+
licenses: []
|
34
|
+
metadata:
|
35
|
+
allowed_push_host: https://rubygems.org
|
36
|
+
homepage_uri: https://github.com/coezbek/omniauth-generator
|
37
|
+
source_code_uri: https://github.com/coezbek/omniauth-generator
|
38
|
+
changelog_uri: https://github.com/coezbek/omniauth-generator/README.md#Changelog
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.4.19
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Rails generator to create an Omniauth installation for Devise
|
58
|
+
test_files: []
|