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

View File

@ -7,6 +7,6 @@ export default class MagicLinkUserNameComponent extends ModelComponent<MagicLink
public readonly username?: string = undefined; public readonly username?: string = undefined;
protected init(): void { 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); Throttler.throttle('register_password', 10, 30000, req.ip);
req.body.username = identifier; req.body.identifier = identifier;
await Validator.validate({ 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: new Validator().defined().minLength(UserPasswordComponent.PASSWORD_MIN_LENGTH),
password_confirmation: new Validator().defined().sameAs('password', req.body.password), password_confirmation: new Validator().defined().sameAs('password', req.body.password),
terms: new Validator().defined(), terms: new Validator().defined(),
@ -104,7 +104,7 @@ export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof>
await user.as(UserPasswordComponent).setPassword(req.body.password); await user.as(UserPasswordComponent).setPassword(req.body.password);
// Username // Username
user.as(UserNameComponent).name = req.body.username; user.as(UserNameComponent).name = req.body.identifier;
return callbacks; return callbacks;
}, async (connection, user) => { }, async (connection, user) => {

View File

@ -152,7 +152,7 @@ describe('Register with username', () => {
}) })
.expect(400); .expect(400);
// username field should be translated from identifier // 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 // Verify nothing changed
expect(await User.select() expect(await User.select()