Skip to content

Commit 5aec2c9

Browse files
fehmerMiodec
andauthored
fix(docker): use frontend url for firebase admin (@fehmer) (#6730)
fixes #6728 --------- Co-authored-by: Jack <[email protected]>
1 parent c6dcfa1 commit 5aec2c9

File tree

7 files changed

+50
-15
lines changed

7 files changed

+50
-15
lines changed

backend/src/api/controllers/user.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { MonkeyResponse } from "../../utils/monkey-response";
88
import * as DiscordUtils from "../../utils/discord";
99
import {
1010
buildAgentLog,
11-
isDevEnvironment,
11+
getFrontendUrl,
1212
replaceObjectId,
1313
replaceObjectIds,
1414
sanitizeString,
@@ -178,11 +178,7 @@ export async function sendVerificationEmail(
178178
const { data: link, error } = await tryCatch(
179179
FirebaseAdmin()
180180
.auth()
181-
.generateEmailVerificationLink(email, {
182-
url: isDevEnvironment()
183-
? "http://localhost:3000"
184-
: "https://monkeytype.com",
185-
})
181+
.generateEmailVerificationLink(email, { url: getFrontendUrl() })
186182
);
187183

188184
if (error) {

backend/src/init/email-client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const templates: Record<EmailType, EmailMetadata> = {
2828

2929
let transportInitialized = false;
3030
let transporter: nodemailer.Transporter;
31+
let emailFrom = "Monkeytype <[email protected]>";
3132

3233
export function isInitialized(): boolean {
3334
return transportInitialized;
@@ -38,7 +39,12 @@ export async function init(): Promise<void> {
3839
return;
3940
}
4041

41-
const { EMAIL_HOST, EMAIL_USER, EMAIL_PASS, EMAIL_PORT } = process.env;
42+
const { EMAIL_HOST, EMAIL_USER, EMAIL_PASS, EMAIL_PORT, EMAIL_FROM } =
43+
process.env;
44+
45+
if (EMAIL_FROM !== undefined) {
46+
emailFrom = EMAIL_FROM;
47+
}
4248

4349
if (!(EMAIL_HOST ?? "") || !(EMAIL_USER ?? "") || !(EMAIL_PASS ?? "")) {
4450
if (isDevEnvironment()) {
@@ -102,7 +108,7 @@ export async function sendEmail(
102108
const template = await fillTemplate<typeof templateName>(templateName, data);
103109

104110
const mailOptions = {
105-
from: "Monkeytype <[email protected]>",
111+
from: emailFrom,
106112
to,
107113
subject: templates[templateName].subject,
108114
html: template,

backend/src/utils/auth.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
setTokenCacheSize,
77
} from "./prometheus";
88
import { type DecodedIdToken, UserRecord } from "firebase-admin/auth";
9-
import { isDevEnvironment } from "./misc";
9+
import { getFrontendUrl } from "./misc";
1010
import emailQueue from "../queues/email-queue";
1111
import * as UserDAL from "../dal/user";
1212
import { isFirebaseError } from "./error";
@@ -98,11 +98,7 @@ export async function sendForgotPasswordEmail(email: string): Promise<void> {
9898

9999
const link = await FirebaseAdmin()
100100
.auth()
101-
.generatePasswordResetLink(email, {
102-
url: isDevEnvironment()
103-
? "http://localhost:3000"
104-
: "https://monkeytype.com",
105-
});
101+
.generatePasswordResetLink(email, { url: getFrontendUrl() });
106102

107103
await emailQueue.sendForgotPasswordEmail(email, name, link);
108104
} catch (err) {

backend/src/utils/misc.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ export function isDevEnvironment(): boolean {
193193
return process.env["MODE"] === "dev";
194194
}
195195

196+
export function getFrontendUrl(): string {
197+
return isDevEnvironment()
198+
? "http://localhost:3000"
199+
: process.env["FRONTEND_URL"] !== undefined
200+
? process.env["FRONTEND_URL"]
201+
: "https://monkeytype.com";
202+
}
203+
196204
/**
197205
* convert database object into api object
198206
* @param data database object with `_id: ObjectId`

docker/docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ services:
3535
- REDIS_URI=redis://monkeytype-redis:6379
3636
- FRONTEND_URL=${MONKEYTYPE_FRONTENDURL}
3737
- RECAPTCHA_SECRET=${RECAPTCHA_SECRET:-}
38+
- EMAIL_HOST=${EMAIL_HOST:-}
39+
- EMAIL_PORT=${EMAIL_PORT:-}
40+
- EMAIL_USER=${EMAIL_USER:-}
41+
- EMAIL_PASS=${EMAIL_PASS:-}
42+
- EMAIL_FROM=${EMAIL_FROM:-}
3843
volumes:
3944
#uncomment to enable the account system, check the SELF_HOSTING.md file
4045
#- type: bind

docker/example.env

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ MONKEYTYPE_FRONTENDURL=http://myserver:8080
44
#url of the backend server, this must be accessible by your clients browser
55
MONKEYTYPE_BACKENDURL=http://myserver:5005
66

7-
# uncomment below config if you need user accounts
87
# firebase config
8+
# uncomment below config if you need user accounts
99
#FIREBASE_APIKEY=
1010
#FIREBASE_AUTHDOMAIN=
1111
#FIREBASE_PROJECTID=
1212
#FIREBASE_STORAGEBUCKET=
1313
#FIREBASE_MESSAGINGSENDERID=
1414
#FIREBASE_APPID=
1515

16+
17+
# email server config
18+
# uncomment below if you want to send emails for e.g. password reset
19+
#EMAIL_HOST=mail.myserver
20+
#EMAIL_USER=mailuser
21+
#EMAIL_PASS=mailpass
22+
#EMAIL_PORT=465
23+
#EMAIL_FROM="Support <noreply@myserver>"
24+
1625
# google recaptcha
1726
# uncomment below config if you need user accounts
1827
# you can use these defaults if you host this privately

docs/SELF_HOSTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Setup Firebase](#setup-firebase)
1515
- [Update backend configuration](#update-backend-configuration)
1616
- [Setup Recaptcha](#setup-recaptcha)
17+
- [Setup email optional](#setup-email-optional)
1718
- [Enable daily leaderboards](#enable-daily-leaderboards)
1819
- [Configuration files](#configuration-files)
1920
- [env file](#env-file)
@@ -126,6 +127,20 @@ RECAPTCHA_SITE_KEY=6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
126127
RECAPTCHA_SECRET=6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
127128
```
128129

130+
### Setup email (optional)
131+
132+
To enable emails for password reset and email verification update the following config in `.env` file:
133+
134+
```
135+
# email server config
136+
# uncomment below if you want to send emails for e.g. password reset
137+
EMAIL_HOST=mail.myserver # your mailserver domain
138+
EMAIL_USER=mailuser # username to authenticate with your mailserver
139+
EMAIL_PASS=mailpass # password for the user
140+
EMAIL_PORT=465 # port, likely 465 or 578
141+
EMAIL_FROM="Support <noreply@myserver>"
142+
```
143+
129144
## Enable daily leaderboards
130145

131146
To enable daily leaderboards update the `backend-configuration.json` file and add/modify

0 commit comments

Comments
 (0)