Sha256: 27083e459f115d335fa8145aebf09600cad9e5854007d45f2109873d6a4cf1ef

Contents?: true

Size: 902 Bytes

Versions: 10

Compression:

Stored size: 902 Bytes

Contents

require 'active_support/concern'

module Wbase
  module AuthHelper
    extend ActiveSupport::Concern

    def logged_in?
      !!current_user
    end

    def subscribed?
      logged_in? && current_user && current_user.try(:subscribed?)
    end

    def current_user
      @current_user ||= User.find_by(session_token: session[:token])
    end

    def admin?
      session[:root]
    end

    def require_subscription!
      redirect_to '/subscription/new' unless subscribed?
    end

    def require_root!
      redirect_to '/' unless current_user.try(:root)
    end

    def require_login!
      redirect_to '/welcome' unless logged_in?
    end

    def login(user)
      session[:token] = user.session_token
      session[:root] = user.root?
      user.update(login_at: Time.now)
    end

    def logout
      current_user.try(:reset_session_token!)
      session[:root] = false
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
wbase-0.2.4 app/helpers/wbase/auth_helper.rb
wbase-0.2.3 app/helpers/wbase/auth_helper.rb
wbase-0.2.2 app/helpers/wbase/auth_helper.rb
wbase-0.2.1 app/helpers/wbase/auth_helper.rb
wbase-0.2.0 app/helpers/wbase/auth_helper.rb
wbase-0.1.7 app/helpers/wbase/auth_helper.rb
wbase-0.1.6 app/helpers/wbase/auth_helper.rb
wbase-0.1.5 app/helpers/wbase/auth_helper.rb
wbase-0.1.4 app/helpers/wbase/auth_helper.rb
wbase-0.1.3 app/helpers/wbase/auth_helper.rb