2012年11月18日日曜日

rails認証プラグイン:Devise導入:その1.5

去年、Deviseの記事を見返していると、どうもその1と、その2の間がぽっかり開いている。migreとかどうしたんよ、と怒れる諸兄の叱咤を受け、1.5を書こうと思う。

前回rails認証プラグイン:Devise導入:その1

1.ユーザモデルを作成
$ bundle exec rails g devise (ユーザ名)

2.作成されたmigreファイルを編集
ここで悩む。1年前にdevise導入した時は、

class DeviseCreateUser < ActiveRecord::Migration
  def change
    create_table(:user) 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


devise (2.1.2)は、

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      ## Token authenticatable
      # t.string :authentication_token

お、Database authenticatableや、Trackable等、モジュールに必要な項目が、予め明示的に展開されているじゃないですか。1年前は、ぱっと見て、どんなエンティティ構成になるか、不安でたまらないもんなぁ。少なくともdevise知らない人が、保守フェーズとかで見たら混乱するだろう、危ねぇ、と思っていたのですが、見事改善。
そして、ユーザ登録時のメール認証を使用したいので、confirmableのコメントアウトを解除。

3.modelにconfirmableを追加(メール認証有効化)
class user < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

4.DBに反映する。
$ rake db:migrate

5.development.rbを編集
・以下のコメントアウトを追加

  # Don't care if the mailer can't send
  # config.action_mailer.raise_delivery_errors = false 

・SMTPサーバを、Gmailに乗る為、以下を追加する。

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => 'smtp.gmail.com',
    :port => 587,
    :authentication => :plain,
    :user_name => 'hrendoh@gmail.com',
    :password => '****'
  }
6.試してみる
ん?Logには<p>Welcome yoshidaagri@gmail.com!</p>と書いていて、Confirm my accountのURLも発行済みなのだが、メールが来ない、うむむ、これは次回に調査するとして、ひとまずConfirm my accountのURLにブラウザでアクセス。
7.その前にapplication.html.erbのbody句に以下を追加
<div id="user_nav">
  <% if user_signed_in?%>
    Signed in as <%= current_user.email %>. Not you?    <%= link_to "Sign out", destroy_user_session_path %>
  <% else %>
    <%= link_to "Sign up", new_user_registration_path %> or
    <%= link_to "Sign in", new_user_session_path %>
  <% end %>
</div>


8.アクセスしてみる。
Signed in as xxxxxxxx@gmail.com. Not you? Sign out
Your account was successfully confirmed. You are now signed in.

出ました!

0 件のコメント:

コメントを投稿