refactor: extraire la vérification JWT/payload en policy partagée
This commit is contained in:
@@ -1,26 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
const { createCoreController } = require('@strapi/strapi').factories;
|
||||
const { ApplicationError, NotFoundError, UnauthorizedError } = require("@strapi/utils").errors
|
||||
const { ApplicationError, NotFoundError } = require("@strapi/utils").errors
|
||||
|
||||
module.exports = createCoreController('api::commentaire.commentaire', ({strapi}) => ({
|
||||
async create(ctx) {
|
||||
const {body} = ctx.request
|
||||
let {data} = body
|
||||
|
||||
if (ctx.request && ctx.request.header && ctx.request.header.authorization) {
|
||||
try {
|
||||
const {id} = await strapi.plugins[
|
||||
'users-permissions'
|
||||
].services.jwt.getToken(ctx)
|
||||
|
||||
if (id !== data.user.id) {
|
||||
throw new UnauthorizedError('Opération non autorisée')
|
||||
}
|
||||
} catch (err) {
|
||||
throw new UnauthorizedError(ctx, err, 'Opération non autorisée')
|
||||
}
|
||||
}
|
||||
const user = await strapi.db.query('plugin::users-permissions.user').findOne({
|
||||
where: {id: data.user.id}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user