Skip to content

Commit cf64472

Browse files
Merge branch 'devise'
2 parents 8365a56 + a8ca870 commit cf64472

File tree

12 files changed

+121
-30
lines changed

12 files changed

+121
-30
lines changed

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@ gem "sprockets-rails"
1010

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

Gemfile.lock

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ PATH
33
specs:
44
ombu_labs-auth (0.1.0)
55
devise (~> 4.8.1)
6-
rails (>= 7.0.4)
6+
omniauth (~> 2.1.0)
7+
omniauth-github (~> 2.0.0)
8+
rails (~> 7.0.2)
79

810
GEM
911
remote: https://rubygems.org/
@@ -84,10 +86,16 @@ GEM
8486
responders
8587
warden (~> 1.2.3)
8688
erubi (1.11.0)
89+
faraday (2.6.0)
90+
faraday-net_http (>= 2.0, < 3.1)
91+
ruby2_keywords (>= 0.0.4)
92+
faraday-net_http (3.0.1)
8793
globalid (1.0.0)
8894
activesupport (>= 5.0)
95+
hashie (5.0.0)
8996
i18n (1.12.0)
9097
concurrent-ruby (~> 1.0)
98+
jwt (2.5.0)
9199
loofah (2.19.0)
92100
crass (~> 1.0.2)
93101
nokogiri (>= 1.5.9)
@@ -97,6 +105,7 @@ GEM
97105
method_source (1.0.0)
98106
mini_mime (1.1.2)
99107
minitest (5.16.3)
108+
multi_xml (0.6.0)
100109
net-imap (0.3.1)
101110
net-protocol
102111
net-pop (0.1.2)
@@ -108,10 +117,29 @@ GEM
108117
nio4r (2.5.8)
109118
nokogiri (1.13.8-arm64-darwin)
110119
racc (~> 1.4)
120+
oauth2 (2.0.9)
121+
faraday (>= 0.17.3, < 3.0)
122+
jwt (>= 1.0, < 3.0)
123+
multi_xml (~> 0.5)
124+
rack (>= 1.2, < 4)
125+
snaky_hash (~> 2.0)
126+
version_gem (~> 1.1)
127+
omniauth (2.1.0)
128+
hashie (>= 3.4.6)
129+
rack (>= 2.2.3)
130+
rack-protection
131+
omniauth-github (2.0.1)
132+
omniauth (~> 2.0)
133+
omniauth-oauth2 (~> 1.8)
134+
omniauth-oauth2 (1.8.0)
135+
oauth2 (>= 1.4, < 3)
136+
omniauth (~> 2.0)
111137
orm_adapter (0.5.0)
112138
pg (1.4.4)
113139
racc (1.6.0)
114140
rack (2.2.4)
141+
rack-protection (3.0.2)
142+
rack
115143
rack-test (2.0.2)
116144
rack (>= 1.3)
117145
rails (7.0.4)
@@ -144,6 +172,10 @@ GEM
144172
responders (3.0.1)
145173
actionpack (>= 5.0)
146174
railties (>= 5.0)
175+
ruby2_keywords (0.0.5)
176+
snaky_hash (2.0.1)
177+
hashie
178+
version_gem (~> 1.1, >= 1.1.1)
147179
sprockets (4.1.1)
148180
concurrent-ruby (~> 1.0)
149181
rack (> 1, < 3)
@@ -155,6 +187,7 @@ GEM
155187
timeout (0.3.0)
156188
tzinfo (2.0.5)
157189
concurrent-ruby (~> 1.0)
190+
version_gem (1.1.1)
158191
warden (1.2.9)
159192
rack (>= 2.0.9)
160193
websocket-driver (0.7.5)
@@ -166,7 +199,6 @@ PLATFORMS
166199
arm64-darwin-21
167200

168201
DEPENDENCIES
169-
devise
170202
ombu_labs-auth!
171203
pg
172204
sprockets-rails
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module OmbuLabs
22
module Auth
33
class ApplicationController < ActionController::Base
4+
def after_sign_in_path_for(resource)
5+
request.base_url
6+
end
47
end
58
end
69
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "open-uri"
2+
3+
module OmbuLabs
4+
module Auth
5+
class CallbacksController < Devise::OmniauthCallbacksController
6+
skip_before_action :verify_authenticity_token, only: :developer
7+
8+
def github
9+
username = request.env["omniauth.auth"]["extra"]["raw_info"]["login"]
10+
11+
organization_name = ENV["ORGANIZATION_LOGIN"]
12+
member_logins = organization_members.map { |member| member["login"] }
13+
14+
if username.in?(member_logins)
15+
@user = User.from_omniauth(request.env["omniauth.auth"])
16+
sign_in_and_redirect @user
17+
else
18+
flash[:error] = "This application is only available to members of #{organization_name}."
19+
redirect_to new_user_session_path
20+
end
21+
end
22+
23+
def developer
24+
@user = User.from_omniauth(request.env["omniauth.auth"])
25+
sign_in_and_redirect @user
26+
end
27+
28+
private
29+
30+
def organization_members
31+
@organization_members ||= begin
32+
members_raw_response = URI.open("https://api.github.com/orgs/#{ENV["ORGANIZATION_LOGIN"]}/members").read
33+
JSON.parse(members_raw_response)
34+
end
35+
end
36+
end
37+
end
38+
end

app/models/ombu_labs/auth/user.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
module OmbuLabs::Auth
22
class User < ApplicationRecord
3-
# Include default devise modules. Others available are:
4-
# :confirmable, :lockable, :timeoutable and :omniauthable
5-
devise :database_authenticatable, :registerable,
6-
:recoverable, :rememberable, :validatable
7-
:trackable
3+
self.table_name = "users"
4+
5+
# Include default devise modules. Others available are:
6+
# :confirmable, :lockable, :timeoutable
7+
devise :database_authenticatable, :registerable,
8+
:recoverable, :rememberable, :trackable,
9+
:validatable, :omniauthable
10+
11+
def self.from_omniauth(auth)
12+
user_attributes = {
13+
email: auth.info.email,
14+
name: auth.info.name,
15+
password: Devise.friendly_token[0, 20]
16+
}
17+
where(provider: auth.provider, uid: auth.uid).first_or_create.tap { |user| user.update(user_attributes) }
18+
end
819
end
920
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div class="fluid-container center floor signin">
2+
<h2 class="new-edit-title">Sign in</h2>
3+
4+
<% if flash[:error] %>
5+
<div class="alert alert-danger" role="alert"><%= flash[:error] %></div>
6+
<% end %>
7+
8+
<%- if devise_mapping.omniauthable? %>
9+
<%- resource_class.omniauth_providers.each do |provider| %>
10+
<%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), class: "button", id:"back", method: :post, data: { 'turbo-method' => :post } %><br />
11+
<%= button_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), class: "button", method: :post %><br />
12+
<% end -%>
13+
<% end -%>
14+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<%- if devise_mapping.omniauthable? %>
2+
<%- resource_class.omniauth_providers.each do |provider| %>
3+
<%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), class: "button", id:"back" %><br />
4+
<% end -%>
5+
<% end -%>

config/initializers/devise.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# ==> Controller configuration
2020
# Configure the parent class to the devise controllers.
21-
config.parent_controller = 'Ombulabs::Auth::ApplicationController'
21+
config.parent_controller = 'OmbuLabs::Auth::ApplicationController'
2222

2323
# ==> Mailer Configuration
2424
# Configure the e-mail address which will be shown in Devise::Mailer,

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", module: :devise
2+
devise_for :users, class_name: OmbuLabs::Auth::User, module: :devise, controllers: { omniauth_callbacks: 'ombu_labs/auth/callbacks' }
33
end

db/migrate/20221014183623_devise_create_ombu_labs_auth_users.rb

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

3-
class DeviseCreateOmbuLabsAuthUsers < ActiveRecord::Migration[7.0]
3+
class DeviseCreateUsers < ActiveRecord::Migration[7.0]
44
def change
5-
create_table :ombu_labs_auth_users do |t|
5+
create_table :users do |t|
66
## Database authenticatable
77
t.string :email, null: false, default: ""
88
t.string :encrypted_password, null: false, default: ""
@@ -36,9 +36,9 @@ def change
3636
t.timestamps null: false
3737
end
3838

39-
add_index :ombu_labs_auth_users, :email, unique: true
40-
add_index :ombu_labs_auth_users, :reset_password_token, unique: true
41-
# add_index :ombu_labs_auth_users, :confirmation_token, unique: true
42-
# add_index :ombu_labs_auth_users, :unlock_token, unique: true
39+
add_index :users, :email, unique: true
40+
add_index :users, :reset_password_token, unique: true
41+
# add_index :users, :confirmation_token, unique: true
42+
# add_index :users, :unlock_token, unique: true
4343
end
4444
end

0 commit comments

Comments
 (0)