deviseのインストール
Gemfileに以下を追加
1 |
gem 'devise' |
続いてbundle installを実行
1 |
$ bundle install |
インストールできたか確認
1 2 3 4 |
$ bundle show ... * devise (1.5.3) ... |
インストール出来ました。続いてRailsで使用できるように設定します。
Railsでの設定
まずは以下のコマンドで必要なファイルを生成します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$ rails g devise:install create config/initializers/devise.rb create config/locales/devise.en.yml =============================================================================== Some setup you must do manually if you haven't yet: 1. Setup default url options for your specific environment. Here is an example of development environment: config.action_mailer.default_url_options = { :host => 'localhost:3000' } This is a required Rails configuration. In production it must be the actual host of your application 2. Ensure you have defined root_url to *something* in your config/routes.rb. For example: root :to => "home#index" 3. Ensure you have flash messages in app/views/layouts/application.html.erb. For example: <p class="notice">< %= notice %></p> <p class="alert">< %= alert %></p> 4. If you are deploying Rails 3.1 on Heroku, you may want to set: config.assets.initialize_on_precompile = false On config/application.rb forcing your application to not access the DB or load models when precompiling your assets. =============================================================================== |
途中の出力にあるように、以下作業を手動でやる必要があります
config/environments/development.rb の編集
config/environments/development.rbに以下を追加(本番環境の場合は、production.rbに実際のホスト名を入れる)
1 |
config.action_mailer.default_url_options = { :host => 'localhost:3000' } |
ルートパスの設定
config/routes.rbへ以下のとおりルートパスを設定する
1 2 |
root :to => "home#index" (home_controller.rbのindexアクションが ルート(/)で表示される) |
ログインエラー時の表示設定
ログイン失敗時のエラー表示のため、app/views/layouts/application.html.erbへ以下を追加します
1 2 |
<p class="notice">< %= notice %></p> <p class="alert">< %= alert %></p> |
rails3.1を使用している場合の設定
rails3.1を使用している場合はconfig/application.rbへ以下のとおり追加します。
1 |
config.assets.initialize_on_precompile = false |
認証用モデルとビューを生成する
rails generate コマンドでモデルを作成します
1 |
rails g devise MODEL名 |
MODEL名は認証に使用するモデルです。userを認証のモデルにする場合は以下のようにします
1 2 3 4 5 6 7 8 9 |
$ rails g devise user invoke active_record create db/migrate/20xxxxxxxxxxxx_devise_create_users.rb create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml insert app/models/user.rb route devise_for :users |
続いて、Devise が使用する標準のビューを生成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ rails g devise:views invoke Devise::Generators::SharedViewsGenerator create app/views/devise/shared create app/views/devise/shared/_links.erb invoke form_for create app/views/devise/confirmations create app/views/devise/confirmations/new.html.erb create app/views/devise/passwords create app/views/devise/passwords/edit.html.erb create app/views/devise/passwords/new.html.erb create app/views/devise/registrations create app/views/devise/registrations/edit.html.erb create app/views/devise/registrations/new.html.erb create app/views/devise/sessions create app/views/devise/sessions/new.html.erb create app/views/devise/unlocks create app/views/devise/unlocks/new.html.erb invoke erb create app/views/devise/mailer create app/views/devise/mailer/confirmation_instructions.html.erb create app/views/devise/mailer/reset_password_instructions.html.erb create app/views/devise/mailer/unlock_instructions.html.erb |
最後に、マイグレーションを実行して設定完了です。マイグレーションの実行前に、以下で説明しているモジュール使用の有無によって、ファイルの内容を変更する必要が出てきます。
例えば、Confirmableモジュールを使用する場合は、t.confirmableのコメントを外します
1 2 3 4 5 6 7 8 9 10 11 12 |
class DeviseCreateUsers < ActiveRecord::Migration def change create_table(:users) do |t| t.database_authenticatable :null => false t.recoverable t.rememberable t.trackable # t.encryptable t.confirmable # コメントを外す # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both # t.token_authenticatable |
マイグレーションを実行します
1 |
$ bundle exec rake db:migrate |
deviseを使用してみる
deviseのモジュールについて
deviseは以下の12のモジュールから構成されています。
- Database Authenticatable
- Token Authenticatable
- Omniauthable
- Confirmable
- Recoverable
- Registerable
- Rememberable
- Trackable
- Timeoutable
- Validatable
- Lockable
- Encryptable
app/modles/user.rbを見ると以下のモジュールがデフォルトで有効となっています
1 2 3 4 5 6 |
class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, (省略) |
認証をかけてみる
実際に認証をかけるには、該当のコントローラーに以下の一行を追加します
1 |
before_filter :authenticate_user! |
認証をかけたページへアクセスし、以下の画面が表示されれば成功です
認証後は以下のヘルパーを用いて、関連の情報を取得できます
- user_signed_in?:ログインしているかどうか
- current_user:ログイン中のユーザー情報
- user_session:ユーザーのセッション情報
コメントを残す