mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-19 03:29:37 +02:00
Backend: auth for bookmarks and categories
This commit is contained in:
parent
22471d64c7
commit
0d36c5cf94
6 changed files with 39 additions and 12 deletions
|
@ -6,8 +6,12 @@ const { Sequelize } = require('sequelize');
|
||||||
// @route GET /api/bookmarks
|
// @route GET /api/bookmarks
|
||||||
// @access Public
|
// @access Public
|
||||||
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
|
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
|
||||||
|
// bookmarks visibility
|
||||||
|
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||||
|
|
||||||
const bookmarks = await Bookmark.findAll({
|
const bookmarks = await Bookmark.findAll({
|
||||||
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
||||||
|
where,
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
|
|
|
@ -6,8 +6,10 @@ const Bookmark = require('../../models/Bookmark');
|
||||||
// @route GET /api/bookmarks/:id
|
// @route GET /api/bookmarks/:id
|
||||||
// @access Public
|
// @access Public
|
||||||
const getSingleBookmark = asyncWrapper(async (req, res, next) => {
|
const getSingleBookmark = asyncWrapper(async (req, res, next) => {
|
||||||
|
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||||
|
|
||||||
const bookmark = await Bookmark.findOne({
|
const bookmark = await Bookmark.findOne({
|
||||||
where: { id: req.params.id },
|
where: { id: req.params.id, ...visibility },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!bookmark) {
|
if (!bookmark) {
|
||||||
|
|
|
@ -12,15 +12,20 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
|
||||||
|
|
||||||
let categories;
|
let categories;
|
||||||
|
|
||||||
|
// categories visibility
|
||||||
|
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||||
|
|
||||||
if (orderType == 'name') {
|
if (orderType == 'name') {
|
||||||
categories = await Category.findAll({
|
categories = await Category.findAll({
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: Bookmark,
|
model: Bookmark,
|
||||||
as: 'bookmarks',
|
as: 'bookmarks',
|
||||||
|
where,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
|
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
|
||||||
|
where,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
categories = await Category.findAll({
|
categories = await Category.findAll({
|
||||||
|
@ -28,9 +33,11 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
|
||||||
{
|
{
|
||||||
model: Bookmark,
|
model: Bookmark,
|
||||||
as: 'bookmarks',
|
as: 'bookmarks',
|
||||||
|
where,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
order: [[orderType, 'ASC']],
|
order: [[orderType, 'ASC']],
|
||||||
|
where,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,15 @@ const Bookmark = require('../../models/Bookmark');
|
||||||
// @route GET /api/categories/:id
|
// @route GET /api/categories/:id
|
||||||
// @access Public
|
// @access Public
|
||||||
const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
||||||
|
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||||
|
|
||||||
const category = await Category.findOne({
|
const category = await Category.findOne({
|
||||||
where: { id: req.params.id },
|
where: { id: req.params.id, ...visibility },
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: Bookmark,
|
model: Bookmark,
|
||||||
as: 'bookmarks',
|
as: 'bookmarks',
|
||||||
|
where: visibility,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const upload = require('../middleware/multer');
|
|
||||||
|
// middleware
|
||||||
|
const { upload, auth, requireAuth } = require('../middleware');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
createBookmark,
|
createBookmark,
|
||||||
|
@ -10,12 +12,15 @@ const {
|
||||||
deleteBookmark,
|
deleteBookmark,
|
||||||
} = require('../controllers/bookmarks');
|
} = require('../controllers/bookmarks');
|
||||||
|
|
||||||
router.route('/').post(upload, createBookmark).get(getAllBookmarks);
|
router
|
||||||
|
.route('/')
|
||||||
|
.post(auth, requireAuth, upload, createBookmark)
|
||||||
|
.get(auth, getAllBookmarks);
|
||||||
|
|
||||||
router
|
router
|
||||||
.route('/:id')
|
.route('/:id')
|
||||||
.get(getSingleBookmark)
|
.get(auth, getSingleBookmark)
|
||||||
.put(upload, updateBookmark)
|
.put(auth, requireAuth, upload, updateBookmark)
|
||||||
.delete(deleteBookmark);
|
.delete(auth, requireAuth, deleteBookmark);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
// middleware
|
||||||
|
const { auth, requireAuth } = require('../middleware');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
createCategory,
|
createCategory,
|
||||||
getAllCategories,
|
getAllCategories,
|
||||||
|
@ -10,14 +13,17 @@ const {
|
||||||
reorderCategories,
|
reorderCategories,
|
||||||
} = require('../controllers/categories');
|
} = require('../controllers/categories');
|
||||||
|
|
||||||
router.route('/').post(createCategory).get(getAllCategories);
|
router
|
||||||
|
.route('/')
|
||||||
|
.post(auth, requireAuth, createCategory)
|
||||||
|
.get(auth, getAllCategories);
|
||||||
|
|
||||||
router
|
router
|
||||||
.route('/:id')
|
.route('/:id')
|
||||||
.get(getSingleCategory)
|
.get(auth, getSingleCategory)
|
||||||
.put(updateCategory)
|
.put(auth, requireAuth, updateCategory)
|
||||||
.delete(deleteCategory);
|
.delete(auth, requireAuth, deleteCategory);
|
||||||
|
|
||||||
router.route('/0/reorder').put(reorderCategories);
|
router.route('/0/reorder').put(auth, requireAuth, reorderCategories);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue