2017-10-17 70 views
1

我有一個新的應用程序,根用戶創建另一個用戶,當我用模型創建用戶。Devise Rails密碼

@user = User.new(:email => params[:email], :password => params[:password], :password_confirmation => params[:password_confirmation]) 
@user.save! 

的用戶節省了「正確」關於「encrypted_pa​​ssword」的價值和其他領域的表格也一樣,但是當我嘗試登錄我有一個錯誤 「無效的電子郵件或密碼」所以,當我閱讀記錄密碼是。

encrypted_password: $2a$11$wFnpiA.l9HezNXfnAGkttuu2IGIXByETytLrEkdDsa8sBFrc8Bdmq 

但我用另一個密碼的實際工作的根密碼。 但在表:

encrypted_password: $2a$11$VKOAUk5pjILU1QHYmkpJSem9KKm70QJPS7Oj.nPM/pTuyu1tqZaQO 

所以,我的色器件模型不保存密碼的正確的加密。

我的TestController:

class UsersController < ApplicationController 
    before_action :authenticate_user! 

    def index 
    @Users = User.all 
    end 

def create 
    begin 
     @user = User.new(:email => params[:email], :password => params[:password_first], :password_confirmation => params[:password_confirmation]) 
     @user.save! 
     flash[:notice] = "Usuario creado correctamente" 
     redirect_to action: 'index' 
    rescue Exception => e   
      flash[:alert] = "Error al crear al Usuario: " + e.message 
      redirect_to action: 'index' 
    end 
end 

end 

我到底做錯了什麼?

問候

+1

絕不要做'rescue Exception => e'這是一個非常糟糕的做法,它會吞噬基本的錯誤,例如'NoMethodError'和語法錯誤,並且使調試變得非常困難。使用'if @user。保存「,如果爲false則渲染新視圖,以便用戶可以更正任何驗證錯誤。不應將救援用於正常的控制流程。 – max

+0

嗨,我只是想弄清楚這一點。 我的用戶有這個通:123 和餐桌上的密碼: $ 2A $ $ 11 VKOAUk5pjILU1QHYmkpJSem9KKm70QJPS7Oj.nPM/pTuyu1tqZaQO 但是,當我在模型上創建一個新用戶的密碼更改爲: $ 2A $ $ 11 drZIcAsXTHxyvtbvL3Wg5e0EPG4JJpsAlmxe78ohEjDSA3/nCzHoa 這就是我的問題,爲什麼該模型不使用Devise的加密。 – UlyssesMarx

+0

好吧'救援例外=> e'除了清除墊子上的錯誤之外什麼都不做。 – max

回答

1

BCrypt超出它的方式來擾亂密碼儘可能使其極難扭轉哈希操作。對於任何給定的輸入字符串BCrypt::Password.create有大約6800000億萬億萬億個可能輸出字符串(2 ),所以這是不可能的,你永遠得到兩次同樣的一個:

hashes = 10.times.map { BCrypt::Password.create('test') } 
# => [ 
# "$2a$10$vMrgjJHqvwnEKIs0fZ76pO3gbWL/0C3ExqK9HOpi/mHYu2.4GAO2K", 
# "$2a$10$KxBOarDzRPHp7QF1GGqNnuplRs1B5rNVfp21IHx1/HzQ0YIcIkLRW", 
# "$2a$10$emCdZAA.GU8GwQZkeJLfAuUTY2aEnhFmZ.GQAhDpJ.JGSh/m6s/k2", 
# "$2a$10$6R6xmGyK7Tb1MKsQb00vpOJKwpi56aj98JLoBJhBN4vWSQb7zagQm", 
# "$2a$10$r4qmb.C.vm88pL2nJK5TdOaWIboYaO6a1xHIRH.QDER6qYR6Ajvo.", 
# "$2a$10$mlVWz4IHTgYHSf3tAgEgpenpDHtGWYev4EUENLs7hnLlm6ikPhUxy", 
# "$2a$10$ixXdZZuc9rIVAozO8tyq5.wlsVOWBc6QWetNh3PvjPj2pGlqh.XOy", 
# "$2a$10$zLzuevtOl.g4RbaHpdeTZ.k4qjE/1m4nh6gN4mhcIKQPSa5sBcG5u", 
# "$2a$10$F/F71.DYEuzxS4W0w5m/a.IRpaVJxeh9sKUJ7DyQb5xU3SvFu1Ib.", 
# "$2a$10$ILXg8R52ZtHHbQbT0FxSFOj8YNqpNLmrH.6FhM3RGMwIuBeP1YXHa" ] 

這是正常的,雖然因爲驗證例程可以處理檢查密碼:

hashes.map { |h| BCrypt::Password.new(h) == 'test' } 
# => [true, true, true, true, true, true, true, true, true, true] 

它們全都匹配。重要的是要注意,驗證十個密碼確實需要很短的時間。這使得BCrypt特別適合用於密碼存儲:猜測是昂貴的,因此將巨大的詞典放在哈希中以查看哪個匹配非常困難。對於像MD5或SHA1這樣較弱的哈希來說,情況並非如此,其中每秒100萬次操作完全可行。 BCrypt故意大約慢50萬倍。

0

從定期體面的Rails crud設置開始,看看問題是否解決不了。

# routes.rb 
resources :users, only: [:create, :index] 

class UsersController < ApplicationController 

    before_action :authenticate_user! 

    # GET /users 
    def index 
    @Users = User.all 
    end 

    # POST /users 
    def create 
    @user = User.new(user_params) 
    if @user.save 
     flash[:success] = "Usuario creado correctamente" 
     redirect_to action: :index 
    else 
     render :new 
    end 
    end 

    def user_params 
    params.require(:user) 
      .permit(:email, :password, :password_confirmation) 
    end 
end 

# app/views/users/_form.html.erb 
<% form_for(@user || User.new) do |f| %> 
    <% if f.object.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(f.object.errors.count, "error") %> prohibited this article from being saved:</h2> 
    <ul> 
     <% f.object.errors.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
    </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :email %> 
    <%= f.email_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation %> 
    <%= f.password_field :password_confirmation %> 
    </div> 
</div> 
<% end %> 

# app/views/users/new.html.erb 
<%= render partial: 'form' %> 

您可以嵌入在你的根視圖中的部分或由用戶索引:

<%= render partial: 'users/form' %> 

請注意,如果記錄無效,您應該呈現並不重定向。如果您重定向用戶輸入,並且您想顯示的任何驗證消息都消失了。