Skip to content

Commit b0ba14c

Browse files
committed
feat: add password field validation to EmailInvitePlugin
1 parent 8d07763 commit b0ba14c

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

custom/SetPassword.vue

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,36 @@ const backgroundPosition = computed(() => {
174174
return coreStore.config?.loginBackgroundPosition || '1/2';
175175
});
176176
177+
const passwordField = computed(
178+
() => route.meta.passwordField
179+
)
180+
181+
177182
function checkPassword() {
178-
if (!password.value || !passwordConfirmation.value) {
179-
return 'Please enter both password and password confirmation';
180-
}
181-
if (password.value !== passwordConfirmation.value) {
182-
return 'Passwords do not match';
183-
}
184183
185-
// Basic password validation
186-
if (password.value.length < 8) {
187-
return 'Password must be at least 8 characters long';
184+
if (!password.value || !passwordConfirmation.value) {
185+
return 'Please enter both password and password confirmation';
186+
}
187+
if (password.value !== passwordConfirmation.value) {
188+
return 'Passwords do not match';
189+
}
190+
191+
if (password.value.length < passwordField.value.minLength) {
192+
return `Password must be at least ${passwordField.value.minLength} characters long`;
193+
}
194+
195+
if (password.value.length > passwordField.value.maxLength) {
196+
return `Password must be at most ${passwordField.value.maxLength} characters long`;
197+
}
198+
199+
if (passwordField.value.validation) {
200+
const valError = applyRegexValidation(password.value, passwordField.value.validation);
201+
if (valError) {
202+
return valError;
188203
}
204+
}
189205
190-
return null;
206+
return null;
191207
}
192208
193209
const validationError = computed(() => {

index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
4848
}
4949
this.emailField = emailField;
5050

51+
if (!this.options.passwordField) {
52+
throw new Error(`passwordField is required to get password constraints and should be a name of virtual field in auth resource`);
53+
}
54+
55+
const passwordField = authResource.columns.find(f => f.name === this.options.passwordField);
56+
if (!passwordField) {
57+
const similar = suggestIfTypo(authResource.columns.map(f => f.name), this.options.passwordField);
58+
59+
throw new Error(`Field with name ${this.options.passwordField} not found in resource ${authResource.resourceId}.
60+
${similar ? `Did you mean ${similar}?` : ''}
61+
`);
62+
}
63+
5164
if (this.options.emailConfirmedField) {
5265
const emailConfirmedField = authResource.columns.find(f => f.name === this.options.emailConfirmedField);
5366
if (!emailConfirmedField) {
@@ -89,7 +102,12 @@ export default class EmailInvitePlugin extends AdminForthPlugin {
89102
file: this.componentPath('SetPassword.vue'),
90103
meta: {
91104
customLayout: true,
92-
pluginInstanceId: this.pluginInstanceId
105+
pluginInstanceId: this.pluginInstanceId,
106+
passwordField: {
107+
minLength: passwordField.minLength,
108+
maxLength: passwordField.maxLength,
109+
validation: passwordField.validation
110+
}
93111
}
94112
}
95113
});

types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export interface PluginOptions {
1212
*/
1313
sendFrom: string;
1414

15+
/**
16+
* Field name in auth resource which contains password
17+
*/
18+
passwordField: string;
19+
1520
/**
1621
* Email adapter to use for sending emails
1722
*/

0 commit comments

Comments
 (0)