Skip to content

Commit 31fa05e

Browse files
committed
Refactor code, change some logic, update readme, added tests
1 parent 1d4e7f5 commit 31fa05e

32 files changed

+475
-136
lines changed

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
44
# Specify your gem's dependencies in ombu_labs-auth.gemspec.
55
gemspec
66

7-
gem "pg"
7+
gem 'sqlite3', '~> 1.5', '>= 1.5.3'
88

99
gem "sprockets-rails"
1010

1111
# Start debugger with binding.b [https://github.com/ruby/debug]
12-
# gem "debug", ">= 1.0.0"
12+
gem "debug", ">= 1.0.0"

Gemfile.lock

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
PATH
2+
remote: .
3+
specs:
4+
ombu_labs-auth (0.1.0)
5+
devise (~> 4.8.1)
6+
omniauth (~> 2.1.0)
7+
omniauth-github (~> 2.0.0)
8+
omniauth-rails_csrf_protection
9+
rails (>= 6.0)
10+
11+
GEM
12+
remote: https://rubygems.org/
13+
specs:
14+
actioncable (6.1.7)
15+
actionpack (= 6.1.7)
16+
activesupport (= 6.1.7)
17+
nio4r (~> 2.0)
18+
websocket-driver (>= 0.6.1)
19+
actionmailbox (6.1.7)
20+
actionpack (= 6.1.7)
21+
activejob (= 6.1.7)
22+
activerecord (= 6.1.7)
23+
activestorage (= 6.1.7)
24+
activesupport (= 6.1.7)
25+
mail (>= 2.7.1)
26+
actionmailer (6.1.7)
27+
actionpack (= 6.1.7)
28+
actionview (= 6.1.7)
29+
activejob (= 6.1.7)
30+
activesupport (= 6.1.7)
31+
mail (~> 2.5, >= 2.5.4)
32+
rails-dom-testing (~> 2.0)
33+
actionpack (6.1.7)
34+
actionview (= 6.1.7)
35+
activesupport (= 6.1.7)
36+
rack (~> 2.0, >= 2.0.9)
37+
rack-test (>= 0.6.3)
38+
rails-dom-testing (~> 2.0)
39+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
40+
actiontext (6.1.7)
41+
actionpack (= 6.1.7)
42+
activerecord (= 6.1.7)
43+
activestorage (= 6.1.7)
44+
activesupport (= 6.1.7)
45+
nokogiri (>= 1.8.5)
46+
actionview (6.1.7)
47+
activesupport (= 6.1.7)
48+
builder (~> 3.1)
49+
erubi (~> 1.4)
50+
rails-dom-testing (~> 2.0)
51+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
52+
activejob (6.1.7)
53+
activesupport (= 6.1.7)
54+
globalid (>= 0.3.6)
55+
activemodel (6.1.7)
56+
activesupport (= 6.1.7)
57+
activerecord (6.1.7)
58+
activemodel (= 6.1.7)
59+
activesupport (= 6.1.7)
60+
activestorage (6.1.7)
61+
actionpack (= 6.1.7)
62+
activejob (= 6.1.7)
63+
activerecord (= 6.1.7)
64+
activesupport (= 6.1.7)
65+
marcel (~> 1.0)
66+
mini_mime (>= 1.1.0)
67+
activesupport (6.1.7)
68+
concurrent-ruby (~> 1.0, >= 1.0.2)
69+
i18n (>= 1.6, < 2)
70+
minitest (>= 5.1)
71+
tzinfo (~> 2.0)
72+
zeitwerk (~> 2.3)
73+
addressable (2.8.0)
74+
public_suffix (>= 2.0.2, < 5.0)
75+
bcrypt (3.1.18)
76+
builder (3.2.4)
77+
capybara (3.36.0)
78+
addressable
79+
matrix
80+
mini_mime (>= 0.1.3)
81+
nokogiri (~> 1.8)
82+
rack (>= 1.6.0)
83+
rack-test (>= 0.6.3)
84+
regexp_parser (>= 1.5, < 3.0)
85+
xpath (~> 3.2)
86+
childprocess (4.1.0)
87+
concurrent-ruby (1.1.10)
88+
crass (1.0.6)
89+
debug (1.6.3)
90+
irb (>= 1.3.6)
91+
reline (>= 0.3.1)
92+
devise (4.8.1)
93+
bcrypt (~> 3.0)
94+
orm_adapter (~> 0.1)
95+
railties (>= 4.1.0)
96+
responders
97+
warden (~> 1.2.3)
98+
erubi (1.11.0)
99+
faraday (2.6.0)
100+
faraday-net_http (>= 2.0, < 3.1)
101+
ruby2_keywords (>= 0.0.4)
102+
faraday-net_http (3.0.1)
103+
globalid (1.0.0)
104+
activesupport (>= 5.0)
105+
hashie (5.0.0)
106+
i18n (1.12.0)
107+
concurrent-ruby (~> 1.0)
108+
io-console (0.5.11)
109+
irb (1.4.2)
110+
reline (>= 0.3.0)
111+
jwt (2.5.0)
112+
loofah (2.19.0)
113+
crass (~> 1.0.2)
114+
nokogiri (>= 1.5.9)
115+
mail (2.7.1)
116+
mini_mime (>= 0.1.1)
117+
marcel (1.0.2)
118+
matrix (0.4.2)
119+
method_source (1.0.0)
120+
mini_mime (1.1.2)
121+
minitest (5.16.3)
122+
multi_xml (0.6.0)
123+
nio4r (2.5.8)
124+
nokogiri (1.13.9-x86_64-darwin)
125+
racc (~> 1.4)
126+
oauth2 (2.0.9)
127+
faraday (>= 0.17.3, < 3.0)
128+
jwt (>= 1.0, < 3.0)
129+
multi_xml (~> 0.5)
130+
rack (>= 1.2, < 4)
131+
snaky_hash (~> 2.0)
132+
version_gem (~> 1.1)
133+
omniauth (2.1.0)
134+
hashie (>= 3.4.6)
135+
rack (>= 2.2.3)
136+
rack-protection
137+
omniauth-github (2.0.1)
138+
omniauth (~> 2.0)
139+
omniauth-oauth2 (~> 1.8)
140+
omniauth-oauth2 (1.8.0)
141+
oauth2 (>= 1.4, < 3)
142+
omniauth (~> 2.0)
143+
omniauth-rails_csrf_protection (1.0.1)
144+
actionpack (>= 4.2)
145+
omniauth (~> 2.0)
146+
orm_adapter (0.5.0)
147+
public_suffix (4.0.7)
148+
puma (4.3.12)
149+
nio4r (~> 2.0)
150+
racc (1.6.0)
151+
rack (2.2.4)
152+
rack-protection (3.0.2)
153+
rack
154+
rack-test (2.0.2)
155+
rack (>= 1.3)
156+
rails (6.1.7)
157+
actioncable (= 6.1.7)
158+
actionmailbox (= 6.1.7)
159+
actionmailer (= 6.1.7)
160+
actionpack (= 6.1.7)
161+
actiontext (= 6.1.7)
162+
actionview (= 6.1.7)
163+
activejob (= 6.1.7)
164+
activemodel (= 6.1.7)
165+
activerecord (= 6.1.7)
166+
activestorage (= 6.1.7)
167+
activesupport (= 6.1.7)
168+
bundler (>= 1.15.0)
169+
railties (= 6.1.7)
170+
sprockets-rails (>= 2.0.0)
171+
rails-dom-testing (2.0.3)
172+
activesupport (>= 4.2.0)
173+
nokogiri (>= 1.6)
174+
rails-html-sanitizer (1.4.3)
175+
loofah (~> 2.3)
176+
railties (6.1.7)
177+
actionpack (= 6.1.7)
178+
activesupport (= 6.1.7)
179+
method_source
180+
rake (>= 12.2)
181+
thor (~> 1.0)
182+
rake (13.0.6)
183+
regexp_parser (2.5.0)
184+
reline (0.3.1)
185+
io-console (~> 0.5)
186+
responders (3.0.1)
187+
actionpack (>= 5.0)
188+
railties (>= 5.0)
189+
rexml (3.2.5)
190+
ruby2_keywords (0.0.5)
191+
rubyzip (2.3.2)
192+
selenium-webdriver (4.1.0)
193+
childprocess (>= 0.5, < 5.0)
194+
rexml (~> 3.2, >= 3.2.5)
195+
rubyzip (>= 1.2.2)
196+
snaky_hash (2.0.1)
197+
hashie
198+
version_gem (~> 1.1, >= 1.1.1)
199+
sprockets (4.1.1)
200+
concurrent-ruby (~> 1.0)
201+
rack (> 1, < 3)
202+
sprockets-rails (3.4.2)
203+
actionpack (>= 5.2)
204+
activesupport (>= 5.2)
205+
sprockets (>= 3.0.0)
206+
sqlite3 (1.5.3-x86_64-darwin)
207+
thor (1.2.1)
208+
tzinfo (2.0.5)
209+
concurrent-ruby (~> 1.0)
210+
version_gem (1.1.1)
211+
warden (1.2.9)
212+
rack (>= 2.0.9)
213+
webdrivers (5.0.0)
214+
nokogiri (~> 1.6)
215+
rubyzip (>= 1.3.0)
216+
selenium-webdriver (~> 4.0)
217+
websocket-driver (0.7.5)
218+
websocket-extensions (>= 0.1.0)
219+
websocket-extensions (0.1.5)
220+
xpath (3.2.0)
221+
nokogiri (~> 1.8)
222+
zeitwerk (2.6.3)
223+
224+
PLATFORMS
225+
x86_64-darwin-19
226+
227+
DEPENDENCIES
228+
capybara
229+
debug (>= 1.0.0)
230+
ombu_labs-auth!
231+
puma
232+
sprockets-rails
233+
sqlite3 (~> 1.5, >= 1.5.3)
234+
webdrivers
235+
236+
BUNDLED WITH
237+
2.2.29

README.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,25 @@ To avoid the need of a GitHub application setup (useful for local development or
4343

4444
## Getting Started
4545

46-
- Add these lines to your application's Gemfile:
46+
### Requirements
47+
48+
A `User`-like model that will be used for the authentication (`User`, `Admin`, `Client`, etc).
49+
50+
The database table for that model must have, at least, these fields:
51+
52+
```rb
53+
create_table :clients do |t|
54+
t.string :email, unique: true
55+
t.string :provider
56+
t.string :uid, unique: true
57+
t.string :name
58+
t.string :encrypted_password
59+
end
60+
```
61+
62+
### Installation
63+
64+
- Add this line to your application's Gemfile:
4765

4866
```ruby
4967
gem 'ombu_labs-auth'
@@ -70,6 +88,8 @@ mount OmbuLabs::Auth::Engine, at: '/', as: 'ombu_labs_auth'
7088
</div>
7189
```
7290

91+
> This will default to a basic HTML page included in this gem. To customize this view, check [this section](#customizing-sign-in-page)
92+
7393
- Add the Devise authentication helper to your private objects controllers
7494

7595
```rb
@@ -79,21 +99,52 @@ before_action :authenticate_user!
7999
- Include the `OmbuLabsAuthenticable` concern in the authenticable model
80100

81101
```rb
82-
class User < ApplicationRecord
102+
class Admin < ApplicationRecord
83103
include OmbuLabsAuthenticable
84104
...
85105
end
86106
```
87107

88-
- Tell `OmbuLabs::Auth` the class name for the authenticable model
108+
- Tell `OmbuLabs::Auth` the user class name and table for the authenticable model
89109

90110
```rb
91111
# config/initializers/ombu_labs-auth.rb
92-
OmbuLabs::Auth.user_class = 'User'
112+
OmbuLabs::Auth.user_class = "Admin" # defaults to "User" if not set
113+
OmbuLabs::Auth.users_table_name = :admins # defaults to :users if not set
114+
```
115+
116+
> You can skip this step if the table is called `users` and the model is called `User`
117+
118+
- Log Out action
119+
120+
A link to `ombu_labs_auth.destroy_user_session_path` with method `DELETE` can be used. If rails-ujs is not available, a `button_to` can be used.
121+
122+
```
123+
<%= link_to "Sign out", ombu_labs_auth.destroy_user_session_path, method: :delete, class: "button magenta" %>
93124
```
94125

95126
### TODO: create a rails template to do all the previous steps automatically
96127

128+
## Customizing sign in page
129+
130+
The gem provides a basic html template to select the authentication method. To customize it, create a view at `views/devise/session/new.html.erb` and a layout at `views/layouts/devise.html.erb`.
131+
132+
Include this snippet in the `new` view:
133+
134+
```
135+
<%- Devise.omniauth_providers.each do |provider| %>
136+
<%= button_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(OmbuLabs::Auth.user_class, provider), method: :post %><br />
137+
<% end -%>
138+
```
139+
140+
To use a `link_to` helper instead of a `button_to` helper to, rails-ujs is needed to support making a `POST` request with link tags. Then, replace with:
141+
142+
```
143+
<%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(OmbuLabs::Auth.user_class, provider), method: :post, data: { 'turbo-method' => :post } %><br />
144+
```
145+
146+
> If this intermediate page is not needed, the button/link to `omniauth_authorize_path` can be used directly.
147+
97148
## Caveats
98149

99150
Please be aware this gem is a mountable engine which depends on Devise, and it's not possible to mount it multiple times. Refer to their Wiki for more on the issue - https://github.com/heartcombo/devise/wiki/How-To:-Use-devise-inside-a-mountable-engine

app/controllers/ombu_labs/auth/callbacks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module OmbuLabs
44
module Auth
55
class CallbacksController < Devise::OmniauthCallbacksController
6-
skip_before_action :verify_authenticity_token, only: :developer
6+
skip_before_action :verify_authenticity_token
77

88
def github
99
username = request.env["omniauth.auth"]["extra"]["raw_info"]["login"]

app/models/concerns/ombu_labs_authenticable.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ module OmbuLabsAuthenticable
66
included do
77
# Include default devise modules. Others available are:
88
# :confirmable, :lockable, :timeoutable
9-
devise :database_authenticatable, :registerable,
10-
:recoverable, :rememberable, :trackable,
11-
:validatable, :omniauthable
9+
devise :database_authenticatable, :omniauthable
1210
end
1311

1412
class_methods do
1513
def from_omniauth(auth)
1614
user_attributes = {
17-
email: auth.info.email,
18-
name: auth.info.name,
15+
email: auth["info"]["email"],
16+
name: auth["info"]["name"],
1917
password: Devise.friendly_token[0, 20]
2018
}
21-
where(provider: auth.provider, uid: auth.uid).first_or_create.tap { |user| user.update(user_attributes) }
19+
where(provider: auth["provider"], uid: auth["uid"]).first_or_create.tap { |user| user.update(user_attributes) }
2220
end
2321
end
2422
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h2 class="new-edit-title">Sign in</h2>
2+
3+
<% if flash[:error] %>
4+
<div class="alert alert-danger" role="alert"><%= flash[:error] %></div>
5+
<% end %>
6+
<%- Devise.omniauth_providers.each do |provider| %>
7+
<%= button_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(OmbuLabs::Auth.user_class, provider), method: :post %><br />
8+
<% end -%>

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
OmbuLabs::Auth::Engine.routes.draw do
2-
devise_for :users, class_name: OmbuLabs::Auth.user_class_name, module: :devise, controllers: { omniauth_callbacks: 'ombu_labs/auth/callbacks' }
2+
devise_for OmbuLabs::Auth.users_table_name, class_name: OmbuLabs::Auth.user_class_name, module: :devise, controllers: { omniauth_callbacks: 'ombu_labs/auth/callbacks' }
33
end

0 commit comments

Comments
 (0)