yposiのブログ

Web開発日記

sorceryのchange_password!の仕様が変わっているので注意

sorcery
sorcery

本番環境でパスワード変更ができなくなりました。辛い。

バージョン0.14の対応でsorceryのchange_password!の仕様が変わっておりました。

    # Clears token and tries to update the new password for the user.
    def change_password(new_password, raise_on_failure: false)
      clear_reset_password_token
      send(:"#{sorcery_config.password_attribute_name}=", new_password)
      sorcery_adapter.save raise_on_failure: raise_on_failure
    end

    def change_password!(new_password)
      change_password(new_password, raise_on_failure: true)
    end

github.com

世の中のブログで紹介されているsorceryの実装方法が上記変更を取り込んだ実装になっていないため更新時に例外が発生してします。

    if @user.change_password!(params[:user][:password])
      redirect_to(root_path, :notice => 'Password was successfully updated.')
    else
      render :action => "edit"
    end

サービス提供されている方はコードを見直すことをおすすめします。 あるいは、私の修正方法を取り込むことで改善できます。

修正前:

@user.change_password!(params[:user][:password])

修正後:

@user.change_password(params[:user][:password])

これでバージョン0.14より前の挙動と同じにすることができます。 例外を発生させなくするだけですが。。。

反省。。。

specがこの辺を拾える様になっていれば問題を起こすことなくgemのバージョンアップができたのですがそうなっていなかったので本番に影響を出してしまいました。 自戒の意を込めて「ちゃんとテストを書きましょう」と言いたいです。