fix: overload Strapi registration
This commit is contained in:
@@ -1,29 +1,87 @@
|
||||
module.exports = plugin => {
|
||||
const sanitizeOutput = (user) => {
|
||||
const {password, resetPasswordToken, confirmationToken, ...sanitizedUser} = user
|
||||
return sanitizedUser;
|
||||
'use strict';
|
||||
|
||||
const yup = require('yup');
|
||||
|
||||
module.exports = (plugin) => {
|
||||
/**
|
||||
* Validation custom (équivalent Strapi)
|
||||
*/
|
||||
const registerSchema = yup.object({
|
||||
username: yup.string().required(),
|
||||
email: yup.string().email().required(),
|
||||
password: yup.string().min(6).required(),
|
||||
});
|
||||
|
||||
const validateRegisterBody = async (data) => {
|
||||
try {
|
||||
return await registerSchema.validate(data, {
|
||||
abortEarly: false,
|
||||
stripUnknown: true,
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err.errors.join(', ');
|
||||
throw new Error(message);
|
||||
}
|
||||
};
|
||||
|
||||
plugin.controllers.user.me = async (ctx) => {
|
||||
if (!ctx.state.user) {
|
||||
return ctx.unauthorized();
|
||||
/**
|
||||
* Override du controller register
|
||||
*/
|
||||
plugin.controllers.auth.register = async (ctx) => {
|
||||
const { body } = ctx.request;
|
||||
|
||||
// 🔒 1. Validation
|
||||
let params;
|
||||
try {
|
||||
params = await validateRegisterBody(body);
|
||||
} catch (err) {
|
||||
return ctx.badRequest(err.message);
|
||||
}
|
||||
|
||||
const user = await strapi.entityService.findOne('plugin::users-permissions.user', ctx.state.user.id, {populate: {
|
||||
paroles: {
|
||||
const { email, username, password } = params;
|
||||
|
||||
// 🔎 2. Vérifier si user existe déjà
|
||||
const userService = strapi.service('plugin::users-permissions.user');
|
||||
|
||||
const existingUser = await userService.fetchAll({
|
||||
filters: {
|
||||
publishedAt: {
|
||||
$eq: null
|
||||
}
|
||||
$or: [{ email }, { username }],
|
||||
},
|
||||
populate: {
|
||||
artistes: true,
|
||||
traductions: true
|
||||
}
|
||||
}
|
||||
}})
|
||||
});
|
||||
|
||||
ctx.body = sanitizeOutput(user)
|
||||
if (existingUser.length > 0) {
|
||||
return ctx.badRequest('Email or Username already taken');
|
||||
}
|
||||
return plugin
|
||||
}
|
||||
|
||||
// ⚙️ 3. Récupérer rôle "authenticated"
|
||||
const role = await strapi
|
||||
.query('plugin::users-permissions.role')
|
||||
.findOne({ where: { type: 'authenticated' } });
|
||||
|
||||
if (!role) {
|
||||
return ctx.badRequest('Default role not found');
|
||||
}
|
||||
|
||||
// 👤 4. Création user
|
||||
const newUser = await userService.add({
|
||||
username,
|
||||
email,
|
||||
password,
|
||||
role: role.id,
|
||||
confirmed: false,
|
||||
});
|
||||
|
||||
// 🔑 5. Générer JWT
|
||||
const jwtService = strapi.service('plugin::users-permissions.jwt');
|
||||
|
||||
const token = jwtService.issue({ id: newUser.id });
|
||||
|
||||
// 📤 6. Réponse
|
||||
ctx.send({
|
||||
jwt: token,
|
||||
user: newUser,
|
||||
});
|
||||
};
|
||||
|
||||
return plugin;
|
||||
};
|
||||
Reference in New Issue
Block a user