Skip to content

Commit 0dc6814

Browse files
Merge pull request #1 from BackendExpert/v200
V200
2 parents 007fc83 + fc5f2dd commit 0dc6814

File tree

8 files changed

+82
-3
lines changed

8 files changed

+82
-3
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MERN-MVC-GEN
22

3-
- A helper toolkit for fast MERN MVC development. Easily generate controllers and integrate authentication functions like signup and signin.
3+
- A helper toolkit for fast MERN MVC development. Easily generate controllers and integrate authentication functions like signup and signin and also auth and Image Upload Middlewares.
44

55
## 📦 Installation
66

@@ -35,6 +35,14 @@ npm install merngen
3535
- Check Password using `bcrypt`
3636
- build-in authentication using `jwt` (`jsonwebtokens`)
3737

38+
### AuthMiddleware
39+
40+
- Check the authentication before access the route
41+
- must provide a login token for this
42+
43+
### ImageUpload Middleware
44+
45+
- helps to upload images in Project
3846

3947
## Indetails Functions
4048

@@ -51,6 +59,14 @@ npm install merngen
5159
- Returns a JWT token with user ID and role if authentication is successful
5260

5361

62+
### AuthMiddleware
63+
64+
- Must need login token to run this fucntion
65+
66+
### ImageUpload Middleware
67+
68+
- image uploading (jpeg, png, gif) only accept
69+
5470
## 🔐 Security Practices
5571

5672
- Passwords hashed using bcrypt
@@ -65,6 +81,19 @@ npm install merngen
6581
- Initial release
6682
- Develop SignUp and Sigin Functions
6783

84+
### 2.0.0 - 01 April 2025
85+
86+
- 2nd release
87+
- Develop Auth and ImageUpload Middleware
88+
89+
90+
## Common Limitations
91+
92+
- ImageUpload Middleware (Following image extension only can upload with this middelware)
93+
- - jpeg
94+
- - png
95+
- - gif
96+
6897
## 🤝 Contributing
6998

7099
- Contributions are welcome! Please feel free to open issues or submit pull requests to improve this package.
@@ -82,6 +111,10 @@ npm install merngen
82111

83112
- According to SignUp("User") mean "User" is Model Name
84113

114+
3. Auth and ImageUpload Middelware
115+
116+
<img src='https://github.com/BackendExpert/mern-mvc-gen/blob/master/imgs/imageuploadandauthmiddle.PNG'>
117+
85118
## 👨‍💻 Author
86119

87120
Name: Jehan Weerasuriya

imgs/imageuploadandauthmiddle.PNG

18.5 KB
Loading

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const { uniqueIdentifier } = require('./src/fucntion/uniqueIdentifier')
22
const { signup } = require('./src/fucntion/signup')
33
const { signin } = require('./src/fucntion/signin')
4+
const { authMiddleware } = require('./src/middleware/authMiddleware')
5+
const { imageupload } = require('./src/middleware/ImageUploadMiddleware')
46

5-
module.exports = { uniqueIdentifier, signup, signin }
7+
8+
module.exports = { uniqueIdentifier, signup, signin, authMiddleware, imageupload }

mern-mvc-gen-1.0.0.tgz

3.65 KB
Binary file not shown.

mern-mvc-gen-2.0.0.tgz

165 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mern-mvc-gen",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"main": "index.js",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import multer from 'multer';
2+
3+
const storage = multer.diskStorage({
4+
destination: (req, file, cb) => {
5+
cb(null, 'uploads/');
6+
},
7+
filename: (req, file, cb) => {
8+
cb(null, Date.now() + '-' + file.originalname);
9+
}
10+
});
11+
12+
13+
const fileFilter = (req, file, cb) => {
14+
const allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
15+
if (allowedTypes.includes(file.mimetype)) {
16+
cb(null, true);
17+
} else {
18+
cb(new Error('Invalid file type, only JPEG, PNG, and GIF are allowed'), false);
19+
}
20+
};
21+
22+
23+
const imageupload = multer({ storage, fileFilter });
24+
25+
export { imageupload }

src/middleware/authMiddleware.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import jwt from 'jsonwebtoken';
2+
3+
const authMiddleware = (req, res, next) => {
4+
const token = req.header('Authorization');
5+
if (!token) {
6+
return res.status(401).json({ message: 'Access denied. No token provided.' });
7+
}
8+
9+
try {
10+
const decoded = jwt.verify(token.replace('Bearer ', ''), process.env.JWT_SECRET);
11+
req.user = decoded;
12+
next();
13+
} catch (error) {
14+
res.status(400).json({ message: 'Invalid token.' });
15+
}
16+
};
17+
18+
export { authMiddleware }

0 commit comments

Comments
 (0)