authenticated?

Hey y'all I'm kinda new to reddit and def new to RoR. I'm reading the Beginning Rail 4 book (https://www.amazon.com/Beginning-Rails-Experts-Voice-Development/dp/1430260343) and I'm on authentication of a hashed password (pg 118) and when I try to run my authenticate method I get an error while in the console:

>> User.authenticate('[email protected]', 'secret')
=>User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1 [["email", "[email protected]"]]
NoMethodError: undefined method `authenticated?' for # Did you mean? attribute_changed?

Here's my code for my user.rb model, I had "f"ed it up before when attempting on my own, but I then went back while debugging and I've compared character by character, line by line to make sure I've got EXACTLY what the book has and I do even copied the book source code from gitHub, and I'm still getting the error, found an answer on SO exactly the same book + issue but a previous version and authentication is handled a bit differently in the previous version, what the heck am I doing wrong?

require 'digest'

class User < ActiveRecord::Base attr_accessor :password validates_uniqueness_of :email validates_length_of :email, :within => 5..50
validates_format_of :email, :with => /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i

validates_confirmation_of :password
validates_length_of :password, :within => 4..20
validates_presence_of :password, :if => :password_required?

has_one :profile
has_many :articles, -> { order('published_at DESC, title ASC') }, #ASC for ascending DESC for descending
:dependent => :nullify
has_many :replies, :through => :articles, :source => :comments

before_save :encrypt_new_password

def self.authenticate(email, password)
user = find_by_email(email)
return user if user && user.authenticated?(password)
end

def encrypt_new_password
self.hashed_password == encrypt(password)
end

protected
def encrypt_new_password
return if password.blank?
self.hashed_password = encrypt(password)
end

def password_required?
hashed_password.blank? || password.present?
end

def encrypt(string)
Digest::SHA1.hexdigest(string)
# The SHA1 hashing algorithm used in this example is weak and was only used for an example. For production
# web sites you should take a look at the bcrypt gem (https://github.com/codahale/bcrypt-ruby).
end
end

Thanks in advance.

p.s. and BTW if someone can actually explain what I did wrong or why it's not working that'd b great thank,you again.

1 thought on “authenticated?”

  1. In your authenticate function, you call authenticated?(password) but authenticated isn’t defined. You have a authenticate but not a function called authenticated. Also authenticated seems like a bad name for the function since you are validating the password matches. Maybe the function should be called passwordMatches?. When you do create the passwordMatches function, make sure you encrypt the password before comparing it to the encrypted password…otherwise you’ll have a bad time….because the version in your database will be encrypted and the one passed in will be plain text and wot match even though it should be correct.

    Reply

Leave a Comment