Ruby on rails Desive Custom Strategy要求新用户首次登录两次才能使用应用程序

Ruby on rails Desive Custom Strategy要求新用户首次登录两次才能使用应用程序,ruby-on-rails,ruby-on-rails-3,authentication,login,devise,Ruby On Rails,Ruby On Rails 3,Authentication,Login,Devise,我基本上是在听 一切正常,但有一个bug,如果我是应用程序的新用户(当然,我的用户信息已经在我们的中央身份验证服务器中),我必须第一次登录两次才能使用该应用程序 还有我的认证: def authenticate! # mapping comes from devise base class, "mapping.to" is the class of the model # being used for authentication, typically the class "User"

我基本上是在听

一切正常,但有一个bug,如果我是应用程序的新用户(当然,我的用户信息已经在我们的中央身份验证服务器中),我必须第一次登录两次才能使用该应用程序

还有我的
认证

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
在我的
用户
车型中:

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

但我不知道如何修改它,这样新用户就不必登录两次就可以使用该应用程序了。

我自己就知道了。在
成功之前!用户
,添加:

    if (user.new_record?)
      user.save
      puts "new user saved ******"
    end

我最近遇到了这个问题,通过将
find\u或\u initialize\u by\u email
替换为
find\u或\u create\u by\u email
解决了这个问题

    if (user.new_record?)
      user.save
      puts "new user saved ******"
    end