Sha256: 9c996442794c10bae3edb0b7169007c80dbb1c2767ca210c2381d9dfd2f1d88b

Contents?: true

Size: 1.16 KB

Versions: 37

Compression:

Stored size: 1.16 KB

Contents

module Ecom
  module Core
    class AccessController < ApplicationController
      skip_before_action :authenticate, only: [:login]

      def login
        app_code = Rails.configuration.app_code
        user = User.find_by(email: auth_params[:email])

        if user
          if user.authenticate(auth_params[:password])
            roles = user.roles_for_module(app_code).each_with_object([]) do |role, result|
              result << role.name
            end

            if roles.count <= 0
              render json: { error: 'User has no roles in this application.' }, status: :unprocessable_entity
              return
            end

            payload = { id: user.id, email: user.email, name: user.full_name, roles: roles }
            jwt = TokenAuthService.issue(payload)
            render json: { token: jwt, user: payload, error: nil }
          else
            render json: { error: 'Invalid username or password' }, status: 400
          end
        else
          render json: { error: 'User does not exist' }, status: 400
        end
      end

      private

      def auth_params
        params.require(:auth).permit(:email, :password)
      end

    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
ecom_core-1.2.9 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.7 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.6 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.5 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.4 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.3 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.2 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.1 app/controllers/ecom/core/access_controller.rb
ecom_core-1.2.0 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.28 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.27 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.26 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.25 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.24 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.23 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.22 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.21 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.20 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.19 app/controllers/ecom/core/access_controller.rb
ecom_core-1.1.18 app/controllers/ecom/core/access_controller.rb