Skip to content

Commit 15ece6e

Browse files
authored
Merge pull request #20 from fastruby/add-test_refactor_logic-changes
Big refactor: logic changes, usage changes, readme updates, added tests
2 parents 369ef64 + 7b53bb9 commit 15ece6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+302
-576
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/test/dummy/log/*.log
77
/test/dummy/storage/
88
/test/dummy/tmp/
9+
Gemfile.lock

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"

README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This gem provides an easy way to generate new (Devise) sessions for members of a
55
If a user is signing in with GitHub and they are a (public) member of the configured GitHub organization, they will be allowed in.
66

77
## Environment Variables
8+
9+
### GitHub Login
10+
811
Make sure you configure your ENV variables to use Github authentication.
912

1013
```
@@ -33,15 +36,39 @@ Once you create the app and generate credentials for it, make sure you add them
3336
GITHUB_APP_ID=xxxxxxxxxxxxxxxxxxxx
3437
GITHUB_APP_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3538
```
39+
40+
### Developer Login
41+
42+
To avoid the need of a GitHub application setup (useful for local development or Heroku Review Apps), the `developer` strategy can be enabled using setting a `SHOW_DEVELOPER_AUTH` variable with any non-blank value (`SHOW_DEVELOPER_AUTH=1` or `SHOW_DEVELOPER_AUTH=true` for example).
43+
3644
## Getting Started
3745

38-
- 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:
3965

4066
```ruby
4167
gem 'ombu_labs-auth'
4268
```
4369

4470
- And then execute:
71+
4572
```bash
4673
$ bundle
4774
```
@@ -61,12 +88,67 @@ mount OmbuLabs::Auth::Engine, at: '/', as: 'ombu_labs_auth'
6188
</div>
6289
```
6390

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+
6493
- Add the Devise authentication helper to your private objects controllers
6594

66-
```
95+
```rb
6796
before_action :authenticate_user!
6897
```
6998

99+
- Include the `OmbuLabsAuthenticable` concern in the authenticable model
100+
101+
```rb
102+
class Admin < ApplicationRecord
103+
include OmbuLabsAuthenticable
104+
...
105+
end
106+
```
107+
108+
- Tell `OmbuLabs::Auth` the user class name and table for the authenticable model
109+
110+
```rb
111+
# config/initializers/ombu_labs-auth.rb
112+
OmbuLabs::Auth.user_class_name = "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" %>
124+
```
125+
126+
### TODO: create a rails template to do all the previous steps automatically
127+
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+
148+
## Running tests
149+
150+
Run `rake app:test:all` to run all tests and `rake app:test` to skip system tests.
151+
70152
## Caveats
71153

72154
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
@@ -78,4 +160,5 @@ Have a fix for a problem you've been running into or an idea for a new feature y
78160
Take a look at the [Contributing document](https://github.com/fastruby/ombu_labs-auth/blob/main/CONTRIBUTING.md) for instructions to set up the repo on your machine and create a good Pull Request.
79161

80162
## License
163+
81164
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

app/assets/config/ombu_labs_auth_manifest.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/assets/stylesheets/ombu_labs/auth/application.css

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/controllers/ombu_labs/auth/callbacks_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def github
1212
member_logins = organization_members.map { |member| member["login"] }
1313

1414
if username.in?(member_logins)
15-
@user = User.from_omniauth(request.env["omniauth.auth"])
15+
@user = OmbuLabs::Auth.user_class.from_omniauth(request.env["omniauth.auth"])
1616
sign_in_and_redirect @user
1717
else
1818
flash[:error] = "This application is only available to members of #{organization_name}."
@@ -21,7 +21,7 @@ def github
2121
end
2222

2323
def developer
24-
@user = User.from_omniauth(request.env["omniauth.auth"])
24+
@user = OmbuLabs::Auth.user_class.from_omniauth(request.env["omniauth.auth"])
2525
sign_in_and_redirect @user
2626
end
2727

app/helpers/ombu_labs/auth/application_helper.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/jobs/ombu_labs/auth/application_job.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/mailers/ombu_labs/auth/application_mailer.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/models/concerns/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)