Exemplo n.º 1
0
            private AuthenticatedUserOut CreateUserWithToken(User user)
            {
                var userOut = mapper.Map <AuthenticatedUserOut>(user);

                userOut.Token = tokenGenerator.CreateTokenString(user);

                return(userOut);
            }
Exemplo n.º 2
0
            public async Task <AuthenticatedUserOut> Handle(Command request, CancellationToken cancellationToken)
            {
                var userExists = await context.Users.AnyAsync(u => u.Email == request.LoginDetails.Email).ConfigureAwait(false);

                if (userExists)
                {
                    throw new UserAlreadyExistsException();
                }

                var user = mapper.Map <User>(request.LoginDetails);

                user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(request.LoginDetails.Password);
                context.Users.Add(user);

                try
                {
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    var userOut = mapper.Map <AuthenticatedUserOut>(user);
                    userOut.Token = tokenGenerator.CreateTokenString(user);

                    return(userOut);
                }
                catch (DbUpdateException ex)
                {
                    Exception resultEx = ex;
                    var       sex      = ex.InnerException as SqlException;
                    if (sex != null)
                    {
                        switch (sex.Number)
                        {
                        case 2601:
                            resultEx = new UserAlreadyExistsException();
                            break;
                        }
                    }

                    throw resultEx;
                }
            }