Class: Cats::Core::AccessController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate, #current_user, #skip_bullet

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
# File 'app/controllers/cats/core/access_controller.rb', line 6

def 
  user = User.find_by(email: auth_params[:email])
  if user
    if user.authenticate(auth_params[:password])
      roles = user.roles.map(&:name)

      unless roles.count.positive?
        render json: {error: "User has no roles."}, status: :unprocessable_entity
        return
      end

      payload = {
        id: user.id, email: user.email, first_name: user.first_name, last_name: user.last_name, roles: roles,
        details: user.details
      }
      jwt = TokenAuthService.issue(payload)
      render json: {token: jwt, user: payload}
    else
      render json: {error: "Invalid password."}, status: 400
    end
  else
    render json: {error: "User does not exist."}, status: 400
  end
end