Esempio n. 1
0
        public string Handle(RegisterQuery query, IWebHydraContext context)
        {
            Console.WriteLine($"Registering with '{query.Email}' and '{query.Password}'...");

            // TODO validation

            UserEntity user = _users.GetByEmail(query.Email);

            if (user != null)
            {
                throw new UserAlreadyExistsException();
            }

            UserEntity userEntity = new UserEntity()
            {
                Claims   = JsonConvert.SerializeObject(new Claims()),
                Id       = new Guid(),
                Email    = query.Email,
                Password = query.Password
            };

            _users.AddUser(userEntity);

            User user2 = new User();

            user2.Id     = userEntity.Id;
            user2.Claims = JsonConvert.DeserializeObject <Claims>(userEntity.Claims);

            return(_auth.GenerateTokenForUser(user2));
        }
        public async Task <RegistrationUserResult> RegisterUser(RegisterUserData newUser)
        {
            ValidatePassword(newUser);
            var user = newUser.ToModel(mapper, passwordHasher);

            await using var tmp = await connectionController.OpenConnectionAsync();

            return(await transaction.Run(async() =>
            {
                try
                {
                    user = await usersRepo.AddUser(user);
                }
                catch (ConnectionException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    throw new UserRegistrationException(e.Message, e);
                }

                await notificationSender.OnUserRegistered(user.ToDto(mapper));
                var token = await AuthenticateUser(user.Id);

                return new RegistrationUserResult
                {
                    User = user.ToDto(mapper),
                    Token = token
                };
            }));
        }
Esempio n. 3
0
 public IActionResult AddUser(AddUserRequest data)
 {
     // IUsersRepo repo = new UsersRepo();
     return(Ok(repo.AddUser(data)));
 }