设计生产登录不起作用 [英] devise production login not working

查看:123
本文介绍了设计生产登录不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在生产模式下登录。我在跟踪这个维基(操作方法: - 允许用户登录使用他们的用户名或电子邮件地址),它在开发模式下工作良好,但不在生产模式。这是我的设置。
我正在使用ruby-2.2.2,rails-4.2.4与nginx。

I cannot sign in in production mode. I was following this wiki(How-To:-Allow-users-to-sign-in-using-their-username-or-email-address) and it works well in development mode, but not in production mode. Here is my setting. I'm using ruby-2.2.2, rails-4.2.4 with nginx.

和我的应用程序控制器 p>

and my application controller

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  before_filter :configure_permitted_parameters, if: :devise_controller?
  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :login, :username, :email, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :username, :email, :password, :password_confirmation, :current_password) }
  end
  protect_from_forgery with: :exception

  skip_before_filter :verify_authenticity_token
  after_filter :store_location

  def store_location
    # store last url - this is needed for post-login redirect to whatever the user last visited.
    return unless request.get? 
    if (request.path != "/users/sign_in" &&
        request.path != "/users/sign_up" &&
        request.path != "/users/password/new" &&
        request.path != "/users/password/edit" &&
        request.path != "/users/confirmation" &&
        request.path != "/users/sign_out" &&
        !request.xhr?) # don't store ajax calls
      session[:previous_url] = request.fullpath 
    end
  end

  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path
  end

end

我的应用/ model / user.rb

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable,
         :validatable, :authentication_keys => [:login]
  devise :omniauthable, :omniauth_providers => [:facebook]

  #validates :email, uniqueness: true

  validate :validate_username

  def validate_username
    if User.where(email: username).exists?
      errors.add(:username, :invalid)
    end
  end

  def email_required?
    false
  end


  def login=(login)
    @login = login
  end

  def login
    @login || self.username || self.email
  end

  #without email 
  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions.to_h).first
    end
  end

  #facebook
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.name = auth.info.name   # assuming the user model has a name
    end
  end

  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end

end 

我的 config / initializers / devise.rb

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|
  config.secret_key = '123'


  require 'omniauth-facebook'
  config.omniauth :facebook, "my", "key", scope: 'email', info_fields: 'email'

  config.authentication_keys = [ :login ]
  config.scoped_views = true

  # config.secret_key = '123'
  config.warden do |manager|
    manager.failure_app = CustomFailure
  end

  # ==> Mailer Configuration

  require 'devise/orm/active_record'

  config.case_insensitive_keys = [:email]  
  config.strip_whitespace_keys = [:email]
  config.skip_session_storage = [:http_auth]

  config.stretches = Rails.env.test? ? 1 : 10  
  config.reconfirmable = true
  config.password_length = 8..72

end

我的应用程序首次尝试允许,但不会收到我的电子邮件,用户名参数。所以我注销后无法登录。

My app allowed on first attempt, but not taking my email, username parameters at all. So I cannot login after I logout.

这是我的日志production.log

Here is my log production.log

I, [2016-01-07T06:38:08.087402 #24876]  INFO -- : Started POST "/users" for 121.128.32.141 at 2016-01-07 06:38:08 +0000
I, [2016-01-07T06:38:08.088372 #24876]  INFO -- : Processing by Users::RegistrationsController#create as HTML
I, [2016-01-07T06:38:08.088426 #24876]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"T6X9fDB54ek7vP/TA4XiZ8Ix5tg5A6FH6e55p8XnQAYl1svp059541I+GN/dwLDomEtYD5dMvUzLoApbJ3mkdA==", "user"=>{"name"=>"myname", "username"=>"010101010", "email"=>"abdc@naver.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"signup"}
D, [2016-01-07T06:38:08.159692 #24876] DEBUG -- :   ^[[1m^[[36m (0.1ms)^[[0m  ^[[1mbegin transaction^[[0m
D, [2016-01-07T06:38:08.160770 #24876] DEBUG -- :   ^[[1m^[[35mUser Exists (0.2ms)^[[0m  SELECT  1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
D, [2016-01-07T06:38:08.161906 #24876] DEBUG -- :   ^[[1m^[[36mSQL (0.2ms)^[[0m  ^[[1mINSERT INTO "users" ("encrypted_password", "name", "created_at", "updated_at") VALUES (?, ?, ?, ?)^[[0m  [["encrypted_password", "$2a$10$RmEbaGBCvGWpGb9TOIyyt.6Y0t0EZIloOJDQM9GNxCApZuQC.kDRu"], ["name", "myname"], ["created_at", "2016-01-07 06:38:08.160981"], ["updated_at", "2016-01-07 06:38:08.160981"]]
D, [2016-01-07T06:38:08.170872 #24876] DEBUG -- :   ^[[1m^[[35m (8.6ms)^[[0m  commit transaction
D, [2016-01-07T06:38:08.171620 #24876] DEBUG -- :   ^[[1m^[[36m (0.0ms)^[[0m  ^[[1mbegin transaction^[[0m
D, [2016-01-07T06:38:08.172626 #24876] DEBUG -- :   ^[[1m^[[35mSQL (0.1ms)^[[0m  UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["last_sign_in_at", "2016-01-07 06:38:08.171208"], ["current_sign_in_at", "2016-01-07 06:38:08.171208"], ["last_sign_in_ip", "121.128.32.141"], ["current_sign_in_ip", "121.128.32.141"], ["sign_in_count", 1], ["updated_at", "2016-01-07 06:38:08.171805"], ["id", 17]]


推荐答案

实际上,这不是生产模式的问题。我发现从未经许可的参数添加 - 新田至色器件,在护栏-4-0 。在我分开我的

Actually, it's not the problem of production mode. I found solution from unpermitted-parameters-adding-new-fields-to-devise-in-rails-4-0. After I seperated my

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  before_filter :configure_permitted_parameters, if: :devise_controller?
  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :login, :username, :email, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :username, :email, :password, :password_confirmation, :current_password) }
  end

给每个控制器/ users / sessions_controller.rb像

to each controllers/users/sessions_controller.rb like

class Users::SessionsController < Devise::SessionsController
# before_filter :configure_sign_in_params, only: [:create]
  before_filter :configure_permitted_parameters
  # GET /resource/sign_in
  # def new
  #   super
  # end

  # POST /resource/sign_in
  # def create
  #   super
  # end


  # DELETE /resource/sign_out
  # def destroy
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_in_params
  #   devise_parameter_sanitizer.for(:sign_in) << :attribute
  # end

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_in).push(:name, :login, :username, :email, :password, :remember_me)
  end

end

和controllers / users / registrations_controller。 rb喜欢

and controllers/users/registrations_controller.rb like

class Users::RegistrationsController < Devise::RegistrationsController
# before_filter :configure_sign_up_params, only: [:create]
# before_filter :configure_account_update_params, only: [:update]
  before_filter :configure_permitted_parameters
  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  # def create
  #   super
  # end

  # GET /resource/edit
  def edit
     super
  end
  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up).push(:name, :username, :email, :password, :password_confirmation, :current_password)
    devise_parameter_sanitizer.for(:account_update).push(:name, :username, :email, :password, :password_confirmation, :current_password)
  end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_up_params
  #   devise_parameter_sanitizer.for(:sign_up) << :attribute
  # end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.for(:account_update) << :attribute
  # end
  protected

   def after_sign_up_path_for(resource)
     session[:previous_url] || root_path(resource)
   end


   def after_inactive_sign_up_path_for(resource)
    session[:previous_url] || root_path(resource)
   end
end

如果有人像我这样的问题,请不要使用 application_cotrollers ,而是将每个设备自己的控制器分开。

If anybody have problems like me, please not use application_cotrollers instead seperate them each devise's own controllers.

这篇关于设计生产登录不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆