-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07472a8
commit 015ed7d
Showing
19 changed files
with
1,549 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,26 +5,144 @@ const swaggerUi = require('swagger-ui-express'); | |
const swaggerDefinition = { | ||
openapi: '3.0.0', | ||
info: { | ||
title: 'Fusion E-Commerce Backend APIs', | ||
version: '1.0.0', | ||
description: 'API documentation for the Fusion E-Commerce backend server.', | ||
title: 'Fusion E-Commerce Backend APIs', // API title | ||
version: '1.1.0', // API version | ||
description: 'API documentation for the Fusion E-Commerce backend server. This documentation provides detailed information on all available endpoints for managing products, users, authentication, and more.', | ||
termsOfService: 'https://mern-stack-ecommerce-app-nine.vercel.app', | ||
contact: { | ||
name: 'Fusion E-Commerce Website', | ||
url: 'https://mern-stack-ecommerce-app-nine.vercel.app', | ||
email: '[email protected]', // Contact email | ||
}, | ||
license: { | ||
name: 'MIT License', | ||
url: 'https://opensource.org/licenses/MIT', // License link | ||
}, | ||
}, | ||
servers: [ | ||
{ | ||
url: 'https://mern-stack-ecommerce-app-h5wb.onrender.com', | ||
description: 'Production server', | ||
}, | ||
{ | ||
url: 'http://localhost:5000', | ||
url: 'http://localhost:8000', | ||
description: 'Development server', | ||
} | ||
], | ||
components: { | ||
securitySchemes: { | ||
BearerAuth: { | ||
type: 'http', | ||
scheme: 'bearer', | ||
bearerFormat: 'JWT', | ||
}, | ||
}, | ||
schemas: { | ||
Product: { | ||
type: 'object', | ||
required: ['name', 'price', 'description', 'category'], | ||
properties: { | ||
id: { | ||
type: 'string', | ||
description: 'Product ID', | ||
}, | ||
name: { | ||
type: 'string', | ||
description: 'Name of the product', | ||
}, | ||
description: { | ||
type: 'string', | ||
description: 'Detailed description of the product', | ||
}, | ||
price: { | ||
type: 'number', | ||
description: 'Price of the product in USD', | ||
}, | ||
category: { | ||
type: 'string', | ||
description: 'Category the product belongs to', | ||
}, | ||
brand: { | ||
type: 'string', | ||
description: 'Brand of the product', | ||
}, | ||
stock: { | ||
type: 'integer', | ||
description: 'Stock count available', | ||
}, | ||
rating: { | ||
type: 'number', | ||
description: 'Average rating of the product', | ||
}, | ||
numReviews: { | ||
type: 'integer', | ||
description: 'Number of reviews for the product', | ||
}, | ||
image: { | ||
type: 'string', | ||
description: 'URL of the product image', | ||
}, | ||
}, | ||
example: { | ||
id: '507f1f77bcf86cd799439011', | ||
name: 'Wireless Headphones', | ||
description: 'Noise-cancelling wireless headphones with long battery life.', | ||
price: 99.99, | ||
category: 'Electronics', | ||
brand: 'Fusion', | ||
stock: 150, | ||
rating: 4.7, | ||
numReviews: 89, | ||
image: 'https://example.com/product.jpg', | ||
}, | ||
}, | ||
User: { | ||
type: 'object', | ||
required: ['name', 'email', 'password'], | ||
properties: { | ||
id: { | ||
type: 'string', | ||
description: 'User ID', | ||
}, | ||
name: { | ||
type: 'string', | ||
description: 'Full name of the user', | ||
}, | ||
email: { | ||
type: 'string', | ||
description: 'Email address of the user', | ||
}, | ||
password: { | ||
type: 'string', | ||
description: 'Password for the user account', | ||
}, | ||
createdAt: { | ||
type: 'string', | ||
format: 'date-time', | ||
description: 'Account creation date', | ||
}, | ||
}, | ||
example: { | ||
id: '507f1f77bcf86cd799439011', | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: 'password123', | ||
createdAt: '2023-10-21T14:21:00Z', | ||
}, | ||
}, | ||
}, | ||
}, | ||
security: [ | ||
{ | ||
BearerAuth: [], | ||
}, | ||
], | ||
}; | ||
|
||
// Options for the swagger docs | ||
const options = { | ||
swaggerDefinition, | ||
apis: ['./routes/*.js'], | ||
apis: ['./routes/*.js'], // Specify the path to API files with JSDoc comments | ||
}; | ||
|
||
// Initialize swagger-jsdoc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const JWT_SECRET = process.env.JWT_SECRET; | ||
|
||
// Authentication middleware for protected routes | ||
module.exports = function (req, res, next) { | ||
// Get token from the header | ||
const token = req.header('x-auth-token'); | ||
|
||
// Check if there's no token | ||
if (!token) { | ||
return res.status(401).json({ msg: 'No token, authorization denied' }); | ||
} | ||
|
||
try { | ||
// Verify the token | ||
const decoded = jwt.verify(token, JWT_SECRET); | ||
|
||
// Attach the user payload to the request object | ||
req.user = decoded.user; | ||
|
||
// Call next middleware | ||
next(); | ||
} catch (err) { | ||
res.status(401).json({ msg: 'Token is not valid' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true | ||
}, | ||
password: { | ||
type: String, | ||
required: true | ||
}, | ||
date: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model('User', UserSchema); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.