merb-auth-slice-activation
は、merbでemailアクティベーションを簡単に実装するための非常に便利なSliceです。
このSliceは、merb-auth-coreが提供するhookである、after_authenticationをフックする事によって、アクティベーションが済んでいないアカウントでのログインをブロックする仕様になっています。該当するコードは以下の通り。
merb-auth-slice-activation/lib/merb-auth-slice-activation.rb
ruby>>
# Initialization hook - runs before AfterAppLoads BootLoader
def self.init
# Actually check if the user is active
::Merb::Authentication.after_authentication do |user, *rest|
if user.respond_to?(:active?)
user.active? ? user : nil
else
user
end
end
end
<<--
ただ、この実装では、ログインに失敗した場合に何のエラーメッセージも表示されないため、以下のようにExceptionsコントローラにコードを加え、エラーメッセージを表示するようにしてみます。
ruby>>
class Exceptions < Merb::Controller
(snip)
def unauthenticated
request.exceptions.each do |e|
session.authentication.errors.add(:general, e.message)
end if request.exceptions
render :format => :html
end
end
<<--
Merbでは、アクションの実行中に例外が発生した場合、Exceptionsコントローラに処理が回ってきます。その場合、発生した例外オブジェクトは、request.exceptionsで参照出来ます。
あとは、view側で
rails>>
<%= error_messages_for session.authentication %>
<<--
を記述すればOK.