refactor(env): unify required-env validation through useRuntimeConfig
validate-env.js now reads all four required vars (MONGODB_URI, JWT_SECRET, RESEND_API_KEY, HELCIM_API_TOKEN) from useRuntimeConfig() instead of mixing direct process.env reads with a JWT-only special case. Mongoose and the six Resend instantiations follow suit. Either bare or NUXT_-prefixed env names are accepted, so Dokploy no longer needs duplicate entries.
This commit is contained in:
parent
1083a1d260
commit
04eb33df6e
9 changed files with 17 additions and 24 deletions
|
|
@ -91,8 +91,7 @@ export default defineNuxtConfig({
|
||||||
},
|
},
|
||||||
runtimeConfig: {
|
runtimeConfig: {
|
||||||
// Private keys (server-side only)
|
// Private keys (server-side only)
|
||||||
mongodbUri:
|
mongodbUri: process.env.MONGODB_URI || "",
|
||||||
process.env.MONGODB_URI || "mongodb://localhost:27017/ghostguild",
|
|
||||||
jwtSecret: process.env.JWT_SECRET || "",
|
jwtSecret: process.env.JWT_SECRET || "",
|
||||||
resendApiKey: process.env.RESEND_API_KEY || "",
|
resendApiKey: process.env.RESEND_API_KEY || "",
|
||||||
helcimApiToken: process.env.HELCIM_API_TOKEN || "",
|
helcimApiToken: process.env.HELCIM_API_TOKEN || "",
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { Resend } from 'resend'
|
||||||
import Member from '../../../models/member.js'
|
import Member from '../../../models/member.js'
|
||||||
import { connectDB } from '../../../utils/mongoose.js'
|
import { connectDB } from '../../../utils/mongoose.js'
|
||||||
|
|
||||||
const resend = new Resend(process.env.RESEND_API_KEY)
|
const resend = new Resend(useRuntimeConfig().resendApiKey)
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
await requireAdmin(event)
|
await requireAdmin(event)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { Resend } from 'resend'
|
||||||
import PreRegistration from '../../../models/preRegistration.js'
|
import PreRegistration from '../../../models/preRegistration.js'
|
||||||
import { connectDB } from '../../../utils/mongoose.js'
|
import { connectDB } from '../../../utils/mongoose.js'
|
||||||
|
|
||||||
const resend = new Resend(process.env.RESEND_API_KEY)
|
const resend = new Resend(useRuntimeConfig().resendApiKey)
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
await requireAdmin(event)
|
await requireAdmin(event)
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,17 @@
|
||||||
export default defineNitroPlugin(() => {
|
export default defineNitroPlugin(() => {
|
||||||
const required = [
|
const config = useRuntimeConfig()
|
||||||
'MONGODB_URI',
|
const checks = {
|
||||||
'JWT_SECRET',
|
MONGODB_URI: config.mongodbUri,
|
||||||
'RESEND_API_KEY',
|
JWT_SECRET: config.jwtSecret,
|
||||||
'HELCIM_API_TOKEN',
|
RESEND_API_KEY: config.resendApiKey,
|
||||||
]
|
HELCIM_API_TOKEN: config.helcimApiToken,
|
||||||
|
}
|
||||||
|
|
||||||
const missing = required.filter((key) => {
|
const missing = Object.entries(checks).filter(([, value]) => !value).map(([key]) => key)
|
||||||
// Check both process.env and runtime config where applicable
|
|
||||||
if (key === 'JWT_SECRET') {
|
|
||||||
const config = useRuntimeConfig()
|
|
||||||
return !config.jwtSecret
|
|
||||||
}
|
|
||||||
return !process.env[key]
|
|
||||||
})
|
|
||||||
|
|
||||||
if (missing.length > 0) {
|
if (missing.length > 0) {
|
||||||
console.error(`FATAL: Missing required environment variables: ${missing.join(', ')}`)
|
console.error(`FATAL: Missing required environment variables: ${missing.join(', ')}`)
|
||||||
console.error('Set these in your .env file or environment variables.')
|
console.error('Set these (or their NUXT_-prefixed equivalents) in your .env file or environment variables.')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ import { Resend } from "resend";
|
||||||
import Member from "../../../models/member.js";
|
import Member from "../../../models/member.js";
|
||||||
import { connectDB } from "../../../utils/mongoose.js";
|
import { connectDB } from "../../../utils/mongoose.js";
|
||||||
|
|
||||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
const resend = new Resend(useRuntimeConfig().resendApiKey);
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
await connectDB();
|
await connectDB();
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import { randomUUID } from 'crypto'
|
||||||
import { Resend } from 'resend'
|
import { Resend } from 'resend'
|
||||||
import Member from '../models/member.js'
|
import Member from '../models/member.js'
|
||||||
|
|
||||||
const resend = new Resend(process.env.RESEND_API_KEY)
|
const resend = new Resend(useRuntimeConfig().resendApiKey)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Issue a 15-minute magic-link JWT for `email` and email it.
|
* Issue a 15-minute magic-link JWT for `email` and email it.
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ export const connectDB = async () => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MONGODB_URI = process.env.NUXT_MONGODB_URI || process.env.MONGODB_URI || 'mongodb://localhost:27017/ghostguild';
|
const MONGODB_URI = useRuntimeConfig().mongodbUri;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await mongoose.connect(MONGODB_URI, {
|
await mongoose.connect(MONGODB_URI, {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { Resend } from 'resend'
|
||||||
import Payment from '../models/payment.js'
|
import Payment from '../models/payment.js'
|
||||||
import { paymentConfirmationEmail } from '../emails/paymentConfirmation.js'
|
import { paymentConfirmationEmail } from '../emails/paymentConfirmation.js'
|
||||||
|
|
||||||
const resend = new Resend(process.env.RESEND_API_KEY)
|
const resend = new Resend(useRuntimeConfig().resendApiKey)
|
||||||
|
|
||||||
function mapStatus(helcimStatus) {
|
function mapStatus(helcimStatus) {
|
||||||
if (helcimStatus === 'paid') return 'success'
|
if (helcimStatus === 'paid') return 'success'
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { Resend } from "resend";
|
import { Resend } from "resend";
|
||||||
|
|
||||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
const resend = new Resend(useRuntimeConfig().resendApiKey);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send event registration confirmation email
|
* Send event registration confirmation email
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue