Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Setup omniauth login with sample 'developer' provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Sabin committed Jul 6, 2018
1 parent e4e2fb5 commit 0284c68
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ gem 'jbuilder', '~> 2.5'
gem 'bootsnap', '>= 1.1.0', require: false

gem 'rspec-rails'
gem 'omniauth'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ GEM
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
hashie (3.5.7)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
io-like (0.3.0)
Expand Down Expand Up @@ -106,6 +107,9 @@ GEM
nio4r (2.3.1)
nokogiri (1.8.3)
mini_portile2 (~> 2.3.0)
omniauth (1.8.1)
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
pg (1.0.0)
public_suffix (3.0.2)
puma (3.11.4)
Expand Down Expand Up @@ -217,6 +221,7 @@ DEPENDENCIES
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
omniauth
pg (>= 0.18, < 2.0)
puma (~> 3.11)
rails (~> 5.2.0)
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class ApplicationController < ActionController::Base
helper_method :user_signed_in?

def user_signed_in?
session[:current_user].present?
end
end
18 changes: 18 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class SessionsController < ApplicationController
skip_before_action :verify_authenticity_token, only: :create

def create
session[:current_user] = {
name: auth_hash.info.name,
email: auth_hash.info.email
}

redirect_to root_path
end

protected

def auth_hash
request.env['omniauth.auth']
end
end
8 changes: 8 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
</head>

<body>
<p>
<% if user_signed_in? %>
You are logged in as <%= session[:current_user]['name'] %>
<% else %>
<%= link_to 'Login (developer)', '/auth/developer' %>
<% end %>
</p>

<%= yield %>
</body>
</html>
5 changes: 5 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
provider :developer
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
resources :stuffs

root 'stuffs#index'

post '/auth/:provider/callback', to: 'sessions#create'
end

0 comments on commit 0284c68

Please sign in to comment.