Skyalchemist
Skyalchemist

Reputation: 461

Validating reset password in rails

i'm trying to make sure the user's new password on reset is not equal to the current one but i'm running into a little problem here.
After the confirmation link sent to the email bla bla, i have the reset form send a post request to this method

def password_reset
        @user = UserVerificationToken.token_valid(params[:format])
        new_pass = params[:user][:password]
        if User.unique_reset_password(new_pass)
            redirect_to recover_password_path :notice => "You need to choose a different password"
        elsif @user.update_attributes(password: new_pass)
            redirect_to root_url, :notice => "Password has been reset"
        else
            redirect_to recover_password_path
        end

    end

And in the user model i have this method.

def self.unique_reset_password(new_pass)
    return true unless :password == new_pass
end

After 10 minutes of trying to figure out why this wasn't working, i slapped myself. Duh, new_pass is not hashed yet.
My question is, how do i go about this, am i on the right path? is there anyway to hash the new_pass before comparing it with the current hashed pass? Thanks.

Upvotes: 2

Views: 285

Answers (1)

Zippie
Zippie

Reputation: 6088

Try via authenticate method:

@user = UserVerificationToken.token_valid(params[:format])
new_pass = params[:user][:password]
if @user.authenticate(new_pass)
    #no good, same as old
    else
    #continue
end

That the method that is used on login (not 100% sure, but it's used on my app built via RoR Twitter tutorial)

Upvotes: 2

Related Questions