Skip to content

Commit

Permalink
Add rails controllers to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Pablo Gil committed Oct 26, 2022
1 parent a757f9b commit 8f6d400
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

#= Application Controller
#
# Base controller for all other controllers.
class ApplicationController < ActionController::Base
helper_method :current_user, :user_signed_in?

def warden
request.env['warden']
end

def user_signed_in?(...)
warden.authenticated?(...)
end

def authenticate_user!(...)
session[:after_sign_in_path] = request.path unless user_signed_in?(...)
warden.authenticate!(...)
end

def after_sign_in_path
session.delete(:after_sign_in_path) || root_path
end

def current_user(...)
warden.user(...)
end
end

9 changes: 9 additions & 0 deletions lib/generators/auth/templates/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

#= HomeController
#
# Handles the home or start page actions.
class HomeController < ApplicationController
def show; end
end

43 changes: 43 additions & 0 deletions lib/generators/auth/templates/controllers/session_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

#= SessionController
#
# Handles the user session actions.
class SessionController < ApplicationController
def new; end

def create
token = IdentityPlatform::Token.load session_params[:token]
if token.valid? && sign_in_token_user(token)
redirect_to session.fetch :after_sign_in_path, root_path
else
render :new, status: :unprocessable_entity
end
end

def destroy
sign_out_user
redirect_to new_session_path
end

private

def session_params
params.require(:session).permit :token
end

def sign_in_token_user(token, scope: :default)
user = User.from_identity_token token
warden.set_user(user, scope: scope)
end

def sign_out_user(scope: nil)
if scope
warden.logout(scope)
warden.clear_strategies_cache!(scope: scope)
else
warden.logout
warden.clear_strategies_cache!
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# UnauthorizedController
#
# The controller configured to be used by Warden to deal whenever the
# authentication fails - either by harshly stopping with an HTTP 401 Unauthorized
# status, or redirecting to the sign-in page.
class UnauthorizedController < ActionController::Metal
include ActionController::Head
include ActionController::Redirecting
include Rails.application.routes.url_helpers

cattr_accessor :navigational_formats, default: ['*/*', :html]

def self.call(env)
@respond ||= action(:respond)
@respond.call(env)
end

def respond
return head :unauthorized unless navigational_format?

redirect_to sign_in_path, alert: 'You need to sign in before continuing.'
end

private

def navigational_format?
request.format.try(:ref).in? navigational_formats
end
end

0 comments on commit 8f6d400

Please sign in to comment.