feat(users): configure users collection and validate email format

Migration sets access rules, password min 8, name max 100, password-only
auth, and a case-insensitive unique email index. A hook rejects non-ASCII
and malformed emails on every save. API tests cover registration, login,
and isolation between two users and a guest.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kamask
2026-11-05 18:24:33 +03:00
co-authored by Claude Sonnet 5.5
parent eb4c1c6604
commit 115b5fb036
4 changed files with 385 additions and 75 deletions
+17
View File
@@ -0,0 +1,17 @@
/// <reference path="../pb_data/types.d.ts" />
// Встроенная проверка PocketBase пропускает кириллицу и домен верхнего уровня из одной буквы.
// onRecordValidate срабатывает при любом сохранении: регистрация, смена email, суперпользователь.
onRecordValidate((e) => {
// Обработчики выполняются изолированно, поэтому регулярка объявлена внутри.
// Только печатные символы ASCII без пробела: что-то@что-то.минимум-два-символа.
const email = /^[!-?A-~]+@[!-?A-~]+\.[!-?A-~]{2,}$/;
if (!email.test(e.record.email())) {
throw new BadRequestError('Некорректный email.', {
email: new ValidationError('validation_invalid_email', 'Некорректный email.'),
});
}
e.next();
}, 'users');
@@ -0,0 +1,40 @@
/// <reference path="../pb_data/types.d.ts" />
function configureUsers(app, { deleteRule, nameMax, emailColumn }) {
const users = app.findCollectionByNameOrId('users');
users.listRule = 'id = @request.auth.id';
users.viewRule = 'id = @request.auth.id';
users.createRule = '';
users.updateRule = 'id = @request.auth.id';
users.deleteRule = deleteRule;
// Встроенный индекс PocketBase учитывает регистр: без замены A@x.ru и a@x.ru — разные аккаунты.
users.addIndex('idx_email__pb_users_auth_', true, emailColumn, "`email` != ''");
users.fields.getByName('password').min = 8;
users.fields.getByName('name').max = nameMax;
users.passwordAuth.enabled = true;
users.passwordAuth.identityFields = ['email'];
users.oauth2.enabled = false;
users.otp.enabled = false;
users.mfa.enabled = false;
app.save(users);
}
migrate(
(app) =>
configureUsers(app, {
deleteRule: null,
nameMax: 100,
emailColumn: '`email` COLLATE NOCASE',
}),
(app) =>
configureUsers(app, {
deleteRule: 'id = @request.auth.id',
nameMax: 255,
emailColumn: '`email`',
}),
);