restful_authenticationの設定
なんかこれからはacts_as_authenticatedではなくて、restful_authenticationが推奨のようですね。設定で詰まったりしたので、メモ書き。
以下のページを参考にしてみました。
restful_authentication - あとで読むRailsのススメ - ZDNet Japan
restful_authentication - happy lie, happy life
インストール
script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
1.普通にログイン認証
ごく普通に新規ユーザーを作成し、ログイン認証だけを行いたい場合は以下ようにgenerateします。
script/generate authenticated user sessions rake db:migrate
app/controllers/application.rbのApplicationController内に
include AuthenticatedSystem
を追加。
login認証の必要なController内に
before_filter :login_required
を追加します。
これで基本はOK。URLは以下のようになります
ユーザー作成 | /users/new |
ログイン | /sessions/new |
ログアウト | /sessions/destroy |
このままではURLが解りにくいのでroute.rbに以下のようにマッピングします
map.signup '/signup', :controller => 'users', :action => 'new' map.login '/login', :controller => 'sessions', :action => 'new' map.logout '/logout', :controller => 'sessions', :action => 'destroy'
これで完了。
2.メールアクティベーションしたい場合
インストール後、以下のように「--include-activation」オプション付きでgenerateします。
script/generate authenticated user sessions --include-activation rake db:migrate
*注意:オプション無しでgenerateしている場合にはmigrateで作成されている00x_create_usersファイルをいったん消してから再度作成します(modelにactivation関係が追加されます)
app/controllers/application.rbのApplicationController内に
include AuthenticatedSystem
を追加します。
environment.rbの中にobserverを追加し、ActionMailerのdelivery_methodを設定します
Rails::Initializer.run do |config| ... config.active_record.observers = :user_observer ... end ActionMailer::Base.delivery_method = :sendmail
これが無いとメールが飛んで行かず、アクティベートできません。
login認証の必要なController内に
before_filter :login_required
を追加します。
1と同様にroute.rbに以下のようにマッピングします。また、アクティベーション用のマッピングも追加します。
map.signup '/signup', :controller => 'users', :action => 'new' map.login '/login', :controller => 'sessions', :action => 'new' map.logout '/logout', :controller => 'sessions', :action => 'destroy' map.connect "activate/:activation_code", :controller => "users", :action => "activate"