How to reset password on rails api? [Not using devise]

Beulah Akindele

So I'm trying to right a recover password mechanism for my rails api and I've hit a wall.

After doing some research, I came up with a way to implement it. It returns the values like it work but the password does not get reset and I don't why.

My user table looks like this:

create_table "users", force: :cascade do |t|
    t.string "first_name"
    t.string "middle_name"
    t.string "last_name"
    t.string "username"
    t.string "email"
    t.string "password_digest"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
  end

And my way of resetting the password goes: From controller:

...
def reset
        token = params[:token].to_s

        if params[:email].blank?
            return render json: {error: 'Token not present.'}
        end

        user = User.find_by(reset_password_token: token)

        if user.present? && user.password_token_valid?
            if user.reset_password!(params[:password])
                render json: {status: 'ok'}, status: :ok
            else
                render json: {error: user.errors.full_messages}, status: :unprocessable_entity
            end
        else
            render json: {error: 'Link not valid or expired.'}, status: :not_found
        end
    end
...

Then model:

...

 def password_token_valid?
        (self.reset_password_sent_at + 4.hours) > Time.now.utc
    end

    def reset_password!(password)
        self.reset_password_token = nil
        self.password = password
    end
...

Is this the right way to implement this? Is this the safest way to implement this?

Note: I only tested this on production, this is due to the limitation of my development environment to send emails.

max

You're just updating the values in memory and never persisting them to the database.

def reset_password!(password)
  update!(
    password: password,
    reset_password_token: nil
  )
end

Is this the safest way to implement this?

No. Reinventing the authentication wheel is one of the most common reason apps get hacked.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Devise Rails API

분류에서Dev

Rails 4 with Clearance password reset issue

분류에서Dev

How to reset a password from kwallet?

분류에서Dev

Rails 4, Devise, Omniauth, Cancan, Twitter API

분류에서Dev

How to reset a lost root password in SmartOS

분류에서Dev

How to delete an attribute from a Devise user in Rails?

분류에서Dev

How to test Devise user was created with proper password with RSpec

분류에서Dev

devise 3.1.1 레일을 사용하여 수동으로 reset_password_instructions 보내기

분류에서Dev

허용되지 않는 매개 변수 : current_password, Rails 4 + Devise

분류에서Dev

change selected attributes for current_user using devise & rails 4

분류에서Dev

change selected attributes for current_user using devise & rails 4

분류에서Dev

How to reset Secure Function Password on a Brother MFC-9560-CDW

분류에서Dev

How do I reset the password on a Dedicated Micros DVR?

분류에서Dev

I have setup apache server, how to reset root password?

분류에서Dev

Rails 4 + Devise + Heroku

분류에서Dev

Rails : ActiveAdmin + Devise

분류에서Dev

Rails Devise 메서드 edit_password_url을 수동으로 실행하는 방법?

분류에서Dev

How to Reset video using html5

분류에서Dev

How to reset a css animation class using jquery?

분류에서Dev

how do I reset netmon using powershell?

분류에서Dev

이 Rails JSON 인증 API (Devise 사용)는 안전합니까?

분류에서Dev

Devise Session을 사용하여 Rails / Ember 앱용 Doorkeeper API 인증

분류에서Dev

How to bypass authentication_user for only 1 method ? (using devise)

분류에서Dev

How to reset the primary key id in ruby on rails 6?

분류에서Dev

/ password-reset /의 SMTPAuthenticationError

분류에서Dev

Remove or reset Windows 10 password

분류에서Dev

Reset or recover GNU screen password

분류에서Dev

Confirm Rails Devise user with checkbox

분류에서Dev

Rails 4/Devise: Attribute not saving

Related 관련 기사

  1. 1

    Devise Rails API

  2. 2

    Rails 4 with Clearance password reset issue

  3. 3

    How to reset a password from kwallet?

  4. 4

    Rails 4, Devise, Omniauth, Cancan, Twitter API

  5. 5

    How to reset a lost root password in SmartOS

  6. 6

    How to delete an attribute from a Devise user in Rails?

  7. 7

    How to test Devise user was created with proper password with RSpec

  8. 8

    devise 3.1.1 레일을 사용하여 수동으로 reset_password_instructions 보내기

  9. 9

    허용되지 않는 매개 변수 : current_password, Rails 4 + Devise

  10. 10

    change selected attributes for current_user using devise & rails 4

  11. 11

    change selected attributes for current_user using devise & rails 4

  12. 12

    How to reset Secure Function Password on a Brother MFC-9560-CDW

  13. 13

    How do I reset the password on a Dedicated Micros DVR?

  14. 14

    I have setup apache server, how to reset root password?

  15. 15

    Rails 4 + Devise + Heroku

  16. 16

    Rails : ActiveAdmin + Devise

  17. 17

    Rails Devise 메서드 edit_password_url을 수동으로 실행하는 방법?

  18. 18

    How to Reset video using html5

  19. 19

    How to reset a css animation class using jquery?

  20. 20

    how do I reset netmon using powershell?

  21. 21

    이 Rails JSON 인증 API (Devise 사용)는 안전합니까?

  22. 22

    Devise Session을 사용하여 Rails / Ember 앱용 Doorkeeper API 인증

  23. 23

    How to bypass authentication_user for only 1 method ? (using devise)

  24. 24

    How to reset the primary key id in ruby on rails 6?

  25. 25

    / password-reset /의 SMTPAuthenticationError

  26. 26

    Remove or reset Windows 10 password

  27. 27

    Reset or recover GNU screen password

  28. 28

    Confirm Rails Devise user with checkbox

  29. 29

    Rails 4/Devise: Attribute not saving

뜨겁다태그

보관