2021-10-20 17:30:05 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-11-03 19:31:57 +01:00
|
|
|
const { yup, validateYupSchema } = require('@strapi/utils');
|
2021-10-20 17:30:05 +02:00
|
|
|
|
2022-06-01 16:22:19 +02:00
|
|
|
const callbackSchema = yup.object({
|
2021-10-20 17:30:05 +02:00
|
|
|
identifier: yup.string().required(),
|
|
|
|
password: yup.string().required(),
|
|
|
|
});
|
|
|
|
|
2024-10-02 12:03:28 +03:00
|
|
|
const createRegisterSchema = (config) =>
|
2024-09-27 10:54:37 +03:00
|
|
|
yup.object({
|
|
|
|
email: yup.string().email().required(),
|
|
|
|
username: yup.string().required(),
|
|
|
|
password: yup
|
|
|
|
.string()
|
|
|
|
.required()
|
2025-01-29 16:54:15 +01:00
|
|
|
.test(function (value) {
|
|
|
|
if (!value) return true;
|
|
|
|
const isValid = new TextEncoder().encode(value).length <= 72;
|
|
|
|
if (!isValid) {
|
|
|
|
return this.createError({ message: 'Password must be less than 73 bytes' });
|
2025-01-29 12:34:22 +01:00
|
|
|
}
|
2025-01-29 16:54:15 +01:00
|
|
|
return true;
|
|
|
|
})
|
2024-09-30 16:28:57 +03:00
|
|
|
.test(async function (value) {
|
|
|
|
if (typeof config?.validatePassword === 'function') {
|
|
|
|
try {
|
|
|
|
const isValid = await config.validatePassword(value);
|
|
|
|
if (!isValid) {
|
|
|
|
return this.createError({ message: 'Password validation failed.' });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return this.createError({ message: error.message || 'An error occurred.' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2024-09-27 12:59:32 +03:00
|
|
|
}),
|
2024-09-27 10:54:37 +03:00
|
|
|
});
|
2021-10-20 17:30:05 +02:00
|
|
|
|
2022-06-01 16:22:19 +02:00
|
|
|
const sendEmailConfirmationSchema = yup.object({
|
2022-08-08 23:33:39 +02:00
|
|
|
email: yup.string().email().required(),
|
2021-10-20 17:30:05 +02:00
|
|
|
});
|
|
|
|
|
2022-06-01 17:59:30 +02:00
|
|
|
const validateEmailConfirmationSchema = yup.object({
|
|
|
|
confirmation: yup.string().required(),
|
|
|
|
});
|
|
|
|
|
2022-06-01 16:22:19 +02:00
|
|
|
const forgotPasswordSchema = yup
|
|
|
|
.object({
|
2022-08-08 23:33:39 +02:00
|
|
|
email: yup.string().email().required(),
|
2022-05-31 19:38:08 +02:00
|
|
|
})
|
|
|
|
.noUnknown();
|
|
|
|
|
2024-10-02 12:03:28 +03:00
|
|
|
const createResetPasswordSchema = (config) =>
|
2024-09-27 10:54:37 +03:00
|
|
|
yup
|
|
|
|
.object({
|
|
|
|
password: yup
|
|
|
|
.string()
|
|
|
|
.required()
|
2025-01-29 16:54:15 +01:00
|
|
|
.test(function (value) {
|
|
|
|
if (!value) return true;
|
|
|
|
const isValid = new TextEncoder().encode(value).length <= 72;
|
|
|
|
if (!isValid) {
|
|
|
|
return this.createError({ message: 'Password must be less than 73 bytes' });
|
2025-01-29 12:34:22 +01:00
|
|
|
}
|
2025-01-29 16:54:15 +01:00
|
|
|
return true;
|
|
|
|
})
|
2024-09-30 16:28:57 +03:00
|
|
|
.test(async function (value) {
|
|
|
|
if (typeof config?.validatePassword === 'function') {
|
|
|
|
try {
|
|
|
|
const isValid = await config.validatePassword(value);
|
|
|
|
if (!isValid) {
|
|
|
|
return this.createError({ message: 'Password validation failed.' });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return this.createError({ message: error.message || 'An error occurred.' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2024-09-27 12:59:32 +03:00
|
|
|
}),
|
2024-09-27 10:54:37 +03:00
|
|
|
passwordConfirmation: yup
|
|
|
|
.string()
|
|
|
|
.required()
|
|
|
|
.oneOf([yup.ref('password')], 'Passwords do not match'),
|
|
|
|
|
|
|
|
code: yup.string().required(),
|
|
|
|
})
|
|
|
|
.noUnknown();
|
|
|
|
|
2024-10-02 12:03:28 +03:00
|
|
|
const createChangePasswordSchema = (config) =>
|
2024-09-27 10:54:37 +03:00
|
|
|
yup
|
|
|
|
.object({
|
|
|
|
password: yup
|
|
|
|
.string()
|
|
|
|
.required()
|
2025-01-29 16:54:15 +01:00
|
|
|
.test(function (value) {
|
|
|
|
if (!value) return true;
|
|
|
|
const isValid = new TextEncoder().encode(value).length <= 72;
|
|
|
|
if (!isValid) {
|
|
|
|
return this.createError({ message: 'Password must be less than 73 bytes' });
|
2025-01-29 12:34:22 +01:00
|
|
|
}
|
2025-01-29 16:54:15 +01:00
|
|
|
return true;
|
|
|
|
})
|
2024-09-30 16:28:57 +03:00
|
|
|
.test(async function (value) {
|
|
|
|
if (typeof config?.validatePassword === 'function') {
|
|
|
|
try {
|
|
|
|
const isValid = await config.validatePassword(value);
|
|
|
|
if (!isValid) {
|
|
|
|
return this.createError({ message: 'Password validation failed.' });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return this.createError({ message: error.message || 'An error occurred.' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2024-09-27 12:59:32 +03:00
|
|
|
}),
|
2024-09-27 10:54:37 +03:00
|
|
|
passwordConfirmation: yup
|
|
|
|
.string()
|
|
|
|
.required()
|
|
|
|
.oneOf([yup.ref('password')], 'Passwords do not match'),
|
|
|
|
currentPassword: yup.string().required(),
|
|
|
|
})
|
|
|
|
.noUnknown();
|
2022-08-02 20:58:37 +02:00
|
|
|
|
2021-10-20 17:30:05 +02:00
|
|
|
module.exports = {
|
2022-06-01 16:22:19 +02:00
|
|
|
validateCallbackBody: validateYupSchema(callbackSchema),
|
2024-10-02 12:03:28 +03:00
|
|
|
validateRegisterBody: (payload, config) =>
|
|
|
|
validateYupSchema(createRegisterSchema(config))(payload),
|
2022-06-01 16:22:19 +02:00
|
|
|
validateSendEmailConfirmationBody: validateYupSchema(sendEmailConfirmationSchema),
|
2022-06-01 17:59:30 +02:00
|
|
|
validateEmailConfirmationBody: validateYupSchema(validateEmailConfirmationSchema),
|
2022-06-01 16:22:19 +02:00
|
|
|
validateForgotPasswordBody: validateYupSchema(forgotPasswordSchema),
|
2024-10-02 12:03:28 +03:00
|
|
|
validateResetPasswordBody: (payload, config) =>
|
|
|
|
validateYupSchema(createResetPasswordSchema(config))(payload),
|
|
|
|
validateChangePasswordBody: (payload, config) =>
|
|
|
|
validateYupSchema(createChangePasswordSchema(config))(payload),
|
2021-10-20 17:30:05 +02:00
|
|
|
};
|