Skip to content

Commit

Permalink
chore: Add connectDB in all controller, Add vercel.json and api folder
Browse files Browse the repository at this point in the history
  • Loading branch information
builtbysuraj committed Mar 1, 2024
1 parent 5683fd2 commit 39de5ae
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions server/.env.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MONGODB_URL=<mongodb_cloud_url>
MONGODB_URL_LOCAL=mongodb://127.0.0.1:27017
CLIENT_URL=<client_url>
JWT_SECRET=<jwt-secret>

RAZORPAY_API_KEY=<razorpay_api_key>
RAZORPAY_APT_SECRET=<razorpay_api_secret>
Expand Down
3 changes: 3 additions & 0 deletions server/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import app from '../src/app'

export default app
2 changes: 1 addition & 1 deletion server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const app = express()

app.use(
cors({
origin: [ENV.CLIENT_URL, ENV.CORS_ORIGIN],
origin: [ENV.CLIENT_URL],
credentials: true,
})
)
Expand Down
1 change: 1 addition & 0 deletions server/src/conf/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const ENV = {
MONGODB_URL: String(process.env.MONGODB_URL),
MONGODB_URL_LOCAL: String(process.env.MONGODB_URL_LOCAL),
CLIENT_URL: String(process.env.CLIENT_URL),
JWT_SECRET: String(process.env.JWT_SECRET),

RAZORPAY_API_KEY: String(process.env.RAZORPAY_API_KEY),
RAZORPAY_APT_SECRET: String(process.env.RAZORPAY_APT_SECRET),
Expand Down
2 changes: 2 additions & 0 deletions server/src/controller/payment/checkout.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { razorpayInstance } from '../..'
import connectDB from '../../db/db'

export const checkout = async (req, res) => {
await connectDB()
const options = {
amount: Number(req.body.amount * 100),
currency: 'INR',
Expand Down
3 changes: 3 additions & 0 deletions server/src/controller/payment/payment.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import crypto from 'crypto'
import { ENV } from '../../conf/conf'
import { CLIENT_BASE_URL } from '../../constants'
import connectDB from '../../db/db'
import { Payment } from '../../model/payment.model'

export const paymentVerification = async (req, res) => {
await connectDB()

const { razorpay_order_id, razorpay_payment_id, razorpay_signature } =
req.body
const userId = req.user.id
Expand Down
3 changes: 3 additions & 0 deletions server/src/controller/payment/verifyPayment.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import connectDB from '../../db/db'
import { Payment } from '../../model/payment.model'

export const verifyPayment = async (req, res) => {
await connectDB()

const paymentId = req.query.paymentId
console.log(paymentId)
try {
Expand Down
13 changes: 11 additions & 2 deletions server/src/controller/user/login.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import bcryptjs from 'bcryptjs'
import jwt from 'jsonwebtoken'
import { ENV } from '../../conf/conf'
import connectDB from '../../db/db'
import { User } from '../../model/user.model'

export const loginUser = async (req, res) => {
try {
await connectDB()

const { username, password } = req.body

const user = await User.findOne({ username })
Expand All @@ -23,11 +27,16 @@ export const loginUser = async (req, res) => {
}

// Create token with JWT
const token = await jwt.sign(tokenData, 'secret', {
const token = await jwt.sign(tokenData, ENV.JWT_SECRET, {
expiresIn: '1d',
})

res.cookie('token', token)
res.cookie('token', token, {
secure: true,
sameSite: 'none',
maxAge: 24 * 60 * 60 * 1000,
})

return res.json({
message: 'Login successful',
success: true,
Expand Down
3 changes: 2 additions & 1 deletion server/src/controller/user/logout.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const logout = async (req, res) => {
try {
res.cookie('token', '', { maxAge: 0 })
res.cookie('token', '', { secure: true, maxAge: 0, sameSite: 'none' })

return res.json({
message: 'Logout successfully',
success: true,
Expand Down
12 changes: 10 additions & 2 deletions server/src/controller/user/register.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import bcryptjs from 'bcryptjs'
import jwt from 'jsonwebtoken'
import { ENV } from '../../conf/conf'
import connectDB from '../../db/db'
import { User } from '../../model/user.model'

export const registerUser = async (req, res) => {
try {
await connectDB()
const { username, password } = req.body
// console.log(name, pass)

Expand All @@ -27,11 +30,16 @@ export const registerUser = async (req, res) => {
}

// Create token with JWT
const token = await jwt.sign(tokenData, 'secret', {
const token = await jwt.sign(tokenData, ENV.JWT_SECRET, {
expiresIn: '1d',
})

res.cookie('token', token)
res.cookie('token', token, {
secure: true,
sameSite: 'none',
maxAge: 24 * 60 * 60 * 1000,
})

return res.json({ message: 'User registered successfully', token })
} catch (error) {
console.log(error)
Expand Down
2 changes: 1 addition & 1 deletion server/src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DB_NAME } from '../constants'

const connectDB = async () => {
try {
await mongoose.connect(`${ENV.MONGODB_URL_LOCAL}/${DB_NAME}`)
await mongoose.connect(`${ENV.MONGODB_URL}/${DB_NAME}`)
console.log('MongoDB connected')
} catch (error) {
console.log('MongoDB connection FAILED ', error)
Expand Down
3 changes: 2 additions & 1 deletion server/src/middleware/verifyToken.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jwt from 'jsonwebtoken'
import { ENV } from '../conf/conf'

export const verifyToken = (req, res, next) => {
const token = req.cookies.token
Expand All @@ -8,7 +9,7 @@ export const verifyToken = (req, res, next) => {
}

try {
const verified = jwt.verify(token, 'secret')
const verified = jwt.verify(token, ENV.JWT_SECRET)
req.user = verified
next()
} catch (err) {
Expand Down
3 changes: 1 addition & 2 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"target": "ESNext",
"module": "CommonJS",
"moduleResolution": "Node",
"rootDir": "src",
"outDir": "build",
"lib": ["ESNext"],
"strict": false,
Expand All @@ -16,6 +15,6 @@
"allowSyntheticDefaultImports": true,
"isolatedModules": true
},
"include": ["src/**/*"],
"include": ["src/**/*", "api/**/*"],
"exclude": ["node_modules"]
}
3 changes: 3 additions & 0 deletions server/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rewrites": [{ "source": "/(.*)", "destination": "/api" }]
}

0 comments on commit 39de5ae

Please sign in to comment.