-
Notifications
You must be signed in to change notification settings - Fork 3
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
5 changed files
with
109 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { query } from '../db/index'; | ||
import validate from '../utils/validate'; | ||
|
||
class entryController { | ||
/** | ||
* Get all of a user's diary entries | ||
* Requires auth token to be passed in authorization header | ||
* @static | ||
* @param {*} req - Client request object | ||
* @param {*} res - Server response object | ||
* @returns {object} token | ||
* @memberof userController | ||
*/ | ||
static getAllEntries(req, res) { | ||
let { limit, page } = req.query; | ||
// validate queries | ||
limit = validate.isNumber(limit) ? limit : 20; | ||
page = validate.isNumber(page) ? page : 0; | ||
// get entries | ||
query( | ||
`SELECT entries.id, entries.title, entries.content, entries.created_on, entries.updated_on, | ||
entries.is_favorite FROM entries INNER JOIN users ON entries.user_id=users.id WHERE users.email=$1 | ||
LIMIT $2 OFFSET $3`, | ||
[req.authorizedUser.email, limit, page * limit], | ||
(err, result) => { | ||
if (err) { | ||
console.log(err); | ||
return res.status(500).json({ error: { message: 'An error occurred on the server' } }); | ||
} | ||
return res.status(200).json({ entries: result.rows, meta: { limit, page } }); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
export default entryController; |
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,24 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const verifyToken = (req, res, next) => { | ||
try { | ||
if (!req.headers.authorization) { | ||
return res | ||
.status(401) | ||
.json({ error: { message: 'Authorization failed. Please provide a token' } }); | ||
} | ||
const token = req.headers.authorization.split(' ')[1]; | ||
const authorizedUser = jwt.verify(token, process.env.SECRET_KEY); | ||
req.authorizedUser = authorizedUser; | ||
next(); | ||
} catch (error) { | ||
res | ||
.status(401) | ||
.json({ error: { message: 'Authorization failed. Your token is invalid or expired' } }); | ||
} | ||
}; | ||
|
||
export default verifyToken; |
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