Class: Api::V1::TokensController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/tokens_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



4
5
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/api/v1/tokens_controller.rb', line 4

def create
  @user = User.find_by_email(params["email"])

  if @user.nil?
    render :json => {:success => false} and return
  end

  @user.ensure_authentication_token!

  if @user.valid_password?(params["password"])
    json_hash = {
      :token              => @user.authentication_token,
      :success            => true,
      :admin              => @user.admin?,
      :current_hours      => @user.unpaid_work_units.sum(:effective_hours),
      :pto_hours          => @user.pto_hours_left(Date.today.end_of_week),
      :offset             => @user.target_hours_offset(Date.today),
      :client             => @user.client?
    }

    json_hash[:client_id] = Client.for_user(@user).first.try(:id) if @user.client
    render :json => json_hash and return
  else
    render :json => {:success => false} and return
  end
end