Sha256: 803026b8ec6380aaff0267994b2eae7e27b2050d4ce015e239a3db7cd21498f9

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Minimalist
  module Sessions
    extend ActiveSupport::Concern

    included do
      skip_before_action :authorization_required,     only: %i(new create)
      skip_before_action :verify_authenticity_token,  only: %i(create destroy)
    end

    def new
      @user = User.new
    end

    def create
      if authenticated_user
        scrub_session!
        authenticated_user.logged_in
        session[:user_id] = authenticated_user.id
        after_authentication_success
        return
      else
        after_authentication_failure
      end
    end

    def destroy
      scrub_session!
      flash[:notice] = "You have been logged out."
      redirect_to logout_redirect_to
    end

    private

    def authenticated_user
      @authenticated_user ||= User.authenticate(user_params)
    end

    def user_params
      @user_params ||= params.require(:user).permit(:email, :username, :password)
    end

    def after_authentication_success
      redirect_back_or_default(login_redirect_to)
    end

    def after_authentication_failure
      flash.now[:alert] = "Couldn't log you in as '#{user_params[:email] || user_params[:username]}'"
      render :new
    end

    def scrub_session!
      (session.keys - %w(session_id _csrf_token return_to)).each do |key|
        session.delete(key)
      end
    end

    def login_redirect_to
      root_path
    end

    def logout_redirect_to
      new_session_path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
minimalist_authentication-1.1.1 lib/minimalist/sessions.rb
minimalist_authentication-1.1.0 lib/minimalist/sessions.rb