protected override async Task <UserLogin?> HandleInput(UserCredentials input) { using (var connection = database.GetConnection()) { IUserRepo userRepo = database.GetRepo <IUserRepo>(connection); User?user = await userRepo.FindByUsername(input.Username); if (user == null) { return(null); } if (passwordHasher.Verify(input.Password, user.PasswordHash)) { UserView userView = userMapper.Map(user); string authToken = tokenHandler.IssueToken(user); return(new UserLogin(userView, authToken)); } else { return(null); } } }
protected override async Task <UserLogin> HandleInput(UserRegisterParams input) { using (var connection = database.GetConnection()) { using (var transaction = connection.BeginTransaction()) { IUserRepo userRepo = database.GetRepo <IUserRepo>(connection); ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection); ISubscriptionRepo subRepo = database.GetRepo <ISubscriptionRepo>(connection); // Check that the email is free first. if (!String.IsNullOrWhiteSpace(input.Email)) { User?emailInUse = await userRepo.FindByEmail(input.Email); if (emailInUse != null) { throw new CollisionException("Email is already in use"); } } User?usernameInUse = await userRepo.FindByUsername(input.Username); if (usernameInUse != null) { throw new CollisionException("Username is unavailable"); } User user = new User() { Username = input.Username, PasswordHash = passwordHasher.Hash(input.Password), Email = StringUtils.NullifyWhiteSpace(input.Email), JoinedDate = System.DateTime.UtcNow }; await userRepo.Add(user); // Subscribe the user to the default spaces. IEnumerable <Space> defaultSpaces = await spaceRepo.FindDefault(); IEnumerable <Subscription> defaultSubscriptions = defaultSpaces.Select(space => new Subscription() { User = user, Space = space }); foreach (Subscription s in defaultSubscriptions) { await subRepo.Add(s); s.Space.SubscriptionCount++; await spaceRepo.Update(s.Space); } UserView userView = userMapper.Map(user); string authToken = tokenHandler.IssueToken(user); transaction.Commit(); return(new UserLogin(userView, authToken)); } } }