137 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

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
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) =>
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;
})
.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;
}),
});
2021-10-20 17:30:05 +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(),
});
const forgotPasswordSchema = yup
.object({
2022-08-08 23:33:39 +02:00
email: yup.string().email().required(),
})
.noUnknown();
2024-10-02 12:03:28 +03:00
const createResetPasswordSchema = (config) =>
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;
})
.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;
}),
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) =>
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;
})
.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;
}),
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 = {
validateCallbackBody: validateYupSchema(callbackSchema),
2024-10-02 12:03:28 +03:00
validateRegisterBody: (payload, config) =>
validateYupSchema(createRegisterSchema(config))(payload),
validateSendEmailConfirmationBody: validateYupSchema(sendEmailConfirmationSchema),
2022-06-01 17:59:30 +02:00
validateEmailConfirmationBody: validateYupSchema(validateEmailConfirmationSchema),
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
};