Skip to content

Commit

Permalink
Check if user is authenticated before visiting the home page
Browse files Browse the repository at this point in the history
Co-authored-by: Cait <[email protected]>
Co-authored-by: Efua Akumanyi <[email protected]>
  • Loading branch information
3 people committed Aug 13, 2020
1 parent 8e73f2a commit 9983bf7
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 4 deletions.
Empty file.
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class ApplicationController < ActionController::Base
def new_session_path(scope)
new_user_session_path
end
end
5 changes: 3 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class HomeController < ApplicationController
before_action :authenticate_user!

def show
render body: "Hello from Staff Device"
end
end
end
14 changes: 14 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def cognito_idp
@user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, kind: "Cognito IDP") if is_navigational_format?
end
end

def failure
redirect_to root_path
end
end
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :omniauthable, omniauth_providers: %i[cognito-idp]
end

def from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create
end
end
3 changes: 3 additions & 0 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>Log in</h2>

<%= link_to "Sign in with Cognito", user_cognito_idp_omniauth_authorize_path %>
3 changes: 3 additions & 0 deletions app/views/home/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Hello from Staff Device</p>

<%= link_to "Logout", destroy_user_session_path, method: :delete %>
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Rails.application.routes.draw do
devise_for :users
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
devise_scope :user do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end

get "/healthcheck", to: "monitoring#healthcheck"
root "home#show"
end

0 comments on commit 9983bf7

Please sign in to comment.