2020-04-29 23:45:50 +00:00
|
|
|
'use strict'
|
|
|
|
/** @typedef {import('@adonisjs/framework/src/Request')} Request */
|
|
|
|
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
|
|
|
|
/** @typedef {import('@adonisjs/framework/src/View')} View */
|
2020-06-04 23:07:34 +00:00
|
|
|
|
2020-04-29 23:45:50 +00:00
|
|
|
class BookPageAuth {
|
|
|
|
/**
|
|
|
|
* @param {object} ctx
|
|
|
|
* @param {Request} ctx.request
|
|
|
|
* @param {Function} next
|
|
|
|
*/
|
|
|
|
async handle(ctx, next) {
|
2020-06-04 23:07:34 +00:00
|
|
|
const {request, auth, response, book} = ctx;
|
2020-04-29 23:45:50 +00:00
|
|
|
// call next to advance the request
|
|
|
|
const user = auth.user;
|
|
|
|
if (book.user_id) {
|
|
|
|
// Belongs to a user. Check if the book user has a connection with this
|
|
|
|
// user
|
2020-04-30 02:34:14 +00:00
|
|
|
if (book.user_id === user.id) {
|
|
|
|
await next();
|
|
|
|
} else {
|
|
|
|
response.status(403);
|
|
|
|
response.send({code: 403, message: 'Book is private'});
|
2020-04-29 23:45:50 +00:00
|
|
|
}
|
2020-04-30 02:34:14 +00:00
|
|
|
} else {
|
|
|
|
await next();
|
2020-04-29 23:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BookPageAuth
|