mirror of
https://github.com/usetrmnl/terminus.git
synced 2026-08-13 14:29:27 -07:00
Necessary to reduce lines of code since both the Dry Monads and Initiable gems are commonly used. By requiring and -- in the case of Dry Monads -- including them within the base action, all actions can now inherit and immediately use this functionality. Milestone: patch
45 lines
893 B
Ruby
45 lines
893 B
Ruby
# auto_register: false
|
|
# frozen_string_literal: true
|
|
|
|
require "dry/monads"
|
|
require "hanami/action"
|
|
require "initable"
|
|
|
|
module Terminus
|
|
# The application base action.
|
|
class Action < Hanami::Action
|
|
include Dry::Monads[:result]
|
|
|
|
before :authorize
|
|
|
|
protected
|
|
|
|
def authorize request, response
|
|
rodauth = request.env["rodauth"]
|
|
|
|
return unless rodauth
|
|
|
|
handle_rodauth_redirect(rodauth, response) { rodauth.require_account }
|
|
|
|
response[:current_user_id] = rodauth.account_id
|
|
end
|
|
|
|
private
|
|
|
|
def handle_rodauth_redirect rodauth, response
|
|
halted = catch(:halt) { yield }
|
|
|
|
# :nocov:
|
|
return unless halted
|
|
|
|
code, headers, body = *halted
|
|
|
|
rodauth.flash.next.each { |key, value| response.flash[key] = value }
|
|
response.redirect headers["Location"], code
|
|
|
|
throw :halt, [code, body]
|
|
# :nocov:
|
|
end
|
|
end
|
|
end
|