Skip to content

Commit 8635c51

Browse files
committed
pass mail instance around as argument
1 parent 0915fdc commit 8635c51

4 files changed

Lines changed: 31 additions & 30 deletions

File tree

api/src/controllers/user.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import Invite from '../schema/invite'
88
import Progress from '../schema/progress'
99
import { getTorrentsPage } from './torrent'
1010
import { getUserRatio } from '../utils/ratio'
11-
import { mail } from '../index'
1211
import { BYTES_GB } from '../tracker/announce'
1312

14-
export const sendVerificationEmail = async (address, token) => {
13+
export const sendVerificationEmail = async (mail, address, token) => {
1514
await mail.sendMail({
1615
from: `"${process.env.SQ_SITE_NAME}" <${process.env.SQ_MAIL_FROM_ADDRESS}>`,
1716
to: address,
@@ -22,7 +21,7 @@ ${process.env.SQ_BASE_URL}/verify-email?token=${token}`,
2221
})
2322
}
2423

25-
export const register = async (req, res) => {
24+
export const register = (mail) => async (req, res) => {
2625
if (
2726
process.env.SQ_ALLOW_REGISTER !== 'open' &&
2827
process.env.SQ_ALLOW_REGISTER !== 'invite'
@@ -131,7 +130,11 @@ export const register = async (req, res) => {
131130
},
132131
process.env.SQ_JWT_SECRET
133132
)
134-
await sendVerificationEmail(req.body.email, emailVerificationToken)
133+
await sendVerificationEmail(
134+
mail,
135+
req.body.email,
136+
emailVerificationToken
137+
)
135138

136139
if (createdUser) {
137140
if (req.body.invite) {
@@ -248,7 +251,7 @@ export const login = async (req, res) => {
248251
}
249252
}
250253

251-
export const generateInvite = async (req, res) => {
254+
export const generateInvite = (mail) => async (req, res) => {
252255
if (process.env.SQ_ALLOW_REGISTER !== 'invite') {
253256
res
254257
.status(403)
@@ -311,7 +314,7 @@ export const fetchInvites = async (req, res) => {
311314
}
312315
}
313316

314-
export const changePassword = async (req, res) => {
317+
export const changePassword = (mail) => async (req, res) => {
315318
if (req.body.password && req.body.newPassword) {
316319
try {
317320
const user = await User.findOne({ _id: req.userId }).lean()
@@ -357,7 +360,7 @@ ${process.env.SQ_BASE_URL}/reset-password/initiate`,
357360
}
358361
}
359362

360-
export const initiatePasswordReset = async (req, res) => {
363+
export const initiatePasswordReset = (mail) => async (req, res) => {
361364
if (req.body.email) {
362365
try {
363366
const user = await User.findOne({ email: req.body.email }).lean()

api/src/index.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import { downloadTorrent } from './controllers/torrent'
3434
import { rssFeed } from './controllers/rss'
3535
import createAdminUser from './setup/createAdminUser'
3636

37-
let mail
38-
3937
validateConfig(config).then(() => {
4038
if (process.env.SENTRY_DSN) {
4139
Sentry.init({
@@ -52,6 +50,16 @@ validateConfig(config).then(() => {
5250
})
5351
}
5452

53+
const mail = nodemailer.createTransport({
54+
host: process.env.SQ_SMTP_HOST,
55+
port: process.env.SQ_SMTP_PORT,
56+
secure: process.env.SQ_SMTP_SECURE,
57+
auth: {
58+
user: process.env.SQ_SMTP_USER,
59+
pass: process.env.SQ_SMTP_PASS,
60+
},
61+
})
62+
5563
const connectToDb = () => {
5664
console.log('[sq] initiating db connection...')
5765
mongoose
@@ -69,17 +77,7 @@ validateConfig(config).then(() => {
6977

7078
mongoose.connection.once('open', async () => {
7179
console.log('[sq] connected to mongodb successfully')
72-
await createAdminUser()
73-
})
74-
75-
mail = nodemailer.createTransport({
76-
host: process.env.SQ_SMTP_HOST,
77-
port: process.env.SQ_SMTP_PORT,
78-
secure: process.env.SQ_SMTP_SECURE,
79-
auth: {
80-
user: process.env.SQ_SMTP_USER,
81-
pass: process.env.SQ_SMTP_PASS,
82-
},
80+
await createAdminUser(mail)
8381
})
8482

8583
const app = express()
@@ -133,7 +131,6 @@ validateConfig(config).then(() => {
133131
http: false,
134132
udp: false,
135133
ws: false,
136-
trustProxy: true,
137134
})
138135
const onTrackerRequest = tracker._onRequest.bind(tracker)
139136
app.get('/sq/*/announce', createTrackerRoute('announce', onTrackerRequest))
@@ -148,9 +145,9 @@ validateConfig(config).then(() => {
148145
)
149146

150147
// auth routes
151-
app.post('/register', register)
148+
app.post('/register', register(mail))
152149
app.post('/login', login)
153-
app.post('/reset-password/initiate', initiatePasswordReset)
150+
app.post('/reset-password/initiate', initiatePasswordReset(mail))
154151
app.post('/reset-password/finalise', finalisePasswordReset)
155152
app.post('/verify-email', verifyUserEmail)
156153

@@ -163,7 +160,7 @@ validateConfig(config).then(() => {
163160
// everything from here on requires user auth
164161
app.use(auth)
165162

166-
app.use('/account', accountRoutes())
163+
app.use('/account', accountRoutes(mail))
167164
app.use('/user', userRoutes(tracker))
168165
app.use('/torrent', torrentRoutes(tracker))
169166
app.use('/announcements', announcementRoutes())
@@ -176,5 +173,3 @@ validateConfig(config).then(() => {
176173
console.log(`[sq] ■ sqtracker running http://localhost:${port}`)
177174
})
178175
})
179-
180-
export { mail }

api/src/routes/account.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import {
1414

1515
const router = express.Router()
1616

17-
export default () => {
17+
export default (mail) => {
1818
router.get('/invites', fetchInvites)
19-
router.post('/generate-invite', generateInvite)
20-
router.post('/change-password', changePassword)
19+
router.post('/generate-invite', generateInvite(mail))
20+
router.post('/change-password', changePassword(mail))
2121
router.get('/get-stats', getUserStats)
2222
router.get('/get-role', getUserRole)
2323
router.get('/get-verified', getUserVerifiedEmailStatus)

api/src/setup/createAdminUser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import jwt from 'jsonwebtoken'
44
import User from '../schema/user'
55
import { sendVerificationEmail } from '../controllers/user'
66

7-
const createAdminUser = async () => {
7+
const createAdminUser = async (mail) => {
88
const existingAdmin = await User.findOne({ username: 'admin' }).lean()
99
if (!existingAdmin) {
1010
const created = Date.now()
@@ -44,9 +44,12 @@ const createAdminUser = async () => {
4444
process.env.SQ_JWT_SECRET
4545
)
4646
await sendVerificationEmail(
47+
mail,
4748
process.env.SQ_ADMIN_EMAIL,
4849
emailVerificationToken
4950
)
51+
52+
console.log('[sq] created initial admin user')
5053
}
5154
}
5255

0 commit comments

Comments
 (0)