Class: Api::V1::UserTokensController

Inherits:
BaseController
  • Object
show all
Defined in:
lib/generators/happy_seed/api/templates/app/controllers/api/v1/user_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
30
31
32
33
34
35
36
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/user_tokens_controller.rb', line 4

def create
  respond_to do |format|
    user = User.where('LOWER(email) = ?', user_params[:email].try(:downcase)).first
    if user.present?
      if user.valid_password?(user_params[:password])
        if user.active_for_authentication?
          user_token = user.user_tokens.create
          if user_token.persisted?
            format.json do
              render json: { user_token: user_token_hash(user_token, user: true) }, status: :ok
            end
          else
            format.json do
              render json: { errors: user_token.errors }, status: :unprocessable_entity
            end
          end
        else
          format.json do
            render json: { errors: { user: 'is locked' } }, status: :forbidden
          end
        end
      else
        format.json do
          render json: { errors: { password: 'is invalid' } }, status: :forbidden
        end
      end
    else
      format.json do
        render json: { errors: { email: 'not found' } }, status: :not_found
      end
    end
  end
end

#destroyObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/user_tokens_controller.rb', line 38

def destroy
  respond_to do |format|
    if current_user_token.try(:persisted?)
      current_user_token.destroy
      format.json do
        render json: { user_token: user_token_hash(current_user_token, user: true) }, status: :ok
      end
    else
      format.json do
        render json: { errors: { token: 'not found' } }, status: :not_found
      end
    end
  end
end