Restful_authenticationとforgot_passwordで楽々ユーザ認証!

7月 3rd, 2009 by nashiki コメントを書く »

Railsのプラグイン『restful_authentication』と『forgot_password』で楽々にユーザ認証部分が作れます。
restful_authenticationは、ユーザ登録やEメールを送ってEメール上のリンクをクリックしないと本登録にならないようなフローなども設定できます。
ログイン処理と、ログインされていないと表示出来ない設定もページ単位で簡単に出来ます。
ここらへん、まじめに作ると結構骨が折れますね。。。

こんな時に、『restful_authentication』と『forgot_password』を使うと汎用性の高いユーザ認証部分が手軽に作れて便利ですよ!

下記、インストールメモです。

ローカル環境上でgitコマンドの準備

私はWindows環境だったので下記URLからGitをダウンロードしてインストール
http://code.google.com/p/msysgit/downloads/list

RailsプロジェクトへPluginの導入

Railsプロジェクトのルートで、下記コマンドを実行
script/plugin install git://github.com/technoweenie/restful-authentication.git
script/plugin install git://github.com/greenisus/forgot_password.git

上記Netbeansからでは入らなかったのでコマンドで実行

ユーザ認証コードをGenerate

script/generate authenticated user sessions --include-activation
script/generate forgot_password forgot_password user

出力されるコードが一部間違っているので下記修正
# 50行目をコメントアウト

#PasswordMailer.deliver_reset_password(@user)
ForgotPasswordMailer.deliver_reset_password(@user)

routeも勝手に追加してくれます。
しかし、下記Routeは追加が必要です。

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

ApplicationControllerに下記1行を追加

include AuthenticatedSystem

DB作成

rake db:migrate

ログイン画面を開く

http://localhost:3000/loginをアクセスしたらRouteエラーが出た
ActionController::RoutingError in Sessions#new
Showing app/views/sessions/new.html.erb where line #3 raised:
session_url failed to generate from {:controller=>"session", :action=>"show"} - you may have ambiguous routes, or you may need to supply additional parameters for this route.  content_url has the following required parameters: ["session", :id] - are they all satisfied?
Extracted source (around line #3):

rake routesを実行してRoutesの設定確認を行った。
ログイン画面の、下記パスを認識してくれないので、書き換えました。

<% form_tag session_path do -%>を <% form_tag :controller => 'sessions', :action => 'create' do -%>に書き換えた
無事に会員登録(仮登録、本登録)、ログイン認証、パスワード再発行まで自動で出来ました!

これは、会員系のサービス作るのであればすごく手軽に作れます。

皆様もお試しください。

参照:http://www.func09.com/wordpress/archives/348

Advertisement

コメント・ツッコミ歓迎