Пример #1
0
        public async Task RegisterAsync(UserProfile user)
        {
            if (!StringUtils.hasText(user.Username))
            {
                throw new ValidationException("username is required");
            }

            if (!StringUtils.hasText(user.Password))
            {
                throw new ValidationException("password is required");
            }

            if (user.BirthDate == null || user.BirthDate == DateTime.MinValue)
            {
                throw new ValidationException("BirthDate is required");
            }

            var dbUser = await userRepository.FindByUsernameAsync(user.Username);

            if (dbUser != null)
            {
                throw new ValidationException("username already exists");
            }

            user.Password = (PasswordUtil.encode(user.Password));
            await userRepository.InsertAsync(user);

            try
            {
                await messageSender.RegisterDestinationAsync(new Destination(DestinationType.User, user.Id));
            }
            catch (Exception e)
            {
                await userRepository.DeleteByIdAsync(user.Id);

                throw;
            }
        }
Пример #2
0
        public async Task <UserSession> LoginAsync(LoginRequest lr)
        {
            UserProfile profile = await FindByUsernameAsync(lr.Username);

            if (profile == null)
            {
                throw new ValidationException("username does not exists");
            }

            if (PasswordUtil.encode(lr.Password).Equals(profile.Password))
            {
                UserSession us = new UserSession()
                {
                    CreatedAt = DateTime.Now,
                    UserId    = profile.Id
                };

                await userSessionRepository.InsertAsync(us);

                return(us);
            }

            throw new ValidationException("Invalid password");
        }