Class: Api::V1::UsersController

Inherits:
BaseController
  • Object
show all
Defined in:
lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 19

def create
  respond_to do |format|
    user = User.new user_params
    if user.save
      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: :created
        end
      else
        format.json do
          render json: { errors: user_token.errors }, status: :unprocessable_entity
        end
      end
    else
      format.json do
        render json: { errors: user.errors }, status: :unprocessable_entity
      end
    end
  end
end

#forgot_passwordObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 41

def forgot_password
  respond_to do |format|
    user = User.find_by email: user_params[:email]
    format.json do
      if user.present?
        user.send_reset_password_instructions
        user.save
        render json: { user: user_hash(user).slice(:email) }, status: :ok
      else
        render json: { errors: { email: 'not found' } }, status: :not_found
      end
    end
  end
end

#reset_passwordObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 56

def reset_password
  respond_to do |format|
    user = User.reset_password_by_token user_params.slice(:reset_password_token, :password, :password_confirmation)
    format.json do
      if user.present?
        if user.errors.empty?
          render json: { user: user_hash(user) }, status: :ok
        else
          render json: { errors: user.errors }, status: :unprocessable_entity
        end
      else
        render json: { errors: { email: 'not found' } }, status: :not_found
      end
    end
  end
end

#showObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/users_controller.rb', line 5

def show
  respond_to do |format|
    if @user.present?
      format.json do
        render json: { user: user_hash(@user) }, status: :ok
      end
    else
      format.json do
        render json: { errors: { id: 'not found' } }, status: :not_found
      end
    end
  end
end