Sha256: 189a418bec0d4acf9858c15a6a77b09bfaf32bb24384ae81bf6e22868b4b919d

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

class Kuroko2::ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  helper_method :current_user, :signed_in?
  before_action :require_sign_in

  rescue_from Http::BadRequest do
    respond_to do |format|
      format.html { render 'public/500.html', layout: false, status: :bad_request }
      format.json { render json: { message: 'Bad Request' }, status: :bad_request }
    end
  end

  def current_user
    @_current_user ||= begin
      if (id = session[:user_id])
        Kuroko2::User.active.find(id)
      end
    end
  rescue ActiveRecord::RecordNotFound
    reset_session
    redirect_to sign_in_path(return_to: url_for(params.permit!.to_h.merge(only_path: true)))
  end

  private

  def current_user=(user)
    session[:user_id] = user.id
    @_current_user    = user
  end

  def signed_in?
    current_user.present?
  end

  def require_sign_in
    unless signed_in?
      redirect_to sign_in_path(return_to: url_for(params.permit!.to_h.merge(only_path: true)))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kuroko2-0.8.0 app/controllers/kuroko2/application_controller.rb
kuroko2-0.7.0 app/controllers/kuroko2/application_controller.rb
kuroko2-0.6.0 app/controllers/kuroko2/application_controller.rb