すべて正常に動作しますが、アプリケーションの新規ユーザーである場合 (もちろん、ユーザー情報は既に中央認証サーバーに存在します)、アプリケーションを使用するために初めて 2 回ログインする必要があるというバグが 1 つあります。
そして私のauthenticate!
:
def authenticate!
# mapping comes from devise base class, "mapping.to" is the class of the model
# being used for authentication, typically the class "User". This is set by using
# the `devise` class method in that model
klass = mapping.to
# login credentials
username = params[:user][:email] # The username is the email field
password = params[:user][:password]
begin
# Here is the code to authenticate
# Basically, we are sending the credentials to another central authentication server
# If the authentication fails, it will throw an exception, which will be caught below to fail!
user = klass.find_or_initialize_by_email(username)
puts "user: #{user.inspect}"
success! user
rescue Exception => e
failureMessage = "Auth error: #{e.inspect}"
puts "#{failureMessage}"
fail! failureMessage
end
# if we wanted to stop other strategies from authenticating the user
end
そして私のUser
モデルでは:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :my_authentication,
:rememberable, :trackable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :first_name, :last_name, :remember_me, :email
end
ご覧のとおりdatabase_authenticatable
、まったく使用していませんが、一部のユーザー情報を中央サーバーからアプリケーション サーバーに保存する必要があります。
私はそれが原因だと推測しています:
user = klass.find_or_initialize_by_email(username)
puts "user: #{user.inspect}"
success! user
しかし、新しいユーザーがアプリケーションを使用するために2回ログインする必要がないように、それを変更する方法がわかりません。