MagicLinkUserNameComponent: allow null username

This commit is contained in:
Alice Gaudon 2020-11-15 15:21:26 +01:00
parent 6a65ec723d
commit 124bc8785f
4 changed files with 15 additions and 6 deletions

View File

@ -12,6 +12,8 @@ import Application from "../../Application";
import {MailTemplate} from "../../mail/Mail";
import AuthMagicLinkActionType from "./AuthMagicLinkActionType";
import Validator from "../../db/Validator";
import ModelFactory from "../../db/ModelFactory";
import UserNameComponent from "../models/UserNameComponent";
export default class MagicLinkAuthMethod implements AuthMethod<MagicLink> {
public constructor(
@ -77,9 +79,16 @@ export default class MagicLinkAuthMethod implements AuthMethod<MagicLink> {
const actionType = isRegistration ? AuthMagicLinkActionType.REGISTER : AuthMagicLinkActionType.LOGIN;
if (isRegistration) {
const usernameValidator = new Validator();
if (ModelFactory.get(User).hasComponent(UserNameComponent)) usernameValidator.defined();
await Validator.validate({
email: new Validator().defined().unique(UserEmail, 'email'),
}, {email: email});
name: usernameValidator,
}, {
email: email,
name: req.body.name,
});
}
await MagicLinkController.sendMagicLink(

View File

@ -7,6 +7,6 @@ export default class MagicLinkUserNameComponent extends ModelComponent<MagicLink
public readonly username?: string = undefined;
protected init(): void {
this.setValidation('username').defined().between(3, 64).regexp(USERNAME_REGEXP).unique(User, 'name');
this.setValidation('username').acceptUndefined().between(3, 64).regexp(USERNAME_REGEXP).unique(User, 'name');
}
}

View File

@ -85,10 +85,10 @@ export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof>
Throttler.throttle('register_password', 10, 30000, req.ip);
req.body.username = identifier;
req.body.identifier = identifier;
await Validator.validate({
username: new Validator().defined().between(3, 64).regexp(USERNAME_REGEXP).unique(User, 'name'),
identifier: new Validator().defined().between(3, 64).regexp(USERNAME_REGEXP).unique(User, 'name'),
password: new Validator().defined().minLength(UserPasswordComponent.PASSWORD_MIN_LENGTH),
password_confirmation: new Validator().defined().sameAs('password', req.body.password),
terms: new Validator().defined(),
@ -104,7 +104,7 @@ export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof>
await user.as(UserPasswordComponent).setPassword(req.body.password);
// Username
user.as(UserNameComponent).name = req.body.username;
user.as(UserNameComponent).name = req.body.identifier;
return callbacks;
}, async (connection, user) => {

View File

@ -152,7 +152,7 @@ describe('Register with username', () => {
})
.expect(400);
// username field should be translated from identifier
expect(res.body.messages?.username?.name).toStrictEqual('AlreadyExistsValidationError');
expect(res.body.messages?.identifier?.name).toStrictEqual('AlreadyExistsValidationError');
// Verify nothing changed
expect(await User.select()