Class: Ecom::Core::AccessController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ecom/core/access_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_user, #logged_in?

Instance Method Details

#loginObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/ecom/core/access_controller.rb', line 6

def 
  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