backedn converted to ts

This commit is contained in:
QkoSad
2023-07-27 15:58:35 +03:00
parent 3bf4e9fc56
commit 40051f9d5e
18 changed files with 1394 additions and 29668 deletions
+10 -7
View File
@@ -1,8 +1,9 @@
const config = require('config');
const jwt = require('jsonwebtoken');
import config from 'config'
import jwt from 'jsonwebtoken'
import type { Request, Response, NextFunction } from 'express';
function auth(req: Request, res: Response, next: NextFunction) {
module.exports = function (req, res, next) {
// Get token from header
const token = req.header('x-auth-token');
// Check if not token
@@ -16,7 +17,8 @@ module.exports = function (req, res, next) {
if (error) {
return res.status(401).json({ msg: 'Token is not valid' });
} else {
req.user = decoded.user;
if ('user' in req && decoded && typeof decoded !== "string")
req.user = decoded?.user;
next();
}
});
@@ -24,5 +26,6 @@ module.exports = function (req, res, next) {
console.error('something wrong with auth middleware');
res.status(500).json({ msg: 'Server Error' });
}
};
};
export default auth
+4 -3
View File
@@ -1,9 +1,10 @@
const mongoose = require('mongoose');
import mongoose from "mongoose";
// middleware to check for a valid object id
const checkObjectId = (idToCheck) => (req, res, next) => {
import type { Request, Response, NextFunction } from "express";
const checkObjectId = (idToCheck: string) => (req: Request, res: Response, next: NextFunction) => {
if (!mongoose.Types.ObjectId.isValid(req.params[idToCheck]))
return res.status(400).json({ msg: 'Invalid ID' });
next();
};
module.exports = checkObjectId;
export default checkObjectId