codebronte
codebronte

Reputation: 15

Ruby on rails tutorial - chapter 11 - authentication working in dev but not production

I'm working through Michael Hartl's ruby on rails tutorial currently looking at email authentications (chapter 11).

I have completed everything in the chapter and everything works perfectly in the development environment but when I push to production with Heroku I get a bug

EXPECTED: after a new user submits the account creation form they should be routed back to the root_url with a flash message asking them to check their email to authenticate

ACTUAL BEHAVIOUR: after a new user submits the account creation form they seem to be routed to /users and there is an error (error reads: we're sorry but something went wrong, please check the logs).

Again this works perfectly in dev, just not in production.

WHAT I HAVE TRIED:

-I checked the logs and can see the email has been generated as expected

-I ran a console in Heroku and can see that the new user was created properly in the database and is has their authenticated field set correctly as false (Waiting for email)

-I can pull the authentication link out of the log and put it in a browser and can see that it authenticates the user properly

-I checked the user model and users controller but can't see any errors (the test suite hopefully would have caught them in dev anyway or the error would have reproduced in dev)

-I reset the heroku database and re-ran migration and db:seed to see if that would help, no change

-I did the push to Heroku again to see if something went wrong there - no change

-I tried deleting the addon in Heroku and reinstalling that, but doing that apparently got my Sendgrid account banned...so I strongly suggest you don't do this. I am now working with Sendgrid to get reactivated.

Attached is the error from the Heroku logs that I could find:

2020-04-08T16:52:25.985006+00:00 app[web.1]: ----==_mimepart_5e8e0149d677d_b2b0ebb83ed445621b-- 2020-04-08T16:52:25.985006+00:00 app[web.1]: 2020-04-08T16:52:25.985363+00:00 app[web.1]: I, [2020-04-08T16:52:25.985276 #11] INFO -- : [f4baf186-2987-4d21-bd92-c8324b3a09ca] Completed 500 Internal Server Error in 716ms (ActiveRecord: 6.2ms | Allocations: 5310) 2020-04-08T16:52:25.986654+00:00 app[web.1]: F, [2020-04-08T16:52:25.986541 #11] FATAL -- : [f4baf186-2987-4d21-bd92-c8324b3a09ca] 2020-04-08T16:52:25.986655+00:00 app[web.1]: [f4baf186-2987-4d21-bd92-c8324b3a09ca] Net::SMTPAuthenticationError (535 Authentication failed: account disabled 2020-04-08T16:52:25.986655+00:00 app[web.1]: ): 2020-04-08T16:52:25.986655+00:00 app[web.1]: [f4baf186-2987-4d21-bd92-c8324b3a09ca] 2020-04-08T16:52:25.986656+00:00 app[web.1]: [f4baf186-2987-4d21-bd92-c8324b3a09ca] app/models/user.rb:53:in send_activation_email' 2020-04-08T16:52:25.986656+00:00 app[web.1]: [f4baf186-2987-4d21-bd92-c8324b3a09ca] app/controllers/users_controller.rb:22:increate' 2020-04-08T16:52:25.997396+00:00 heroku[router]: at=info method=POST path="/users" host=afternoon-dawn-31172.herokuapp.com request_id=f4baf186-2987-4d21-bd92-c8324b3a09ca fwd="72.38.16.251" dyno=web.1 connect=0ms service=724ms status=500 bytes=1891 protocol=https

Any help is much appreciated!!!

Attached are the SMTP server settings in case they're the problem?

`

  config.action_mailer.delivery_method = :smtp
  host = 'afternoon-dawn-31172.herokuapp.com'
  config.action_mailer.default_url_options = { host: host }
  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => 'heroku.com',
    :enable_starttls_auto => true
  }  `

Upvotes: 0

Views: 293

Answers (4)

Balada Rock
Balada Rock

Reputation: 33

Just posting this in case anyone stumbles upon the same problem, in Michael's book. It seems that SENDGRID add-on is not currently supported by Heroku, or you have to set your API key and do more work than it's necessary. Managed to solve the problem, by using MailGun service, instead of SENDGRID. All you have to do is:

  1. Add MailGun add-on to your app. You can do this from your terminal, directly: $heroku addons:create mailgun:starter

  2. Configure your production.rb file. Your settings should be almost identical to the SENDGRID version from the book, just with domain name changed.

  config.action_mailer.perform_caching = false
  host = '<yourHerokuApp>.herokuapp.com'
  config.action_mailer.default_url_options = {host: "#{host}"}
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.smtp_settings = {
    :user_name => ENV['MAILGUN_SMTP_LOGIN'],
    :password => ENV['MAILGUN_SMTP_PASSWORD'],
    :domain => "<yourHerokuApp>.heroku.com",
    :address => 'smtp.mailgun.org',
    :port => 587,
    :authentication => :plain,
    :enable_starttls_auto => true
    }

That should do the trick!

Upvotes: 1

pierrecode
pierrecode

Reputation: 222

I've exactly passed through the same problem, and all of these steps right now ! In spite you've resolved it another way, here's how to fix it for other Michael Hartl's readers :

  • Sendgrid bad connection was follown by bannishment too when I tried to reinstall the addon ! I created a new Sendgrid account on sendgrid.com, and sent an SOS to the support. Within 12 hours, they fixed my access with my heroku login :)

  • Then I created an API Key on sendgrid.com, and configured heroku with it : heroku config:set SENDGRID_API_KEY=my_api_key

  • in config/environment/production.rb :

    config.action_mailer.perform_deliveries = true
    config.action_mailer.default_url_options = { host: host, protocol: 'https' }
    ActionMailer::Base.smtp_settings = {
        user_name: ENV['SENDGRID_USERNAME'],
        password: ENV['SENDGRID_PASSWORD'],
        domain: 'staticpages-pg.herokuapp.com',
        address: 'smtp.sendgrid.net',
        port: 587, 
        authentication: :plain,
        enable_starttls_auto: true
      }
  • I also added the 'sendgrid-ruby' gem.

The proces is detailed here : https://devcenter.heroku.com/articles/sendgrid#actionmailer

Upvotes: 0

codebronte
codebronte

Reputation: 15

thanks for your help.

Given the trouble I was having with SENDGRID including the fact that I managed to ban myself from SENDGRID entirely by deleting/reinstalling the Heroku addon, I decided to change the SMTP to Google.

This was easy to do and worked first time.

One note: remember to setup 2-step verification prior to setting up the SMTP and to generate an app specific password.

Thanks again to everyone who commented.

Upvotes: 0

Patryk Dąbrowski
Patryk Dąbrowski

Reputation: 1

In the logs that you have attached - you can find this fragment : Net::SMTPAuthenticationError (535 Authentication failed: account disabled 2020-04-08T16:52:25.986655+00:00 app[web.1]: )

It means that the SMTP (the service to send an email) is not authentication well. So you should check here - with the SMTP configuration. Maybe there is a rule which is setting up the configurations only for development ENV.

Upvotes: 0

Related Questions