Example #1
0
        public async Task <IOutput> Handle(SignInInput request, CancellationToken cancellationToken)
        {
            var user = await _context.Users
                       .Include(x => x.RolesEntities)
                       .ThenInclude(x => x.Role)
                       .FirstOrDefaultAsync(x => x.Mail == request.Mail, cancellationToken);

            if (user is null)
            {
                _logger.LogInformation("User {name} was not found", request.Mail);
                return(ActionOutput.Error("Пользователь не найден"));
            }

            if (user.IsBanned == true)
            {
                _logger.LogInformation("User {name} tried to enter with ban", request.Mail);
                return(ActionOutput.Error("Пользователь забанен."));
            }

            var signInResult = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

            if (signInResult.Succeeded == false)
            {
                _logger.LogInformation("User {name} was not found", request.Mail);
                return(ActionOutput.Error("Пользователь не найден"));
            }

            _logger.LogInformation($"User {user} signed in");

            var identity = _dataProvider.GetIdentity(request.Mail);

            if (identity is null)
            {
                return(ActionOutput.Error("Пользователь не найден"));
            }

            return(ActionOutput.SuccessData(new { token = _dataProvider.GetJwtByIdentity(identity) }));
        }
Example #2
0
        public async Task <IOutput> Handle(CheckInput request, CancellationToken cancellationToken)
        {
            var user = await _currentUserProvider.GetCurrentUser();

            if (user is null)
            {
                return(ActionOutput.Error("Вы не авторизованы"));
            }

            if (user.IsBanned == true)
            {
                _logger.LogInformation("User {name} tried to enter with ban", user.Mail);
                return(ActionOutput.Error("Пользователь забанен."));
            }

            var identity = _dataProvider.GetIdentity(user.Mail);

            if (identity is null)
            {
                return(ActionOutput.Error("Пользователь не найден"));
            }

            return(ActionOutput.SuccessData(new { token = _dataProvider.GetJwtByIdentity(identity) }));
        }
Example #3
0
        public async Task <IOutput> Handle(SignUpInput request, CancellationToken cancellationToken)
        {
            var hasSameNick = await _context.Users
                              .AnyAsync(x => x.Nick == request.Nick, cancellationToken : cancellationToken);

            if (hasSameNick)
            {
                return(ActionOutput.Error("Пользователь с таким ником уже зарегистрирован"));
            }

            var user = new Entity.User(request.Mail)
            {
                Nick = request.Nick
            };
            var registerResult = await _userManager.CreateAsync(user);

            if (registerResult.Succeeded == false)
            {
                return(ActionOutput.Error("Такой пользователь уже есть"));
            }

            await _context.SaveChangesAsync(cancellationToken);

            using var unit = _context.CreateUnitOfWork();

            var userPhotoPath = _configuration.GetSection("Static:DefaultUserPhoto").Value;

            if (request.UserPhoto != null)
            {
                var fileSaveResult = await _fileUploader.SaveFile(request.UserPhoto);

                if (fileSaveResult.Succeeded == false)
                {
                    return(ActionOutput.Error("Что-то пошло не так"));
                }
                var filePath        = fileSaveResult.Data.OperatedFilePath;
                var filePathRelated = fileSaveResult.Data.OperatedFileRelatedPath;
                var fileEntity      = new AppFile(request.UserPhoto.FileName, filePath, filePathRelated)
                {
                    UserId = user.Id,
                };
                userPhotoPath = filePathRelated;
                _context.AppFiles.Add(fileEntity);
            }

            user.Photo = userPhotoPath;

            await _userManager.AddToRoleAsync(user, UserRoles.Participant.ToString());

            await _userManager.AddPasswordAsync(user, request.Password);

            _logger.LogInformation($"User {user} was registered");

            await _context.SaveChangesAsync(cancellationToken);

            await unit.Apply();

            var identity = _dataProvider.GetIdentity(request.Mail);

            if (identity is null)
            {
                return(ActionOutput.Error("Данные не верны"));
            }

            return(ActionOutput.SuccessData(new { token = _dataProvider.GetJwtByIdentity(identity) }));
        }