diff --git a/.gitignore b/.gitignore index aa5e9b5..b1af356 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ dist-ssr *.sqlite *.db .env -.prisma/ \ No newline at end of file +.prisma/ +public/uploads/ \ No newline at end of file diff --git a/src/api/posts/index.ts b/src/api/posts/index.ts index 297fb82..cd2f1b0 100644 --- a/src/api/posts/index.ts +++ b/src/api/posts/index.ts @@ -6,6 +6,7 @@ import deletePost from './deletePost'; import likePost from './likePost'; import unlikePost from './unlikePost'; import { verifyToken } from '@/controller/AuthController'; +import { upload } from '@/controller/FileController'; const posts = Router(); @@ -17,7 +18,7 @@ const getToken = (req: Request): string | undefined => { const checkAuth = (req: Request, res: Response, next: NextFunction) => { const token = getToken(req); if (token === undefined) { - return res.status(401).send('No token provided'); + return res.status(401).send({ error: 'No token provided' }); } return verifyToken(token) .then((decodedToken: number) => { @@ -25,15 +26,15 @@ const checkAuth = (req: Request, res: Response, next: NextFunction) => { next(); }) .catch((error) => { - return res.status(401).send(error); + return res.status(401).send({ error }); }); }; posts.use(checkAuth); posts.get('/', getPosts); -posts.post('/new', newPost); -posts.put('/edit/:id', putPost); +posts.post('/new', upload.single('image'), newPost); +posts.put('/edit/:id', upload.single('image'), putPost); posts.delete('/delete/:id', deletePost); posts.put('/like/:id', likePost); posts.put('/unlike/:id', unlikePost); diff --git a/src/controller/AuthController.ts b/src/controller/AuthController.ts index f69225d..60fbc88 100644 --- a/src/controller/AuthController.ts +++ b/src/controller/AuthController.ts @@ -32,20 +32,20 @@ const genToken = (userId: number) => { }); }; -const verifyToken = async (token: string): Promise => { - const prismaToken = await prisma.token.findUnique({ - where: { token }, - }); - if (prismaToken === null) { - throw 'Token not found'; - } +const verifyToken = (token: string): Promise => { return new Promise((resolve, reject) => { - jwt.verify(token, config.JWT_SECRET, (err?, decoded?: jwt.JwtPayload | string) => { + jwt.verify(token, config.JWT_SECRET, async (err?, decoded?: jwt.JwtPayload | string) => { if (err) { - reject(err); + reject('Unkown error'); } else if (decoded === undefined || typeof decoded === 'string' || decoded.id === undefined) { reject('Invalid token'); } else { + const prismaToken = await prisma.token.findUnique({ + where: { token }, + }); + if (prismaToken === null) { + throw 'Token not found'; + } const decodedToken: number = decoded.id; resolve(decodedToken); } diff --git a/src/controller/FileController.ts b/src/controller/FileController.ts index e69de29..467360f 100644 --- a/src/controller/FileController.ts +++ b/src/controller/FileController.ts @@ -0,0 +1,11 @@ +import multer from 'multer'; +import path from 'path'; + +const storage = multer.diskStorage({ + destination: path.join(__dirname, '../../public/uploads'), + filename: (req, file, cb) => { + cb(null, file.originalname); + }, +}); + +export const upload = multer({ storage }); diff --git a/src/index.ts b/src/index.ts index f63f60f..0836ef1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,8 +37,8 @@ const checkExpiredTokenTimer = ms(process.env.CHECK_EXPIRED_TOKEN_EVERY || '60s' const app = express(); app.use(cors()); +app.use(json({ limit: '50mb' })); app.use(urlencoded({ extended: true, limit: '50mb' })); -app.use(json()); app.use('/api', api);