Friday, September 20, 2024 12:04:40 AM
> settings

Customize


Authenticate

> application_controller.rb
# frozen_string_literal: true

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :store_user_location!, if: :storable_location?
  before_action :prepare_exception_notifier

  def redirect_not_found
    raise ActionController::RoutingError.new("Not Found")
  end

  protected

  def authenticate_user!
    return super if user_signed_in?

    redirect_to login_path
  end

  def after_sign_in_path_for(resource)
    if session[:registering]
      session.delete(:registering)
      edit_user_path(resource.discord_id)
    else
      stored_location_for(resource) || super || dashboard_index_path
    end
  end

  def load_downloads
    download_struct = Struct.new(:version, :link)
    downloads = Download.all.order(created_at: :asc)
    current_release = downloads.find(&:current_release?)
    archived_releases = downloads.select { |download| !download.current_release? }

    @releases = []
    @releases << download_struct.new("Latest", latest_downloads_path) if current_release.present?
    @releases += archived_releases.map do |release|
      download_struct.new(release.version, download_path(release.uuid))
    end
  end

  def prepare_exception_notifier
    return unless user_signed_in?

    request.env["exception_notifier.exception_data"] = current_user&.to_error_h
  end

  # Its important that the location is NOT stored if:
  # - The request method is not GET (non idempotent)
  # - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an
  #    infinite redirect loop.
  # - The request is an Ajax request as this can lead to very unexpected behaviour.
  def storable_location?
    deny_list = [login_path]
    request.get? && is_navigational_format? && !devise_controller? && !request.xhr? && deny_list.exclude?(request.fullpath)
  end

  def store_user_location!
    store_location_for(:user, request.fullpath)
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b