-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
273 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- 'stable' | ||
- '10' | ||
|
||
cache: | ||
directories: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import models from '../models'; | ||
import Util from './util'; | ||
|
||
const util = new Util(); | ||
const db = models.Article; | ||
const viewDb = models.View; | ||
|
||
const viewArticleHelper = async (req, res, slug) => { | ||
const findArticle = await db.findOne({ where: { slug } }); | ||
if (!findArticle) { | ||
util.setError(404, 'That article does not exist'); | ||
util.send(res); | ||
} | ||
// find ip address | ||
const ipAddress = req.header('x-forwarded-for') || req.connection.remoteAddress; | ||
|
||
// check if the article was viewed | ||
const findViewsOfArticle = await viewDb.findOne({ where: { articleId: findArticle.id } }); | ||
|
||
let viewObject = { | ||
articleId: findArticle.id, | ||
userId: req.auth ? req.auth.id : null, | ||
IP_address: ipAddress, | ||
userType: req.auth ? 'loggedInUser' : 'guest', | ||
views: 1 | ||
}; | ||
// update or create article views | ||
if (findViewsOfArticle) { | ||
viewObject = { ...viewObject, views: findViewsOfArticle.views + 1 }; | ||
await viewDb.update(viewObject, { | ||
where: { | ||
articleId: findArticle.id | ||
}, | ||
returing: true | ||
}); | ||
} else { | ||
await viewDb.create(viewObject); | ||
} | ||
return { | ||
views: findViewsOfArticle.views | ||
}; | ||
}; | ||
|
||
export default viewArticleHelper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import env from 'dotenv'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
env.config(); | ||
const verifyIfTokenExists = async (req, res, next) => { | ||
let token = req.headers['x-access-token'] || req.headers.authorization; | ||
if (!token) { | ||
next(); | ||
} else { | ||
if (token.startsWith('Bearer ')) { | ||
token = token.slice(7, token.length); | ||
} | ||
if (token) { | ||
jwt.verify(token, process.env.SECRET_KEY, (err, decode) => { | ||
if (err) { | ||
return res.status(401).send({ | ||
status: 401, | ||
message: 'Token is not valid' | ||
}); | ||
} | ||
req.token = token; | ||
req.auth = decode; | ||
next(); | ||
}); | ||
} | ||
} | ||
}; | ||
export default verifyIfTokenExists; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.