Switched emails to being mandetory for users

ref #93
This commit is contained in:
2021-01-13 17:44:22 +01:00
parent cf788fe07b
commit 9feeb302e8
8 changed files with 40 additions and 24 deletions

View File

@@ -1,8 +1,8 @@
import * as argon2 from "argon2";
import { IsBoolean, IsEmail, IsInt, IsOptional, IsPhoneNumber, IsString, IsUrl } from 'class-validator';
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUrl } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { config } from '../../../config';
import { UsernameOrEmailNeededError } from '../../../errors/AuthError';
import { UserEmailNeededError } from '../../../errors/UserErrors';
import { UserGroupNotFoundError } from '../../../errors/UserGroupErrors';
import { User } from '../../entities/User';
import { UserGroup } from '../../entities/UserGroup';
@@ -40,7 +40,7 @@ export class UpdateUser {
/**
* The updated user's username.
* You have to provide at least one of: {email, username}.
* You have to provide a email addres, so this is optional.
*/
@IsOptional()
@IsString()
@@ -48,12 +48,11 @@ export class UpdateUser {
/**
* The updated user's email address.
* You have to provide at least one of: {email, username}.
*/
@IsEmail()
@IsString()
@IsOptional()
email?: string;
@IsNotEmpty()
email: string;
/**
* The updated user's phone number.
@@ -99,11 +98,11 @@ export class UpdateUser {
* @param user The user that shall be updated.
*/
public async update(user: User): Promise<User> {
if (!this.email) {
throw new UserEmailNeededError();
}
user.email = this.email;
user.username = this.username;
if ((user.email === undefined || user.email === null) && (user.username === undefined || user.username === null)) {
throw new UsernameOrEmailNeededError();
}
if (this.password) {
user.password = await argon2.hash(this.password + user.uuid);
user.refreshTokenCount = user.refreshTokenCount + 1;