I'm working on a system with a user-facing frontend and with 1-n backend services which I'm trying to design according to the principles of the Twelve-Factor App.
I'm now facing the task of sending emails to the user that contain a link to the frontend. The question is, how do I generate the URLs, or more specifically where do I take the URL domain/host from?
I'm seeing the following approaches:
1. Put the domain in the configuration
I'm putting the domain in an environment variable and expose it to the service using the framework's configuration API.
Here, I see a potential problem if in the future the application needs to be available from multiple domains (e.g. one domain per country or multi tenancy). Currently, this is not the case, but who knows what new requirements might come.
2. Extract the host from the request
When the the user enables email notifications, I extract the domain from the request headers. As the email(s) will be sent at a later time, I'll need to persist the domain in the database where the rest of the per-user settings live.
This will work in a scenario where the application has more than one domain but until this is the case, putting the same value (domain = "https://my-domain.com"
) in every database entry feels redundant.
3. Have the frontend generate the whole URL
Currently, the system only serves one domain but it already supports localization using subpaths (think /{language}/login
). To generate the correct URL for each user, I need to persist the locale that will be part of the URL in the DB anyway.
This begs the question, why not have the frontend generate the complete URL including domain, language and path and store it in the database? This would kill two birds with one stone since now the backend doesn't need to know the URL structure of the frontend. However this would potentially open up a possibility for a malicious client to mess with the generated URLs. The redundancy argument from 2. also applies.
Are there any more arguments for/against the given approaches or even alternative approaches or does it come down to taste?