Class: KnockOnce::PasswordsController

Inherits:
ApplicationController show all
Includes:
ActiveModel::SecurePassword
Defined in:
app/controllers/knock_once/passwords_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/knock_once/passwords_controller.rb', line 8

def create
  @user = User.find_by_email(params[:email])
  # if valid user
  if @user
    # generate a new token and save
    password = Password.new(@user)
    password.email_reset
    password.save_token_and_expiry

    render status: 200, json: {
      message: 'Your request has been received. If we have an email matching that account you will receive link to reset your password.'
    }
  # if invalid user
  else
    render status: 200, json: {
      message: 'Your request has been received. If we have an email matching that account you will receive link to reset your password.'
    }
  end
end

#editObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/knock_once/passwords_controller.rb', line 38

def edit
  @token = params[:token]
  @user = User.find_by_password_reset_token(@token)

  if @user && Time.now < @user.password_token_expiry
    if @user.update(password: params[:password], password_confirmation: params[:password_confirmation])
      render status: 200, json: { message: 'Your password has been updated' }
      # delete token and exiry on successful update
      @user.update(password_reset_token: nil, password_token_expiry: nil)
    else
      render status: :unprocessable_entity, json: @user.errors.full_messages
    end
  else
    render status: :expectation_failed, json: { message: 'Looks like something went wrong' }
  end
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/knock_once/passwords_controller.rb', line 55

def update
  @user = current_user
  if @user.authenticate(params[:current_password])
    if @user.update(password_params)
      render json: {
        user: @user,
        message: 'Your password has been udpated!'
      }
    else
      render json: @user.errors.full_messages, status: :unprocessable_entity
    end
  else
    render status: :unprocessable_entity, json: ['Current password is incorrect']
  end
end

#validateObject



28
29
30
31
32
33
34
35
36
# File 'app/controllers/knock_once/passwords_controller.rb', line 28

def validate
  @token = params[:token]
  @user = User.find_by_password_reset_token(@token)
  if @user && Time.now < @user.password_token_expiry
    render status: 202
  else
    render status: 404, json: { message: 'Looks like something went wrong' }
  end
end