Skip to content

Commit b4f074e

Browse files
committed
feature/Add basic github omniauth login
1 parent de584f9 commit b4f074e

File tree

15 files changed

+138
-15
lines changed

15 files changed

+138
-15
lines changed

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ gem 'active_model_serializers', '~> 0.10.0'
2222

2323
gem 'byebug'
2424

25-
gem 'devise'
25+
gem "devise", github: "heartcombo/devise", branch: "master"
2626
gem "aws-sdk-s3", require: false
2727
gem 'will_paginate', '~> 3.1.0'
2828
gem 'redis'
29+
gem 'omniauth-github'
30+
gem 'omniauth-rails_csrf_protection'
2931

3032
# Use Redis adapter to run Action Cable in production
3133
# gem 'redis', '~> 4.0'

Gemfile.lock

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
GIT
2+
remote: https://github.com/heartcombo/devise.git
3+
revision: 0cd72a56f984a7ff089246f87a8b259120545edd
4+
branch: master
5+
specs:
6+
devise (4.7.3)
7+
bcrypt (~> 3.0)
8+
orm_adapter (~> 0.1)
9+
railties (>= 4.1.0)
10+
responders
11+
warden (~> 1.2.3)
12+
113
GEM
214
remote: https://rubygems.org/
315
specs:
@@ -79,7 +91,7 @@ GEM
7991
aws-sigv4 (~> 1.1)
8092
aws-sigv4 (1.2.1)
8193
aws-eventstream (~> 1, >= 1.0.2)
82-
bcrypt (3.1.13)
94+
bcrypt (3.1.16)
8395
bindex (0.8.1)
8496
bootsnap (1.4.6)
8597
msgpack (~> 1.0)
@@ -89,22 +101,23 @@ GEM
89101
activesupport
90102
concurrent-ruby (1.1.6)
91103
crass (1.0.6)
92-
devise (4.7.1)
93-
bcrypt (~> 3.0)
94-
orm_adapter (~> 0.1)
95-
railties (>= 4.1.0)
96-
responders
97-
warden (~> 1.2.3)
98104
erubi (1.9.0)
105+
faraday (1.3.0)
106+
faraday-net_http (~> 1.0)
107+
multipart-post (>= 1.2, < 3)
108+
ruby2_keywords
109+
faraday-net_http (1.0.1)
99110
ffi (1.12.2)
100111
globalid (0.4.2)
101112
activesupport (>= 4.2.0)
113+
hashie (4.1.0)
102114
i18n (1.8.2)
103115
concurrent-ruby (~> 1.0)
104116
jbuilder (2.10.0)
105117
activesupport (>= 5.0.0)
106118
jmespath (1.4.0)
107119
jsonapi-renderer (0.2.2)
120+
jwt (2.2.2)
108121
launchy (2.5.0)
109122
addressable (~> 2.7)
110123
letter_opener (1.7.0)
@@ -126,15 +139,39 @@ GEM
126139
mini_portile2 (2.4.0)
127140
minitest (5.14.0)
128141
msgpack (1.3.3)
142+
multi_json (1.15.0)
143+
multi_xml (0.6.0)
144+
multipart-post (2.1.1)
129145
nio4r (2.5.2)
130146
nokogiri (1.10.9)
131147
mini_portile2 (~> 2.4.0)
148+
oauth2 (1.4.4)
149+
faraday (>= 0.8, < 2.0)
150+
jwt (>= 1.0, < 3.0)
151+
multi_json (~> 1.3)
152+
multi_xml (~> 0.5)
153+
rack (>= 1.2, < 3)
154+
omniauth (2.0.3)
155+
hashie (>= 3.4.6)
156+
rack (>= 1.6.2, < 3)
157+
rack-protection
158+
omniauth-github (2.0.0)
159+
omniauth (~> 2.0)
160+
omniauth-oauth2 (~> 1.7.1)
161+
omniauth-oauth2 (1.7.1)
162+
oauth2 (~> 1.4)
163+
omniauth (>= 1.9, < 3)
164+
omniauth-rails_csrf_protection (1.0.0)
165+
actionpack (>= 4.2)
166+
omniauth (~> 2.0)
132167
orm_adapter (0.5.0)
133168
pg (1.2.3)
134169
public_suffix (4.0.6)
135170
puma (4.3.3)
136171
nio4r (~> 2.0)
137172
rack (2.2.2)
173+
rack-protection (2.1.0)
174+
rack
138175
rack-proxy (0.6.5)
139176
rack
140177
rack-test (1.1.0)
@@ -170,9 +207,10 @@ GEM
170207
rb-inotify (0.10.1)
171208
ffi (~> 1.0)
172209
redis (4.2.5)
173-
responders (3.0.0)
210+
responders (3.0.1)
174211
actionpack (>= 5.0)
175212
railties (>= 5.0)
213+
ruby2_keywords (0.0.4)
176214
ruby_dep (1.5.0)
177215
sass-rails (6.0.0)
178216
sassc-rails (~> 2.1, >= 2.1.1)
@@ -205,8 +243,8 @@ GEM
205243
turbolinks-source (5.2.0)
206244
tzinfo (1.2.7)
207245
thread_safe (~> 0.1)
208-
warden (1.2.8)
209-
rack (>= 2.0.6)
246+
warden (1.2.9)
247+
rack (>= 2.0.9)
210248
web-console (4.0.1)
211249
actionview (>= 6.0.0)
212250
activemodel (>= 6.0.0)
@@ -230,10 +268,12 @@ DEPENDENCIES
230268
aws-sdk-s3
231269
bootsnap (>= 1.4.2)
232270
byebug
233-
devise
271+
devise!
234272
jbuilder (~> 2.7)
235273
letter_opener
236274
listen (>= 3.0.5, < 3.2)
275+
omniauth-github
276+
omniauth-rails_csrf_protection
237277
pg (>= 0.18, < 2.0)
238278
puma (~> 4.1)
239279
rails (~> 6.0.2, >= 6.0.2.2)

app/controllers/home_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class HomeController < ApplicationController
22
def index
3+
@page_title = 'Home'
34
@display_popover = true
45

56
@languages = Language.order(name: :asc).to_json
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class 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+
5+
def github
6+
# 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"])
8+
9+
if @user.persisted?
10+
flash[:notice] = 'Signed in from GitHub'
11+
sign_in_and_redirect @user# this will throw if @user is not activated
12+
else
13+
# byebug
14+
# session["devise.github_data"] = request.env["omniauth.auth"].except(:extra) # Removing extra as it can overflow some session stores
15+
redirect_to new_user_registration_url(notice: 'Unable to sign in with Github. Perhaps you already signed up with the associated email?')
16+
end
17+
end
18+
19+
def failure
20+
redirect_to root_path
21+
end
22+
end

app/controllers/snippets_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class SnippetsController < ApplicationController
22
before_action :authenticate_user!, except: :show
33

44
def index
5+
@page_title = 'Stanley / Snippet'
56
@user = User.find_by(id: params[:user_id]) || current_user
67
@display_popover = true
78
@snippets = @user.filed_snippets.includes(:user, :folders)

app/models/user.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class User < ApplicationRecord
1010
# Include default devise modules. Others available are:
1111
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
1212
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :confirmable, :validatable
13+
devise :omniauthable, omniauth_providers: %i[github]
1314

1415
has_many :notifications
1516
has_many :folders
@@ -140,6 +141,18 @@ def self.authenticate(params)
140141
user&.valid_password?(params[:password]) ? user : nil
141142
end
142143

144+
def self.from_omniauth(auth)
145+
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
146+
user.email = auth.info.email
147+
user.password = Devise.friendly_token[0, 20]
148+
user.name = auth.info.nickname # assuming the user model has a name
149+
# user.image = auth.info.image # assuming the user model has an image
150+
# If you are using confirmable and the provider(s) you use validate emails,
151+
# uncomment the line below to skip the confirmation emails.
152+
user.skip_confirmation!
153+
end
154+
end
155+
143156
private
144157

145158
def create_default_folder

app/views/devise/registrations/new.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@
2626
<%= f.submit "SIGN UP", class: "mt-4 button--cta-primary w-full flex justify-center" %>
2727
<% end %>
2828

29+
<%= render partial: 'shared/omniauth_login' %>
30+
2931
<% end %>

app/views/devise/sessions/new.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@
2525
<%= f.submit "SIGN IN", class: "mt-4 button--cta-primary w-full flex justify-center " %>
2626
<% end %>
2727

28+
<%= render partial: 'shared/omniauth_login' %>
29+
2830
<% end %>

app/views/modals/users/sign_in.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@
2525
<%= f.submit "SIGN IN", class: "mt-4 button--cta-primary w-full flex justify-center " %>
2626
<% end %>
2727

28+
<%= render partial: 'shared/omniauth_login' %>
29+
2830
<% end %>

app/views/modals/users/sign_up.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
<%= f.submit "SIGN UP", class: "mt-4 button--cta-primary w-full flex justify-center" %>
3030
<% end %>
3131

32+
<%= render partial: 'shared/omniauth_login' %>
33+
3234
<% end %>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="relative mt-4">
2+
<div class="absolute inset-0 flex items-center">
3+
<div class="w-full border-t border-gray-300"></div>
4+
</div>
5+
<div class="relative flex justify-center text-sm leading-5">
6+
<span class="px-2 bg-cyan-light text-gray-500">
7+
Or continue with
8+
</span>
9+
</div>
10+
</div>
11+
12+
<div class="mt-4">
13+
<div>
14+
<span class="w-full inline-flex rounded-md shadow-sm">
15+
<%= link_to user_github_omniauth_authorize_path, method: :post, class: "w-full inline-flex justify-center py-2 px-4 border border-gray-300 rounded-md bg-white text-sm leading-5 font-medium text-gray-500 hover:text-gray-400 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition duration-150 ease-in-out" do %>
16+
<svg class="h-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
17+
<path fill-rule="evenodd" d="M10 0C4.477 0 0 4.484 0 10.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0110 4.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.203 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.942.359.31.678.921.678 1.856 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0020 10.017C20 4.484 15.522 0 10 0z" clip-rule="evenodd"></path>
18+
</svg>
19+
GitHub
20+
<% end %>
21+
</span>
22+
</div>
23+
</div>

config/initializers/devise.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@
259259
# ==> OmniAuth
260260
# Add a new OmniAuth provider. Check the wiki for more information on setting
261261
# up on your models and hooks.
262-
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
262+
config.omniauth :github, 'c9ba150c81465d914e7c', '9ef7c9d7a3d348911dc1108c71c3769c835c5374', scope: 'user'
263+
264+
OmniAuth.config.before_request_phase do |env|
265+
env['HTTP_REFERER'] = nil
266+
end
263267

264268
# ==> Warden configuration
265269
# If you want to use other strategies, that are not supported by Devise, or

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
registrations: 'registrations',
1111
sessions: 'sessions',
1212
passwords: 'passwords',
13-
confirmations: 'confirmations'
13+
confirmations: 'confirmations',
14+
omniauth_callbacks: 'omniauth_callbacks'
1415
}
1516
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
1617
root to: 'home#index'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddOmniauthToUsers < ActiveRecord::Migration[6.0]
2+
def change
3+
add_column :users, :provider, :string
4+
add_column :users, :uid, :string
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2021_02_15_163203) do
13+
ActiveRecord::Schema.define(version: 2021_02_21_152520) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -126,6 +126,8 @@
126126
t.datetime "confirmed_at"
127127
t.datetime "confirmation_sent_at", default: -> { "now()" }
128128
t.string "unconfirmed_email"
129+
t.string "provider"
130+
t.string "uid"
129131
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
130132
t.index ["email"], name: "index_users_on_email", unique: true
131133
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

0 commit comments

Comments
 (0)