はじめに
Railsとrestful-authenticationの組み合わせで、パスワード忘れの機能を実装するにあたって、いろいろと参考になるものを探していたところ、簡単に機能を実装できるプラグインがあったので紹介。
forgot_passwordプラグイン
http://github.com/greenisus/forgot_password/tree/master
restful-authenticationを使う事前提で、script/generate forgot_passwordというジェネレータでちゃちゃっとパスワードを忘れた人用のページを作ってくれます。実際にやってみます。
準備
まずはプロジェクトのインストールから
- % rails forgot_password_sample
- % cd forgot_password_sample/
つづいてプラグインのインストール。gitコマンドが必要です。
- % script/plugin install git://github.com/technoweenie/restful-authentication.git
- % script/plugin install git://github.com/greenisus/forgot_password.git
そして、認証とパスワード忘れのジェネレーターを実行。
- % script/generate authenticated --include-activation user sessions
- % script/generate forgot_password forgot_password user
forgot_passwordプラグインによって、以下のファイルが追加・編集されている
- 追加ファイル
- db/migrate/xxxxxxxxxxxxxx_create_forgot_passwords.rb
- app/models/forgot_password.rb
- app/models/forgot_password_mailer.rb
- app/controllers/forgot_passwords_controller.rb
- 修正ファイル
- config/routes.rb
修正
アクティベーション用のルートを設定。config/routes.rbに追加
- map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil
forgot_passwordのバグなのか、パスワード用のモデル名にpassword以外を使うと、メール送信の行が2行、それも片方は存在しないクラス名で作られています。
app/controllers/forgot_passwords_controller.rbを修正して、使わない方の行をコメントアウトしておきます。
- # 50行目をコメントアウト
- #PasswordMailer.deliver_reset_password(@user)
- ForgotPasswordMailer.deliver_reset_password(@user)
確認
パスワード忘れを表すテーブルが追加されました。
- % cat db/migrate/*_create_forgot_passwords.rb
- class CreateForgotPasswords <ActiveRecord::Migration
- def self.up
- create_table "forgot_passwords" do |t|
- t.integer :user_id
- t.string :reset_code
- t.datetime :expiration_date
- t.timestamps
- end
- end
- def self.down
- drop_table "forgot_passwords"
- end
- end
パスワードの再設定を申請する名前付きルートとパスワードを再設定するルートが追加されています。
- % head config/routes.rb
- ActionController::Routing::Routes.draw do |map|
- map.forgot_password '/forgot_password', :controller => 'forgot_passwords', :action => 'new'
- map.change_password '/change_password/:reset_code', :controller => 'forgot_passwords', :action => 'reset'
- map.resources :forgot_passwords
- map.logout '/logout', :controller => 'sessions', :action => 'destroy'
- map.login '/login', :controller => 'sessions', :action => 'new'
- map.register '/register', :controller => 'users', :action => 'create'
- map.signup '/signup', :controller => 'users', :action => 'new'
- map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil
サーバを起動
- % rake db:migrate
- % script/serve
サーバを起動して確認。
通常のrestful-autenticationのノリでユーザーを登録したあと
パスワード再設定の申請フォーム
http://localhost:3000/forgot_password
にアクセスすると、パスワードを再設定するためのメールフォームが表示されます。

登録したメールを入力してサブミットすると、パスワードをリセットするためのURLがメールで通知されました。

メールに記載されたURLを踏むと、パスワードの再設定画面が現れます。
http://localhost:3000/change_password/:reset_code

最後に
毎回同じファイルを書くのはバカバカしいのでこういったプラグインを使うのもありですね。
パスワード用のテーブルを生成しなければならないのをどう捉えるかにもよりますが、restful-authenticationで作ったモデルを全くいじらなくて良いのはコードが分離して見通しが良いです。
とりあえず自分は勉強のために、これから自分なりに実装してみようと思ってますけど。
11月 18th, 2008 at 11:35 AM #komagata
よさそうすねー!
11月 19th, 2008 at 10:51 AM #haga
パスワード再設定はほぼ絶対使いますからねぇ。 role_requirementとかopen_id_authenticationとか、その他restful-authenticationのアドオンぽく使えるものをしばらく追ってみようかと
7月 3rd, 2009 at 5:55 AM #Restful_authenticationとforgot_passwordで楽々ユーザ認証!
[...] 参照:http://www.func09.com/wordpress/archives/348 7 月 3rd, 2009 | [...]