Sha256: b62997fda954b2c1ae968f4313df4152cd1b8a42dc4b8e2e3a1a1eddd723d634

Contents?: true

Size: 947 Bytes

Versions: 5

Compression:

Stored size: 947 Bytes

Contents

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

      def login
        user = User.find_by(email: auth_params[:email])
        if user
          if user.authenticate(auth_params[:password])
            roles = user.user_roles.each_with_object([]) do |role, result|
              result << role.name
            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

5 entries across 5 versions & 1 rubygems

Version Path
ecom_core-1.1.0 app/controllers/ecom/core/access_controller.rb
ecom_core-1.0.25 app/controllers/ecom/core/access_controller.rb
ecom_core-1.0.24 app/controllers/ecom/core/access_controller.rb
ecom_core-1.0.23 app/controllers/ecom/core/access_controller.rb
ecom_core-1.0.22 app/controllers/ecom/core/access_controller.rb