0

私は基本的にhttps://github.com/mattconnolly/devise-custom-strategy-demo/blob/master/lib/my_authentication.rbに従っています

すべて正常に動作しますが、アプリケーションの新規ユーザーである場合 (もちろん、ユーザー情報は既に中央認証サーバーに存在します)、アプリケーションを使用するために初めて 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回ログインする必要がないように、それを変更する方法がわかりません。

4

2 に答える 2

0

私は自分でそれを理解しました。の前success! userに、次を追加します。

    if (user.new_record?)
      user.save
      puts "new user saved ******"
    end
于 2012-11-30T00:46:47.743 に答える