From 124bc8785fdfde41507b0574fe18132492620d29 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 15 Nov 2020 15:21:26 +0100 Subject: [PATCH] MagicLinkUserNameComponent: allow null username --- src/auth/magic_link/MagicLinkAuthMethod.ts | 11 ++++++++++- src/auth/models/MagicLinkUserNameComponent.ts | 2 +- src/auth/password/PasswordAuthMethod.ts | 6 +++--- test/Authentication.test.ts | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/auth/magic_link/MagicLinkAuthMethod.ts b/src/auth/magic_link/MagicLinkAuthMethod.ts index 52eba91..d321c7a 100644 --- a/src/auth/magic_link/MagicLinkAuthMethod.ts +++ b/src/auth/magic_link/MagicLinkAuthMethod.ts @@ -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 { public constructor( @@ -77,9 +79,16 @@ export default class MagicLinkAuthMethod implements AuthMethod { 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( diff --git a/src/auth/models/MagicLinkUserNameComponent.ts b/src/auth/models/MagicLinkUserNameComponent.ts index 000db34..f4fb2ca 100644 --- a/src/auth/models/MagicLinkUserNameComponent.ts +++ b/src/auth/models/MagicLinkUserNameComponent.ts @@ -7,6 +7,6 @@ export default class MagicLinkUserNameComponent extends ModelComponent 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 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) => { diff --git a/test/Authentication.test.ts b/test/Authentication.test.ts index 430bcab..4f91abc 100644 --- a/test/Authentication.test.ts +++ b/test/Authentication.test.ts @@ -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()