Skip to content

Commit 199aa62

Browse files
committed
implemented database connection security
1 parent 116b87f commit 199aa62

File tree

10 files changed

+120
-88
lines changed

10 files changed

+120
-88
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ yarn-error.log*
3535
# typescript
3636
*.tsbuildinfo
3737
next-env.d.ts
38+
39+
40+
# application logs (contains sensitive data)
41+
/server/logs/
42+
/logs/
43+
*.log

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ We have applied this method by examining the code after each new added functiona
124124
</ol>
125125

126126
```
127-
DATABASE_URL="mysql://username:password@localhost:3306/singitronic_nextjs"
127+
NODE_ENV=development
128+
DATABASE_URL="mysql://username:password@localhost:3306/singitronic_nextjs?sslmode=disabled"
128129
NEXTAUTH_SECRET=12D16C923BA17672F89B18C1DB22A
129130
NEXTAUTH_URL=http://localhost:3000
130131
```
131132

132133
<p>7. After you do it, you need to create another .env file in the server folder and put the same DATABASE_URL you used in the previous .env file:</p>
133134

134135
```
135-
DATABASE_URL="mysql://username:password@localhost:3306/singitronic_nextjs"
136+
NODE_ENV=development
137+
DATABASE_URL="mysql://username:password@localhost:3306/singitronic_nextjs?sslmode=disabled"
136138
```
137139

138140
<p>8. Now you need to open your terminal of choice in the root folder of the project and write:</p>

app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Metadata } from "next";
22
import { Inter } from "next/font/google";
33
import "./globals.css";
4-
import { getServerSession } from "next-auth";
4+
import { getServerSession } from "next-auth/next";
55
import 'svgmap/dist/svgMap.min.css';
66
import SessionProvider from "@/utils/SessionProvider";
77
import Header from "@/components/Header";

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
node_modules
22
# Keep environment variables out of version control
33
.env
4+
5+
# application logs (contains sensitive data)
6+
/server/logs/
7+
/logs/
8+
*.log

server/controllers/users.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const { PrismaClient } = require("@prisma/client");
2-
const prisma = new PrismaClient();
1+
const prisma = require("../utills/db"); // ✅ Fixed: removed .default
32
const bcrypt = require("bcryptjs");
43

54
// Helper function to exclude password from user object

server/logs/access.log

Lines changed: 0 additions & 79 deletions
Large diffs are not rendered by default.

server/utills/db.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { PrismaClient } = require("@prisma/client");
2+
3+
const prismaClientSingleton = () => {
4+
// Validate that DATABASE_URL is present
5+
if (!process.env.DATABASE_URL) {
6+
throw new Error('DATABASE_URL environment variable is required');
7+
}
8+
9+
// Parse DATABASE_URL to check SSL configuration
10+
const databaseUrl = process.env.DATABASE_URL;
11+
const url = new URL(databaseUrl);
12+
13+
// Log SSL configuration for debugging
14+
if (process.env.NODE_ENV === "development") {
15+
console.log(` Database connection: ${url.protocol}//${url.hostname}:${url.port || '3306'}`);
16+
console.log(`🔒 SSL Mode: ${url.searchParams.get('sslmode') || 'not specified'}`);
17+
}
18+
19+
return new PrismaClient({
20+
// Add logging for debugging
21+
log: process.env.NODE_ENV === "development"
22+
? ['query', 'info', 'warn', 'error']
23+
: ['error', 'warn'],
24+
});
25+
}
26+
27+
const globalForPrisma = globalThis;
28+
29+
const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
30+
31+
module.exports = prisma;
32+
33+
if(process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

server/utills/db.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
import { PrismaClient } from "@prisma/client";
22

33
const prismaClientSingleton = () => {
4-
return new PrismaClient();
4+
// Validate that DATABASE_URL is present
5+
if (!process.env.DATABASE_URL) {
6+
throw new Error('DATABASE_URL environment variable is required');
7+
}
8+
9+
// Parse DATABASE_URL to check SSL configuration
10+
const databaseUrl = process.env.DATABASE_URL;
11+
const url = new URL(databaseUrl);
12+
13+
// Log SSL configuration for debugging
14+
if (process.env.NODE_ENV === "development") {
15+
console.log(` Database connection: ${url.protocol}//${url.hostname}:${url.port || '3306'}`);
16+
console.log(`🔒 SSL Mode: ${url.searchParams.get('sslmode') || 'not specified'}`);
17+
}
18+
19+
return new PrismaClient({
20+
// Add logging for debugging
21+
log: process.env.NODE_ENV === "development"
22+
? ['query', 'info', 'warn', 'error']
23+
: ['error', 'warn'],
24+
});
525
}
626

727
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;
@@ -12,7 +32,6 @@ const globalForPrisma = globalThis as unknown as {
1232

1333
const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
1434

15-
1635
export default prisma;
1736

1837
if(process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

utils/db.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
import { PrismaClient } from "@prisma/client";
22

33
const prismaClientSingleton = () => {
4-
return new PrismaClient();
4+
// Validate that DATABASE_URL is present
5+
if (!process.env.DATABASE_URL) {
6+
throw new Error('DATABASE_URL environment variable is required');
7+
}
8+
9+
// Parse DATABASE_URL to check SSL configuration
10+
const databaseUrl = process.env.DATABASE_URL;
11+
const url = new URL(databaseUrl);
12+
13+
// Log SSL configuration for debugging
14+
if (process.env.NODE_ENV === "development") {
15+
console.log(` Database connection: ${url.protocol}//${url.hostname}:${url.port || '3306'}`);
16+
console.log(`🔒 SSL Mode: ${url.searchParams.get('sslmode') || 'not specified'}`);
17+
}
18+
19+
return new PrismaClient({
20+
// Add logging for debugging
21+
log: process.env.NODE_ENV === "development"
22+
? ['query', 'info', 'warn', 'error']
23+
: ['error', 'warn'],
24+
});
525
}
626

727
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;
@@ -12,7 +32,6 @@ const globalForPrisma = globalThis as unknown as {
1232

1333
const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
1434

15-
1635
export default prisma;
1736

1837
if(process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

0 commit comments

Comments
 (0)