Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions apps/auth/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cors from 'cors';
import express from 'express';
import type { Request, Response, NextFunction } from 'express';
import rateLimit from 'express-rate-limit';

const port = process.env.AUTH_PORT || '8080';

Expand Down Expand Up @@ -38,8 +39,8 @@
// This endpoint accepts an email, looks up the user, then finds their reset token
app.get('/auth/test/verification-token', async (req, res) => {
try {
const identifier = String(req.query.identifier ?? '');

Check warning on line 42 in apps/auth/app.ts

View workflow job for this annotation

GitHub Actions / lint-and-typecheck

'req.query.identifier ?? ''' may use Object's default stringification format ('[object Object]') when stringified
const type = String(req.query.type ?? 'reset-password');

Check warning on line 43 in apps/auth/app.ts

View workflow job for this annotation

GitHub Actions / lint-and-typecheck

'req.query.type ?? 'reset-password'' may use Object's default stringification format ('[object Object]') when stringified
if (!identifier) {
return res.status(400).json({ error: 'identifier query parameter is required (email address)' });
}
Expand Down Expand Up @@ -111,9 +112,24 @@
);
}

// Mount the Better Auth handler for all auth routes
// app.use() will handle all methods and paths under /auth
app.use('/auth', toNodeHandler(auth));
// Disable rate limiting in test environments to avoid flaky integration tests.
// This matches the pattern used by the backend's rateLimiting middleware.
const isTestEnv =
process.env.NODE_ENV === 'test' || process.env.USE_MOCK_SERVICES === 'true' || process.env.AUTH_BYPASS === 'true';

if (isTestEnv) {
app.use('/auth', toNodeHandler(auth));
} else {
const authRateLimit = rateLimit({
windowMs: 15 * 60 * 1000,
limit: 10,
standardHeaders: 'draft-7',
legacyHeaders: false,
message: { error: 'Too many requests, please try again later.' },
});

app.use('/auth', authRateLimit, toNodeHandler(auth));
}

//endpoint for healthchecks
app.get('/healthcheck', async (req, res) => {
Expand Down Expand Up @@ -213,7 +229,7 @@
},
});

organizationId = newOrganization.id;

Check warning on line 232 in apps/auth/app.ts

View workflow job for this annotation

GitHub Actions / lint-and-typecheck

Unsafe assignment of an `any` value
}

if (!organizationId) {
Expand All @@ -224,7 +240,7 @@
model: 'member',
where: [
{ field: 'userId', value: newUser.id },
{ field: 'organizationId', value: organizationId },

Check warning on line 243 in apps/auth/app.ts

View workflow job for this annotation

GitHub Actions / lint-and-typecheck

Unsafe assignment of an `any` value
],
});

Expand All @@ -236,7 +252,7 @@
model: 'member',
data: {
userId: newUser.id,
organizationId: organizationId,

Check warning on line 255 in apps/auth/app.ts

View workflow job for this annotation

GitHub Actions / lint-and-typecheck

Unsafe assignment of an `any` value
role: 'stationManager',
createdAt: new Date(),
},
Expand Down
3 changes: 2 additions & 1 deletion apps/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"better-auth": "^1.5.6",
"cors": "^2.8.6",
"dotenv": "^17.3.1",
"express": "^5.2.1"
"express": "^5.2.1",
"express-rate-limit": "^8.2.1"
},
"peerDependencies": {
"drizzle-orm": "^0.41.0"
Expand Down
14 changes: 10 additions & 4 deletions apps/auth/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ export default defineConfig((options) => ({
target: 'node20',
clean: true,
sourcemap: true,
external: ['@wxyc/database', 'better-auth', 'drizzle-orm', 'express', 'cors', 'postgres', '@sentry/node'],
env: {
NODE_ENV: process.env.NODE_ENV || 'development',
},
external: [
'@wxyc/database',
'better-auth',
'drizzle-orm',
'express',
'express-rate-limit',
'cors',
'postgres',
'@sentry/node',
],
onSuccess: options.watch ? 'node ./dist/app.js' : undefined,
}));
1 change: 1 addition & 0 deletions dev_env/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ services:
NPM_TOKEN: ${NPM_TOKEN:-}
profiles: [ci]
environment:
- NODE_ENV=test
- DB_HOST=ci-db
- DB_PORT=5432
- DB_USERNAME=${DB_USERNAME}
Expand Down
Loading
Loading