devise_token_auth email sending issue - ruby-on-rails

I'm using devise_token_auth in my app. When user registers, it automatically sends confirmation email.
from https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/registrations_controller.rb
# ..code
unless #resource.confirmed?
# user will require email authentication
#resource.send_confirmation_instructions({
client_config: params[:config_name],
redirect_url: #redirect_url
})
# ..code
And I get 500 error.
I, [2016-09-01T12:32:06.142856 #15153] INFO -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9] Completed 500 Internal Server Error in 31331ms (ActiveRecord: 8.4ms)
F, [2016-09-01T12:32:06.145874 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9]
F, [2016-09-01T12:32:06.146192 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9] Net::OpenTimeout (execution expired):
F, [2016-09-01T12:32:06.146249 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9]
F, [2016-09-01T12:32:06.146292 #15153] FATAL -- : [07ca58ab-1797-4edb-99bb-c7d408bfd2c9] /home/deploy/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/smtp.rb:542:in `initialize'
...
As you can see from the log it times out Net::OpenTimeout (execution expired):
But when I run User.last.send_confirmation_instructions from console it sends the email how it is suppose to be.
What am I suppose to do?
Thanks!

The problem was because of ipv6.
To fix it I had to edit /etc/gai.conf by uncommenting #precedence ::ffff:0:0/96 100 line

Related

twilio-ruby stops working after upgrading to ruby 3.1.3

I have been using the twilio api for 3 years to send messages and it worked perfectly. After upgrading from ruby 2.6 to ruby 3.1.3, Twilio is no longer working. We are using twilio-ruby 5.74.2. We are getting the error ArgumentError (wrong number of arguments (given 1, expected 0)):. I have the following code:
def send_pin_through_twilio
client = Twilio::REST::Client.new
client.messages.create({
from: ENV["twilio_phone_number"],
to: self.phone_number,
body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
})
end
We get the following error:
2023-02-03T17:11:25.312087+00:00 app[web.2]: F, [2023-02-03T17:11:25.311995 #4] FATAL -- : [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e]
2023-02-03T17:11:25.312089+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] ArgumentError (wrong number of arguments (given 1, expected 0)):
2023-02-03T17:11:25.312089+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e]
2023-02-03T17:11:25.312090+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] app/models/user.rb:157:in `send_pin_through_twilio'
2023-02-03T17:11:25.312090+00:00 app[web.2]: [0d0b1732-1dbc-4170-a3d3-ba99bb7a676e] app/controllers/api/v3/users_controller.rb:127:in `login_next'
2023-02-03T17:11:25.320370+00:00 heroku[router]: at=info method=PUT path="/api/v3/users/login_next" host=app.hiwave.co request_id=0d0b1732-1dbc-4170-a3d3-ba99bb7a676e fwd="73.179.143.247,108.162.212.48" dyno=web.2 connect=0ms service=2790ms status=500 bytes=307 protocol=https
Here is our initializer for reference:
Twilio.configure do |config|
config.account_sid = ENV["accountsid"]
config.auth_token = ENV["authtoken"]
end
How do I fix this?
Change this:
client.messages.create({
from: ENV["twilio_phone_number"],
to: self.phone_number,
body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
})
To this:
client.messages.create(
from: ENV["twilio_phone_number"],
to: self.phone_number,
body: "Hello from ...! Your one-time pin is #{self.one_time_pin}."
)
Note that the second example is not a hash, it is using keyword arguments.
This is caused by the separation of positional and keyword arguments in Ruby 3.
If you had updated to Ruby 2.7 then you would have received a warning about this rather than an error. By skipping from 2.6 to 3 you missed the warning and went straight to an error.
I'd say that's because you're using a hash as parameter while the method is now waiting for keyword arguments, try to replace the method call with this line:
client.messages.create(from: ENV["twilio_phone_number"], to: self.phone_number, body: "Hello from ...! Your one-time pin is #{self.one_time_pin}.")

nameerror unitialized constant rails

I am working on deploying a rails 5 api. After much trail error its almost functioning. The problem I am having is that I am running into a 500 server error. I sshed into the instance to check the logs and I keep getting this name error. The log output:
[2017-11-14T15:41:47.289647 #10484] INFO -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] Completed 500 Internal Server Error in 244ms (Views: 0.2ms | ActiveRecord: 3.6ms)
F, [2017-11-14T15:41:47.290161 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc]
F, [2017-11-14T15:41:47.290225 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] NameError (uninitialized constant Net::HTTP):
F, [2017-11-14T15:41:47.290245 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc]
F, [2017-11-14T15:41:47.290263 #10484] FATAL -- : [6d21f1c2-6770-42a2-93be-cd68ee0441fc] app/models/user.rb:41:in `authorization'
[6d21f1c2-6770-42a2-93be-cd68ee0441fc] app/models/user.rb:17:in `create_new_auth_token'
I thought I forgot to require net/http, but when I check the require statement was already there. I'm not understanding this at all. Further, I checked the console and its showing that the package has been included. The code:
require "uri"
require "net/http"
def authorization(client_id, token)
encrypted_token = Digest::SHA256.hexdigest(token)
params = { "token": encrypted_token, "client": client_id }
Net::HTTP.post_form(URI.parse("path to api"), params)
end
Does anyone have any insight as to what is going on?

Rails CSRF issue on receiving POST request from API

I am working on Rails application which interacts with the API. Now there is a step that the API will send the POST request to the application and I tried saving the returned data to the database. But it looks like the application does not accept the data and return 422 : Unprocessable Entity
Here is the error message from the log file
W, [2016-02-29T12:21:54.865291 #22727] WARN -- : Can't verify CSRF token authenticity
I, [2016-02-29T12:21:54.865839 #22727] INFO -- : Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
F, [2016-02-29T12:21:54.870852 #22727] FATAL -- :
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
actionpack (4.2.4) lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'
actionpack (4.2.4) lib/action_controller/metal/request_forgery_protection.rb:209:in `handle_unverified_request'
actionpack (4.2.4) lib/action_controller/metal/request_forgery_protection.rb:204:in `verify_authenticity_token'
I also tried disabling the CSRF
class MyController < ApplicationController
skip_before_action :verify_authenticity_token
end
But it still doesn't work.
Please help me get through this. I have been struggling with this issue for three days.
You can try this
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }

Object appears as existing and not at the same time

I've got some very strange issue which I cannot debug.
This is my code:
def find_order_and_payment
#payment = Spree::Payment.find_by_identifier(params['session_id'])
Rails.logger.info "payment_id #{#payment.id}"
#order = #payment.order
end
And this is the output:
I, [2014-01-17T16:39:43.084827 #12342] INFO -- : payment_id 187
I, [2014-01-17T16:39:43.090718 #12332] INFO -- : Completed 500 Internal Server Error in 448ms
NoMethodError (undefined method `id' for nil:NilClass):
app/controllers/payu_status_controller.rb:36:in `find_order_and_payment'
Line 36 is the line with Rails.logger. I don't understand, why I get correct id, but same line returns undefined method id? If I'll call above code from console everything works as expected.

NoMethodError users_url with devise (ajax)

I use devise 2.2.2 with rails 3.2.11
I use devise with ajax requests
I changed the following configuration in initializers/devise.rb
config.navigational_formats = [:json, :html]
config.http_authenticatable_on_xhr = false
when I submit an empty sign in request, I expect to get a json response with errors hash, but i get a 500 instead (see below for the trace) (it works fine with sign up request)
here are my routes (nothing special)
devise_for :users
the trace:
Started POST "/users/sign_in.json" for 127.0.0.1 at 2013-01-27 13:33:45 +0100
Processing by Devise::SessionsController#create as JSON
Parameters: {"user"=>{"email"=>"", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 1ms
Processing by Devise::SessionsController#new as JSON
Parameters: {"user"=>{"email"=>"", "password"=>"[FILTERED]"}}
Completed 500 Internal Server Error in 40ms
NoMethodError (undefined method `users_url' for #<Devise::SessionsController:0x007fe88ddd9550>):
You are probably overriding after_sign_in_path_for and have a code path in there that returns nil.
This causes devise to fall back to its default behaviour and call users_url to get the path to redirect to.
Why do I think this? Because you are having the same error I had (and lost some hair over) and also this bug report contains the github usernames of many other people who have been humbled by this particular issue.

Resources