public GenericCommandResult Authenticate(AuthenticateUserCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Erro ao autenticar o usuário", command.Notifications));
            }

            var user = _userRepository.Authenticate(command.Email);

            if (user == null)
            {
                return(new GenericCommandResult(false, "Usuário inválido", command.Notifications));
            }

            var password = Password.VerifyPasswordHash(command.Password, user.PasswordHash, user.PasswordSalt);

            if (!password)
            {
                return(new GenericCommandResult(false, "Senha incorreta", Notifications));
            }

            var token = TokenService.GenerateToken(user.Id.ToString(), Settings.Secret, 6);

            user.Token = token;

            return(new GenericCommandResult(true, "Usuário autenticado com sucesso", (DTOs.User)user));
        }
        public ICommandResult Handle(AuthenticateUserCommand command)
        {
            command.Validate();
            if (Invalid)
            {
                return(new GenericCommandResult(false, "Erro ao autenticar o usuário", command.Notifications));
            }

            var userVerify = _userRepository.Authenticate(command.UserName);

            if (userVerify.Count() == 0)
            {
                return(new GenericCommandResult(false, "Usuário inválido", command.Notifications));
            }

            var user = userVerify.First();

            var password = Password.VerifyPasswordHash(command.Password, user.PasswordHash, user.PasswordSalt);

            if (!password)
            {
                return(new GenericCommandResult(false, "Senha incorreta", Notifications));
            }

            if (!_keyAccessRepository.ValidKeyAccess(user.KeyAccess.Key))
            {
                return(new GenericCommandResult(false, "Chave de acesso inválida", command.UserName));
            }

            AddNotifications(user);

            if (Invalid)
            {
                return(new GenericCommandResult(false, "Ocorreu um erro ao autenticar o usuário", command.Notifications));
            }

            var token = _tokenService.GenerateToken(user);

            user.AddToken(token);

            return(new GenericCommandResult(true, "Usuário cadastrado com sucesso", user));
        }
Exemplo n.º 3
0
        public async Task <ICommandResult> Handle(AuthenticateUserCommand command)
        {
            //validar command
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Ops, algo errado aconteceu!", command.Notifications));
            }

            //GetUser
            var user = await _repository.GetUserByEmail(command.Email);

            if (user == null)
            {
                return(new GenericCommandResult(false, "Usuário não encontrado", null));
            }

            if (user.Password.Pass != command.Password)
            {
                AddNotification("Password", "Senha incorreta");
            }

            AddNotifications(user);
            if (Invalid)
            {
                return(new GenericCommandResult(false, "Ops, algo errado aconteceu!", Notifications));
            }

            ///CriandoToken
            var token = _tokenService.GenerateToken(user);

            user.Password.HidePassword();

            //Retorna valores
            return(new GenericCommandResult(true, "Usuário autenticado", new { user, token }));
        }