diff --git a/Gemfile b/Gemfile index de3f2006..b7125830 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 0eea0429..d902c6d0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d12..22e7ea78 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,7 @@ class ApplicationController < ActionController::Base + helper_method :user_signed_in? + + def user_signed_in? + session[:current_user].present? + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 00000000..df9d04e2 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d537ac95..63130f29 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,6 +10,14 @@
++ <% if user_signed_in? %> + You are logged in as <%= session[:current_user]['name'] %> + <% else %> + <%= link_to 'Login (developer)', '/auth/developer' %> + <% end %> +
+ <%= yield %>