Commit cfaecc8

mo <mo.khan@gmail.com>
2017-10-23 01:21:19
extract user
1 parent bd88160
airport/app/assets/javascripts/application.js
@@ -10,6 +10,7 @@
 // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
 // about supported directives.
 //
+//= require bootstrap
 //= require rails-ujs
 //= require turbolinks
 //= require_tree .
airport/app/assets/stylesheets/application.css
@@ -10,6 +10,7 @@
  * files in this directory. Styles in this file should be added after the last require_* statement.
  * It is generally better to create a new file per style scope.
  *
+ *= require bootstrap
  *= require_tree .
  *= require_self
  */
airport/app/controllers/application_controller.rb
@@ -1,3 +1,18 @@
 class ApplicationController < ActionController::Base
   protect_from_forgery with: :exception
+  helper_method :current_user
+  before_action :authenticate!
+
+  def current_user
+    return nil unless session[:user_id].present?
+    @current_user ||= User.new(id: session[:user_id], email: session[:email])
+  end
+
+  def current_user?
+    current_user.present?
+  end
+
+  def authenticate!
+    redirect_to new_session_path unless current_user?
+  end
 end
airport/app/controllers/dashboard_controller.rb
@@ -1,6 +1,4 @@
 class DashboardController < ApplicationController
   def show
-    @user_id = session[:user_id]
-    @email = session[:email]
   end
 end
airport/app/controllers/sessions_controller.rb
@@ -1,5 +1,6 @@
 class SessionsController < ApplicationController
   skip_before_action :verify_authenticity_token, only: [:create]
+  skip_before_action :authenticate!
 
   def new
     uri = URI.parse(Rails.configuration.x.authentication_host)
airport/app/models/user.rb
@@ -0,0 +1,8 @@
+class User
+  attr_reader :id, :email
+
+  def initialize(attributes)
+    @id = attributes[:id]
+    @email = attributes[:email]
+  end
+end
airport/app/views/dashboard/show.html.erb
@@ -1,1 +1,7 @@
-<h1>Welcome <%= @user_id %>:<%= @email %></h1>
+<div class="container">
+  <div class="row">
+    <div class="col">
+      <h1>Welcome <%= current_user.id %>:<%= current_user.email %></h1>
+    </div>
+  </div>
+</div>
airport/Gemfile
@@ -59,3 +59,4 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
 gem 'dotenv-rails'
 gem "factory_girl_rails", "~> 4.0"
 gem "saml-kit", path: "../saml-kit"
+gem "rails-assets-bootstrap", source: "https://rails-assets.org"
airport/Gemfile.lock
@@ -7,6 +7,7 @@ PATH
 
 GEM
   remote: https://rubygems.org/
+  remote: https://rails-assets.org/
   specs:
     actioncable (5.1.4)
       actionpack (= 5.1.4)
@@ -130,6 +131,9 @@ GEM
       bundler (>= 1.3.0)
       railties (= 5.1.4)
       sprockets-rails (>= 2.0.0)
+    rails-assets-bootstrap (3.3.7)
+      rails-assets-jquery (>= 1.9.1, < 4)
+    rails-assets-jquery (3.2.1)
     rails-controller-testing (1.0.2)
       actionpack (~> 5.x, >= 5.0.1)
       actionview (~> 5.x, >= 5.0.1)
@@ -235,6 +239,7 @@ DEPENDENCIES
   listen (>= 3.0.5, < 3.2)
   puma (~> 3.7)
   rails (~> 5.1.4)
+  rails-assets-bootstrap!
   rails-controller-testing
   rspec-rails (~> 3.6)
   saml-kit!