Sha256: 43bf1d0301e6226a2410a08cf90a4f2e1317ee3523a6c0c1a4dea5df804fd7c2

Contents?: true

Size: 914 Bytes

Versions: 3

Compression:

Stored size: 914 Bytes

Contents

require_relative "user"

module Applicaster
  module AuthHelpers
    def current_user
      return nil unless session[:omniauth_credentials]

      @current_user ||= user_from_session.tap do |user|
        session.delete(:omniauth_credentials) unless user
      end
    rescue OAuth2::Error => e
      session.delete(:omniauth_credentials)
      nil
    end

    def user_signed_in?
      !current_user.nil?
    end

    protected

    def authenticate_user!
      unless current_user
        session[:path_before_login] = url_for(params)
        redirect_to '/auth/applicaster'
      end
    end

    def user_from_session
      Applicaster::User.new(
        accounts_client.user_data_from_omniauth(session[:omniauth_credentials])
      )
    end

    def accounts_client
      Applicaster::Accounts.new(
        Settings.accounts_service.id,
        Settings.accounts_service.secret,
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
omniauth-applicaster-1.0.2 lib/applicaster/auth_helpers.rb
omniauth-applicaster-1.0.1 lib/applicaster/auth_helpers.rb
omniauth-applicaster-1.0.0 lib/applicaster/auth_helpers.rb