[rails] ユーザーのパスワード忘れ処理を簡単に生成してくれるプラグインforgot_password ( with Restful-authentication)

このエントリーを含むはてなブックマーク

はじめに

Railsとrestful-authenticationの組み合わせで、パスワード忘れの機能を実装するにあたって、いろいろと参考になるものを探していたところ、簡単に機能を実装できるプラグインがあったので紹介。

forgot_passwordプラグイン
http://github.com/greenisus/forgot_password/tree/master

restful-authenticationを使う事前提で、script/generate forgot_passwordというジェネレータでちゃちゃっとパスワードを忘れた人用のページを作ってくれます。実際にやってみます。

準備

まずはプロジェクトのインストールから

  1. % rails forgot_password_sample
  2. % cd forgot_password_sample/


つづいてプラグインのインストール。gitコマンドが必要です。

  1. % script/plugin install git://github.com/technoweenie/restful-authentication.git
  2. % script/plugin install git://github.com/greenisus/forgot_password.git


そして、認証とパスワード忘れのジェネレーターを実行。

  1. % script/generate authenticated --include-activation user sessions
  2. % 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に追加

  1. map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil


forgot_passwordのバグなのか、パスワード用のモデル名にpassword以外を使うと、メール送信の行が2行、それも片方は存在しないクラス名で作られています。

app/controllers/forgot_passwords_controller.rbを修正して、使わない方の行をコメントアウトしておきます。

  1. # 50行目をコメントアウト
  2. #PasswordMailer.deliver_reset_password(@user)
  3. ForgotPasswordMailer.deliver_reset_password(@user)


確認

パスワード忘れを表すテーブルが追加されました。

  1. % cat db/migrate/*_create_forgot_passwords.rb


  1. class CreateForgotPasswords <ActiveRecord::Migration
  2.   def self.up
  3.     create_table "forgot_passwords" do |t|
  4.       t.integer :user_id
  5.       t.string :reset_code
  6.       t.datetime :expiration_date
  7.  
  8.       t.timestamps
  9.     end
  10.   end
  11.  
  12.   def self.down
  13.     drop_table "forgot_passwords"
  14.   end
  15. end


パスワードの再設定を申請する名前付きルートとパスワードを再設定するルートが追加されています。

  1. % head config/routes.rb


  1. ActionController::Routing::Routes.draw do |map|
  2.   map.forgot_password '/forgot_password', :controller => 'forgot_passwords', :action => 'new'
  3.   map.change_password '/change_password/:reset_code', :controller => 'forgot_passwords', :action => 'reset'
  4.   map.resources :forgot_passwords
  5.  
  6.   map.logout '/logout', :controller => 'sessions', :action => 'destroy'
  7.   map.login '/login', :controller => 'sessions', :action => 'new'
  8.   map.register '/register', :controller => 'users', :action => 'create'
  9.   map.signup '/signup', :controller => 'users', :action => 'new'
  10.   map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil


サーバを起動

  1. % rake db:migrate
  2. % script/serve


サーバを起動して確認。
通常のrestful-autenticationのノリでユーザーを登録したあと

パスワード再設定の申請フォーム

http://localhost:3000/forgot_password
にアクセスすると、パスワードを再設定するためのメールフォームが表示されます。

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

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

最後に

毎回同じファイルを書くのはバカバカしいのでこういったプラグインを使うのもありですね。
パスワード用のテーブルを生成しなければならないのをどう捉えるかにもよりますが、restful-authenticationで作ったモデルを全くいじらなくて良いのはコードが分離して見通しが良いです。

とりあえず自分は勉強のために、これから自分なりに実装してみようと思ってますけど。

Posted in ruby, ruby on rails at 11月 18th, 2008. Trackback URI: trackback
Tags: ,

3 Responses to “[rails] ユーザーのパスワード忘れ処理を簡単に生成してくれるプラグインforgot_password ( with Restful-authentication)”

  1. 11月 18th, 2008 at 11:35 AM #komagata

    よさそうすねー!

  2. 11月 19th, 2008 at 10:51 AM #haga

    パスワード再設定はほぼ絶対使いますからねぇ。 role_requirementとかopen_id_authenticationとか、その他restful-authenticationのアドオンぽく使えるものをしばらく追ってみようかと

  3. 7月 3rd, 2009 at 5:55 AM #Restful_authenticationとforgot_passwordで楽々ユーザ認証!

    [...] 参照:http://www.func09.com/wordpress/archives/348 7 月 3rd, 2009 | [...]

Leave a Reply