Rails: Production Authlogic Problem with find_using_perishable_token - ruby-on-rails

So I'm pretty puzzled by this one. This works in development mode and had worked in production mode before, but I just got this hoptoad error that said this:
An error has just occurred. View full details at:
http://goldhat.hoptoadapp.com/errors/2418099
Error Message:
NoMethodError: undefined method `reset_perishable_token!' for #
Where:
password_resets#create
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb, line 260
Now my password resets and user email verification doesn't work because this method (and the reset_perishable_token! method) is apparently undefined. I can't conceive of where to begin with this problem).

Might make sure you have the perishable_token in your database, if you don't I don't think authlogic defines the method.

I am facing the same issue "NoMethodError: undefined method `reset_perishable_token!'" and it take me 8 hours to figure out. I hope this can help someone in the future.
1) Make sure you have "perishable_token" column
2) Make sure you get the correct user record and call "reset_perishable_token" method
My problem is i get the wrong user record to call the "reset_perishable_token" method.
Before: user = User.where(["users.email = ?", email])
After : user = User.where(["users.email = ?", email]).first

Related

devise-two-factor how to get code valid for custom time to send over SMS/Email etc

I am trying to implement 2FA(two factor authentication) in my existing rails 4.2.10 application, I have configured many bits.
Issue I am facing is to get/retrieve a code which is valid for 5 minutes and send this code over to user on his defined phone number or email.
I did tried ROTP::TOTP.new(user.otp_secret).at(Time.now), guessing from gem's source code, which seems to work fine and give a valid otp_code in console, but in sessions_controler, as weird as it sounds, user.otp_secret is null, always...
I have posted an issue on the gem.
I don't think this can be bug, rather this is a functionality I want to build.
My stack:
Ruby: 2.4.2
Rails: 4.2.10
Devise: 4
attr_encrypted: 1.4(if it matters)
Additionally, I want to extend drift period(code acceptance time) to 5 minutes. I think that will be easy, but doing it for single code, not universally, or for all codes, this has me thinking for a while now.
My main issue is the first one, getting the code to send through SMS, this is a subproblem, which I think is doable, but if anyone has/had experience with this and can help, that will be great.
UPDATE: I updated attr_encrypted and restarted the system, it started working, also I realized there is a method current_otp in which devise_two_factor adds in the user model, so I started using that. BUT after a few minutes, it is also throwing the same issue of user.otp_secret being nil. Its getting weird...
UPDATE 2/Hacky solution: Weirdly enough, I had to add these 3 methods in user model and everything started working:
def encrypted_otp_secret
self[:encrypted_otp_secret]
end
def encrypted_otp_secret_iv
self[:encrypted_otp_secret_iv]
end
def encrypted_otp_secret_salt
self[:encrypted_otp_secret_salt]
end
As you can suspect, i got here by examining a behavior thatdoing user.encrypted_otp_secret was giving me nil while it was not, even after reloading user model. And doing user[:encrypted_otp_secret] was giving me the actual value.
It seems like a bug in attr_encrypted. I am not sure yet.
For anyone else that runs into this issue, I have found a next step needed to get the current_otp method to work. In the method pre_otp method call
> u = User.find_by(email: 'test#example.com')
> u.otp_required_for_login = true
> u.otp_secret = User.generate_otp_secret
> u.save!
and then you can call u.current_otp...
https://blog.tommyku.com/blog/integrating-two-step-two-factor-authentication-into-rails-4-project-with-devise/

NoMethodError undefined method `encoding' for nil:NilClass CGI.escape

came across this problem a little earlier today, anyone know what might be going on? I'm a rookie with api's. Tried Googling but not much luck so far..
Error:
NoMethodError in ArtistsController#index
undefined method `encoding' for nil:NilClass
the problem line is:
response = HTTParty.get("https://api.spotify.com/v1/search?q=#{CGI.escape params[:query]}&type=artist")
which makes me think it might be an issue with CGI.escape.
If I enter the following url with a query already at the end everything works fine:
http://localhost:3000/search?utf8=%E2%9C%93&query=Caribou&commit=Search
However, if enter the following url without a query I get the error mentioned above:
http://localhost:3000/search
Guess that explains the nil part, but I don't no how to get past this..
Think that's all the info I need to give for this issue but let me know if more is needed.
If params[:query] isn't defined, then CGI.escape is receiving a nil argument.
Make sure params[:query] is defined if you're referencing it, and/or don't call CGI.escape when searching by type only, and not keyword. (Assuming that's possible--that's what the empty search above seems to imply.)

Rails 4.0 User Authentication/Signup

I'm currently doing this tutorial:
http://net.tutsplus.com/tutorials/building-ribbit-in-rails/
but tried to update it to 4.0 myself since it seems a bit outdated.
https://github.com/erosenberg/myribbit
One of the things that stands out to me is that the tutorial was using 'attr_accessible' whereas I was using a private method to define the params for users to signup.
The solution doesn't have to match the tutorial (I only just got to the part with Sessions) but I would love to be able to see how to fix this specific error that isn't making sense to me and constantly sending me down a rabbit hole:
ArgumentError in Users#new. wrong number of arguments (1 for 0).
app/controllers/application_controller.rb:7:in `current_user'
Any help is greatly appreciated. Thanks!
EDIT: Here is a screenshot of my error in case nobody else is seeing it:
You have a typo in your code: https://github.com/erosenberg/myribbit/blob/master/app/controllers/application_controller.rb#L7
session [:user_id] should be session[:user_id].

Why does this unused self.hash method cause a "can't convert String into Integer" error?

I am running through the Lynda Rails 3 tutorial. At one point, in a controller called access_controller, we call a method from a model called AdminUser. The original call was:
authorized_user = AdminUser.authenticate(params[:username], params[:password])
When I run rails server, open up the browser, and access the appropriate view, I get the error: TypeError, can't convert String into Integer
This same question has been asked twice before. The first time, the asker says the problem resolved itself the next day. (I first ran into this 3 days ago, so that has not happened.) The second question has not been answered. I will try to provide much more detail:
The method in the model was:
def self.authenticate(username="", password="")
user = AdminUser.find_by_username(username)
if user && user.password_match?(password)
return user
else
return false
end
end
When I call this method from the rails console, it works totally fine. Something about calling it from a controller, or trying to get at via the browser, seems to be going wrong (I am relative beginner, so I apologize that I cannot express this thought better). I have since replicated this error with a more simple method in the same AdminUser model:
def self.nothing
true
end
This still gives me the same error. I then tried calling the self.nothing method from a different controller and action (called pages_controller#show). When I tried to open that up in the browser, I once again got the same error: "can't convert String into Integer"
I then created an identical self.nothing method in my Subject model. When I try to run that method from the show action in pages_controller, it works totally fine. No errors.
So, the same method runs totally fine in rails console, totally fine when I place it in my Subject model, but produces an error when I place it in my AdminUser model.
I then tried to comment out basically everything in sight in my AdminUser model to see if I can make the error go away. I finally was able to. The error was apparently caused by another method:
def self.hash(password="")
Digest::SHA1.hexdigest(password)
end
I was supposed to have deleted this method a few video lessons ago when we added these other methods:
def self.make_salt(username="")
Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
end
def self.hash_with_salt(password="", salt="")
Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
end
I never deleted the initial one, but for some reason, it was the one causing the error.
So, my question now is: Why did leaving in that method (which was not being used anywhere) cause this "can't convert String into Integer" error?
The reason is that User.hash overrides Object.hash that should return a Fixnum.
You should change it's name for something like User.make_hash

Rails create method not adding record to the table, but works for the IRB

I've looked everywhere for a similar error but couldn't find a solution, so in desperation I'm posting here.
My controller has this:
def add_upc
#upcs = Dvd.add_upc(params[:dogTag], params[:newUpc])
end
and in the Model we have:
def self.add_upc(dogTag, newUpc)
existingUpc = Dvd.find(dogTag).dvd_upc2title.find_by_upc(newUpc)
if existingUpc.nil?
createdUpc = Dvd.find(dogTag).dvd_upc2title.create(:upc => newUpc)
if createdUpc
upcs = createdUpc
else
upcs = 'Error: nothing was created'
end
end
end
I've set up a view page to see what's happening and I can see the object being created by createdUpc. I can also confirm that the parameters dogTag and newUpc are being passed correctly. Yet the record is not being added to the table.
Weirdly, this does work if I issue the Dvd.find(dogTag).dvd_upc2title.create(:upc => newUpc) command with the values substituted for the variables from the IRB.
Can't figure out why this is not working. I'm new to Rails so don't know what other error debugging I could use to figure out where the problem lies.
Ideas are welcome.
Thanks.
Edit:
Found the error thanks to RyanWilcox, it was the validation I had set up in the controller for UPC telling me that value already existed (even though UPCs are supposed to be unique. Is there a way to validate on a combination of 2 fields?
What I really like doing for situations like this ("why did this fail to save?") is using create! instead of create.
This will throw an exception on error, with the failed validation's text as the message of the exception. It makes problems like this obvious.

Resources