Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise confirm link redirects user to heroku domain not a custom domain

I have a web app: front-end in Angular and back-end in Rails. I have a custom domain www.example.com and redirects users to heroku URL(example.herokuapp.com).

After user signs up, they need to confirm their emails. When they receive an email from my web app, they click the link. I use Devise to confirm user's email by the way. It is supposed to take the user to www.example.com/log_in. However, for some reason, the user is taken to example.herokuapp.com/log_in. How do I redirect the user to custom domain for this edge case?

like image 334
JoHksi Avatar asked Dec 18 '22 16:12

JoHksi


2 Answers

To do this you want to override the default_url_options in your environment config files. For example, in your config/environments/production.rb you'll probably want the following:

config.action_mailer.default_url_options = {
  host: 'http://www.example.com'
}
config.action_mailer.asset_host = 'http://www.example.com'

Whatever you set the host of default_url_options to will be the host of any links contained inside emails sent out through ActionMailer (this includes Devise confirmation / password-reset emails). asset_host is used as the host for any assets (eg images) you put inside of your emails, so that they can be found correctly.

like image 190
Joel G Avatar answered Jan 13 '23 13:01

Joel G


Just in case someone else was an idiot like me, I spent hours trying to figure out why my user confirmation link worked on development but not on production. The confirmation link would just redirect me to my landing page with no errors.

I have my domain hosted on Google domains with the naked domain forwarding to 'www'. In my config/environments/production.rb file I had set the host to my naked domain:

host: 'example.com'

The problem was in my google domain settings. It was redirecting the domain but NOT forwarding the path, hence the redirecting right to my landing page. Make sure you are also forwarding the path or include the www in your host setting:

host: 'www.example.com'
like image 22
lithxe Avatar answered Jan 13 '23 11:01

lithxe