Guest User

Untitled

a guest
Mar 4th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #Login method
  2. def login
  3. session[:user_id] = nil
  4. if request.post?
  5. user = User.authenticate(params[:email], params[:password])
  6. if user
  7. session[:user_id] = user.id
  8. User.last_login = Time.now()
  9. redirect_to(:action => "index")
  10. else
  11. flash[:notice] = "Invalid user/password combination"
  12. end
  13. end
  14. end
  15.  
  16. #which calls authenticate method in User class
  17. def self.authenticate(email, password)
  18. user = self.find_by_email(email)
  19. if user
  20. expected_password = encrypted_password(password, user.salt)
  21. if user.password != expected_password
  22. user = nil
  23. end
  24. end
  25. user
  26. end
  27.  
  28. #Which uses encrypted_password() function
  29. def self.encrypted_password(password, salt)
  30. string_to_hash = password + "junk" + salt
  31. Digest::SHA1.hexdigest(string_to_hash)
  32. end
Add Comment
Please, Sign In to add comment