Ejemplo n.º 1
0
        public async Task RegisterAsync(UserModel newUser, string password, string verificationCode)
        {
            //double check user does not exist in case function is called in some other way.
            bool isUserExist = await _userRepository.IsExistsAsync(newUser.Email);

            if (isUserExist)
            {
                throw new UserWithRequestedEmailAlreadyExistsException(newUser.Email);
            }
            EmailVerificationModel verification = await _userRepository.GetVerificationCodeAsync(newUser.Email);

            if (verification.Code != verificationCode)
            {
                throw new IncorrectVerificationCodeException(verificationCode);
            }
            if (verification.ExpirationTime < DateTime.Now)
            {
                throw new VerificationCodeExpiredException(verification.ExpirationTime);
            }
            string passwordSalt = _passwordHasher.CreateSalt();
            string passwordHash = _passwordHasher.CreatePasswordHash(password, passwordSalt);

            newUser.PasswordHash = passwordHash;
            newUser.PasswordSalt = passwordSalt;
            newUser.IsAdmin      = false;
            newUser.Address.Id   = Guid.NewGuid();
            await _userRepository.AddAsync(newUser);

            Log.Information("User with email {@email}  created successfully", newUser.Email);
        }
        public async Task <Unit> Handle(CreatePasswordCommand request, CancellationToken ct)
        {
            var account = await _repository.GetAsync(request.Id, ct);

            if (account is null)
            {
                throw new ElwarkException(ElwarkExceptionCodes.AccountNotFound);
            }

            var salt = _hasher.CreateSalt();

            account.SetPassword(request.Password, salt, _hasher.CreateHash);

            await _repository.UpdateAsync(account, ct);

            await _confirmation.DeleteAsync(account.Id, ct);

            return(Unit.Value);
        }
        public async Task <SignUpResult> Handle(SignUpByEmailCommand request, CancellationToken ct)
        {
            var exists = await _repository.GetAsync(request.Email, ct);

            ObjectId confirmationId;

            if (exists is not null)
            {
                if (exists.IsConfirmed())
                {
                    throw new ElwarkException(ElwarkExceptionCodes.EmailAlreadyExists);
                }

                confirmationId = await _mediator.Send(
                    new SendConfirmationCommand(exists.Id, exists.GetPrimaryEmail().GetIdentity(), request.Language),
                    ct
                    );

                return(new SignUpResult(exists.Id, exists.Name.FullName(), confirmationId));
            }

            var email   = request.Email.GetMailAddress();
            var account = new Account(new Name(email.User), request.Language, Profile.DefaultPicture, request.Ip);

            account.AddEmail(email, false);

            var salt = _passwordHasher.CreateSalt();

            account.SetPassword(request.Password, salt, _passwordHasher.CreateHash);

            await _repository.CreateAsync(account, ct);

            confirmationId = await _mediator.Send(
                new SendConfirmationCommand(account.Id, account.GetPrimaryEmail().GetIdentity(), request.Language),
                ct
                );

            return(new SignUpResult(account.Id, account.Name.FullName(), confirmationId));
        }