Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use 'Has_secure_password', when trying to refactor?
    primarykey
    data
    text
    <p>I am trying to refactor the railstutorial authorization mechanism.</p> <p>My version of rails is 3.2.0 and I am using ruby 1.9.3-p0 and postrgresql 9.1.</p> <p>So far, my tests are passing when it comes to a failed attempt to sigin but the successfull sign in fails.(The reason is that I have to refactor the old signin mechanism)</p> <p>Here is my session helpers sign_in function:</p> <pre><code>def sign_in(employee) cookies.permanent.signed[:remember_token] = [employee.id, employee.salt] self.current_employee = employee end. </code></pre> <p>One problem I see immediately with the sign_in function is that has_secure_password already takes care of the encryption and salt etc ... my thinking was that maybe I should use password_digest instead of employee.salt, but that failed as well.</p> <p>I would like to have my cookies expire after two hours. I found this option at api.rubyonrails.org under cookies.</p> <pre><code> cookies[:key] = { value =&gt; "employee.id, employee.salt", expires =&gt; 2.hours.from.now } </code></pre> <p>Another question I have has to do with the fact that has_secure_password already has an authenticate method so that means that I do not have to use the authenticate definition defined in the employee model,(user model) in the rails tutorial, but when I comment it out I get a flag reading:</p> <pre><code>NoMethodError: undefined method 'authenticate' </code></pre> <p>Here is my session controllers create action:</p> <pre><code>def create employee = Employee.authenticate(params[:session][:email], params[:session][:password]) if employee.nil? flash.now[:error] = "Invalid email/password combination." @title = "Sign in" render 'new' else sign_in employee redirect_back_or employee end end </code></pre> <p>It seems the Employee.authenticate is a problem.</p> <p>So I essentially have three question and they are as follows:</p> <p>In the rails tutorial we go through a fairly lengthly process of encrypting and applying salt etc to the employees password. Since has_secure_password has this already taken care of, what variable would I pass to my functions or arguments that would capture the encrypted password?</p> <p>The next question has to do with the expiration of the cookie, and how I would use that in the sign_in function?</p> <p>Lastly, how do I use the authenticate method so that rails recognizes it as a genuine method?</p> <p>Just for the record, I have searched through railsguide, api.rubyonrails.org and other questions asked on SO that are similar to this one. Of course this merely points up my lack of understanding of the principles, but I am learning and do take direction well.</p> <p>Thanks for any thoughts, suggestions and or resources you might share with me.</p> <p><em>Update</em> I re-read the api on has_secure_password and authenticate takes only one argument, namely an unencrypted password ... so I have something to work with.</p> <p>I still need any help or thoughts or suggestions that you might offer ... thanks.</p> <p><strong>update</strong> I found this article that deals with session timeouts: <a href="http://madkingsmusings.blogspot.com/2011/05/session-timeouts-on-rails.html" rel="nofollow">http://madkingsmusings.blogspot.com/2011/05/session-timeouts-on-rails.html</a> I am still working to see if I can get it to work for me, but it is tailored for the railstutorial. As for the other questions, Michael Hartl is busy pushing out the second edition of Ruby on Rails tutorial and in that edition he will be dealing with has_secure_password.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload