Skip to content

Commit 9cc3a53

Browse files
committed
fix(env): migrate origin/email env reads to readServerEnv and extend fallback keys
1 parent c14a6a6 commit 9cc3a53

7 files changed

Lines changed: 29 additions & 11 deletions

File tree

frontend/lib/auth/admin.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'server-only';
22

33
import type { NextRequest } from 'next/server';
4-
import { readServerEnv } from '@/lib/env/server-env';
54

65
import { getCurrentUser } from '@/lib/auth';
6+
import { readServerEnv } from '@/lib/env/server-env';
77

88
export class AdminApiDisabledError extends Error {
99
code = 'ADMIN_API_DISABLED' as const;
@@ -30,10 +30,14 @@ export class AdminForbiddenError extends Error {
3030
}
3131

3232
export function assertAdminApiEnabled(): void {
33-
if (
34-
process.env.NODE_ENV === 'production' &&
35-
readServerEnv('ENABLE_ADMIN_API') !== 'true'
36-
) {
33+
const enabled =
34+
(
35+
readServerEnv('ENABLE_ADMIN_API') ??
36+
readServerEnv('NEXT_PUBLIC_ENABLE_ADMIN') ??
37+
''
38+
).toLowerCase() === 'true';
39+
40+
if (process.env.NODE_ENV === 'production' && !enabled) {
3741
throw new AdminApiDisabledError();
3842
}
3943
}

frontend/lib/email/sendPasswordResetEmail.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readServerEnv } from '@/lib/env/server-env';
2+
13
import { resetPasswordTemplate } from './templates/reset-password';
24
import { mailer } from './transporter';
35

@@ -7,7 +9,7 @@ type Params = {
79
};
810

911
export async function sendPasswordResetEmail({ to, resetUrl }: Params) {
10-
const from = process.env.EMAIL_FROM;
12+
const from = readServerEnv('EMAIL_FROM');
1113

1214
if (!from) {
1315
throw new Error('EMAIL_FROM is not configured');

frontend/lib/email/sendVerificationEmail.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readServerEnv } from '@/lib/env/server-env';
2+
13
import { verifyEmailTemplate } from './templates/verify-email';
24
import { mailer } from './transporter';
35

@@ -7,7 +9,7 @@ type Params = {
79
};
810

911
export async function sendVerificationEmail({ to, verifyUrl }: Params) {
10-
const from = process.env.EMAIL_FROM;
12+
const from = readServerEnv('EMAIL_FROM');
1113

1214
if (!from) {
1315
throw new Error('EMAIL_FROM is not configured');

frontend/lib/email/transporter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import nodemailer from 'nodemailer';
22

3-
const user = process.env.GMAIL_USER;
4-
const pass = process.env.GMAIL_APP_PASSWORD;
3+
import { readServerEnv } from '@/lib/env/server-env';
4+
5+
const user = readServerEnv('GMAIL_USER');
6+
const pass = readServerEnv('GMAIL_APP_PASSWORD');
57

68
if (!user || !pass) {
79
throw new Error('Missing Gmail SMTP credentials');

frontend/lib/env/server-env.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const GENERATED_FALLBACK_KEYS = new Set([
4444
'ENABLE_ADMIN_API',
4545
'NEXT_PUBLIC_ENABLE_ADMIN',
4646
'SHOP_STATUS_TOKEN_SECRET',
47+
'APP_ORIGIN',
48+
'APP_ADDITIONAL_ORIGINS',
49+
'GMAIL_USER',
50+
'GMAIL_APP_PASSWORD',
51+
'EMAIL_FROM',
4752
]);
4853

4954

frontend/lib/security/origin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { NextRequest, NextResponse } from 'next/server';
22

3+
import { readServerEnv } from '@/lib/env/server-env';
4+
35
const LOCALHOST_ORIGIN = 'http://localhost:3000';
46

57
function buildErrorResponse(
@@ -49,13 +51,13 @@ export function normalizeOrigin(input: string): string {
4951
export function getAllowedOrigins(): string[] {
5052
const allowed = new Set<string>();
5153

52-
const appOrigin = (process.env.APP_ORIGIN ?? '').trim();
54+
const appOrigin = (readServerEnv('APP_ORIGIN') ?? '').trim();
5355
if (appOrigin) {
5456
const normalized = normalizeOrigin(appOrigin);
5557
if (normalized) allowed.add(normalized);
5658
}
5759

58-
const additionalRaw = (process.env.APP_ADDITIONAL_ORIGINS ?? '').trim();
60+
const additionalRaw = (readServerEnv('APP_ADDITIONAL_ORIGINS') ?? '').trim();
5961
if (additionalRaw) {
6062
for (const entry of additionalRaw.split(',')) {
6163
const candidate = entry.trim();

frontend/lib/shop/status-token.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import crypto from 'node:crypto';
2+
23
import { readServerEnv } from '@/lib/env/server-env';
34

45
export const STATUS_TOKEN_SCOPES = [

0 commit comments

Comments
 (0)