From 6a0bf2f0c423e5c99a59e799b41e67238bcfb414 Mon Sep 17 00:00:00 2001 From: lucky-victory Date: Fri, 8 Apr 2022 18:29:44 +0100 Subject: [PATCH] [fix] article intro, decode html author bio --- constants/index.js | 8 ++++---- controllers/articles.js | 3 ++- helpers/auth.js | 4 ++-- middlewares/csrf-protect.js | 6 +++--- request.rest | 2 +- server.js | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/constants/index.js b/constants/index.js index af12fbb..a2c4897 100644 --- a/constants/index.js +++ b/constants/index.js @@ -1,10 +1,10 @@ -const isProd=process.env.NODE_ENV=='production'; -const isDev=process.env.NODE_ENV=='development'; +const IS_PROD=process.env.NODE_ENV==='production'; +const IS_DEV=process.env.NODE_ENV !=='production'; module.exports={ - isProd, - isDev + IS_PROD, + IS_DEV } \ No newline at end of file diff --git a/controllers/articles.js b/controllers/articles.js index 7b8d871..261f1ea 100644 --- a/controllers/articles.js +++ b/controllers/articles.js @@ -26,7 +26,7 @@ const getPublishedArticles=asyncHandler(async(req,res)=>{ res.status(200).json({message:"No more Articles","articles":[]}); return } - const articlesQuery=`SELECT a.id,a.publishedAt,a.title,a.authorId,a.views,a.heroImage,a.slug,a.tags,a.category,a.content,a.readTime,a.modifiedAt,u.fullname as _fullname,u.id as _id,u.twitter as _twitter,u.linkedIn as _linkedin,u.bio as _bio,u.username as _username,u.profileImage as _profileImage FROM ArticlesSchema.Articles as a INNER JOIN ArticlesSchema.Authors as u ON a.authorId=u.id WHERE a.published=true ${!NullOrUndefined(category) ? ` AND category='${category}'`:''} ORDER BY a.${orderBy} ${order} LIMIT ${limit} OFFSET ${offset} `; + const articlesQuery=`SELECT a.id,a.publishedAt,a.title,a.authorId,a.intro,a.views,a.heroImage,a.slug,a.tags,a.category,a.content,a.readTime,a.modifiedAt,u.fullname as _fullname,u.id as _id,u.twitter as _twitter,u.linkedIn as _linkedin,u.bio as _bio,u.username as _username,u.profileImage as _profileImage FROM ArticlesSchema.Articles as a INNER JOIN ArticlesSchema.Authors as u ON a.authorId=u.id WHERE a.published=true ${!NullOrUndefined(category) ? ` AND category='${category}'`:''} ORDER BY a.${orderBy} ${order} LIMIT ${limit} OFFSET ${offset} `; let articles=await Articles.query(articlesQuery); // nest author info as author property @@ -36,6 +36,7 @@ const getPublishedArticles=asyncHandler(async(req,res)=>{ articles=articles.map((article)=>{ article.title=decode(article.title); article.content=decode(article.content); + article.author.bio=decode(article.author.bio); article.tags=StringToArray(article.tags) return article; }); diff --git a/helpers/auth.js b/helpers/auth.js index 1e67242..64c7188 100644 --- a/helpers/auth.js +++ b/helpers/auth.js @@ -1,7 +1,7 @@ const cookie=require("cookie"); const { NullOrUndefined } = require("./utils"); const jwt=require('jsonwebtoken'); -const { isProd } = require("../constants"); +const { IS_PROD } = require("../constants"); const getJwtFromCookies=(req)=>{ @@ -11,7 +11,7 @@ const getJwtFromCookies=(req)=>{ return ({token}) } const setJwtToCookies=(res,token='')=>{ - res.cookie("blog_user_token",token,{httpOnly:isProd,secure:isProd}); + res.cookie("blog_user_token",token,{httpOnly:IS_PROD,secure:IS_PROD}); } const verifyToken=(token)=>{ return jwt.verify(token,process.env.JWT_SECRET || "12345") diff --git a/middlewares/csrf-protect.js b/middlewares/csrf-protect.js index 3080ad0..b37b39e 100644 --- a/middlewares/csrf-protect.js +++ b/middlewares/csrf-protect.js @@ -1,9 +1,9 @@ const csurf=require("csurf"); -const { isProd } = require("../constants"); +const { IS_PROD } = require("../constants"); const csrfProtect=csurf({cookie:{ - httpOnly:isProd, - secure:isProd + httpOnly:IS_PROD, + secure:IS_PROD }}); module.exports={csrfProtect}; \ No newline at end of file diff --git a/request.rest b/request.rest index a787bc7..609af84 100644 --- a/request.rest +++ b/request.rest @@ -8,7 +8,7 @@ Content-Type: application/json } -### +### PUT https://4901-luckyvictory-blogapi-pzfdy6iu340.ws-eu38.gitpod.io/profile/edit Content-Type: application/json diff --git a/server.js b/server.js index 3726a23..8199004 100644 --- a/server.js +++ b/server.js @@ -8,7 +8,7 @@ const createErrors=require('http-errors'); const fs=require('fs'); const path=require('path'); const morgan=require("morgan"); -const { isDev } = require('./constants'); +const { IS_DEV } = require('./constants'); const connectDB=require('./config/db'); connectDB(); @@ -63,7 +63,7 @@ return { status, code, message, -"stack":isDev ? stack : null +"stack":IS_DEV ? stack : null } }