Compare commits

..

No commits in common. "9dab2c0ae51d94c2994a6bd6b4f1fd97f6cc0270" and "0b6340dca460cf91a1d03a9a69cf7c5d487c9af6" have entirely different histories.

6 changed files with 21 additions and 32 deletions

2
app.js
View File

@ -53,7 +53,7 @@ startServer()
// app.use('/', loginUtils.aopMiddleware, indexRouter); // app.use('/', loginUtils.aopMiddleware, indexRouter);
app.use('/', indexRouter) app.use('/', indexRouter)
app.use('/user', loginUtil.authenticateSession, userRouter) app.use('/user',loginUtil.authenticateSeesion, userRouter)
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use(function (req, res, next) { app.use(function (req, res, next) {

View File

@ -1,4 +1,3 @@
const { body, validationResult } = require('express-validator')
const userService = require('../services/userService') const userService = require('../services/userService')
exports.getAllUsers = async (req, res) => { exports.getAllUsers = async (req, res) => {
@ -10,19 +9,11 @@ exports.getAllUsers = async (req, res) => {
} }
} }
exports.createUser = [ exports.createUser = async (req, res) => {
body('account').isLength({ min: 3 }), try {
body('password').isLength({ min: 6 }), const user = await userService.create_user(req.body)
async (req, res, next) => { res.json(user)
const errors = validationResult(req) } catch (err) {
if (!errors.isEmpty()) { res.status(500).json({ error: err.message })
return res.status(400).json({ errors: errors.array() })
}
try {
await userService.create_user(req.body)
res.status(201).json({ message: 'User created successfully' })
} catch (err) {
res.status(500).json({ error: err.message })
}
} }
] }

11
package-lock.json generated
View File

@ -16,7 +16,7 @@
"express": "^4.19.2", "express": "^4.19.2",
"express-async-handler": "^1.2.0", "express-async-handler": "^1.2.0",
"express-session": "^1.18.0", "express-session": "^1.18.0",
"express-validator": "^7.2.0", "express-validator": "^7.0.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jade": "^0.29.0", "jade": "^0.29.0",
"mongoose": "^8.3.4", "mongoose": "^8.3.4",
@ -767,13 +767,12 @@
] ]
}, },
"node_modules/express-validator": { "node_modules/express-validator": {
"version": "7.2.0", "version": "7.0.1",
"resolved": "https://registry.npmmirror.com/express-validator/-/express-validator-7.2.0.tgz", "resolved": "https://registry.npmjs.org/express-validator/-/express-validator-7.0.1.tgz",
"integrity": "sha512-I2ByKD8panjtr8Y05l21Wph9xk7kk64UMyvJCl/fFM/3CTJq8isXYPLeKW/aZBCdb/LYNv63PwhY8khw8VWocA==", "integrity": "sha512-oB+z9QOzQIE8FnlINqyIFA8eIckahC6qc8KtqLdLJcU3/phVyuhXH3bA4qzcrhme+1RYaCSwrq+TlZ/kAKIARA==",
"license": "MIT",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"validator": "~13.12.0" "validator": "^13.9.0"
}, },
"engines": { "engines": {
"node": ">= 8.0.0" "node": ">= 8.0.0"

View File

@ -15,7 +15,7 @@
"express": "^4.19.2", "express": "^4.19.2",
"express-async-handler": "^1.2.0", "express-async-handler": "^1.2.0",
"express-session": "^1.18.0", "express-session": "^1.18.0",
"express-validator": "^7.2.0", "express-validator": "^7.0.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jade": "^0.29.0", "jade": "^0.29.0",
"mongoose": "^8.3.4", "mongoose": "^8.3.4",

7
utils/LoginUtil.js Normal file
View File

@ -0,0 +1,7 @@
exports.authenticateSeesion = async (req, res, next) => {
if (req.session.user) {
next()
} else {
res.status(401).json({ error: 'Unauthorized' })
}
}

View File

@ -1,8 +0,0 @@
exports.authenticateSession = async (req, res, next) => {
if (req.session.user) {
next()
} else {
res.status(401).json({ error: 'Unauthorized' })
}
}