public async Task <Unit> Handle(ForgetPasswordCommand request, CancellationToken cancellationToken)
        {
            var user = await UserManager.FindByEmailAsync(request.Email.EmailNormalize());

            if (user == null)
            {
                throw new CustomException(new Error
                {
                    ErrorType = ErrorType.EmailNotFound,
                    Message   = Localizer["EmailNotFound"]
                });
            }

            BaseUser baseUser =
                _context.BaseUsers.FirstOrDefault(u => u.Id == user.Id);

            baseUser.IsValidating = true;
            string validationCode = ConfirmEmailCodeGenerator.GenerateCode();

            baseUser.ValidationCode = validationCode;

            await _context.SaveChangesAsync(cancellationToken);

            _emailService.SendEmail(request.Email,
                                    "Farakhu", "ResetPassword", "ResetPassword",
                                    validationCode);

            return(Unit.Value);
        }
Exemplo n.º 2
0
        public async Task <SignInViewModel> Handle(SignInCommand request, CancellationToken cancellationToken)
        {
            var user = await _userManager.FindByEmailAsync(request.Logon.EmailNormalize());

            if (user == null)
            {
                throw new CustomException(new Error
                {
                    ErrorType = ErrorType.UserNotFound,
                    Message   = _localizer["UserNotFound"]
                });
            }
            if (!user.EmailConfirmed)
            {
                user.IsValidating = true;
                string validationCode = ConfirmEmailCodeGenerator.GenerateCode();
                user.ValidationCode = validationCode;

                await _userManager.UpdateAsync(user);

                await _context.SaveChangesAsync(cancellationToken);

                _emailService.SendEmail(request.Logon,
                                        "Farakhu", "EmailVerification", "EmailVerification",
                                        validationCode);
                return(null);
            }

            var result = await _signInManager.PasswordSignInAsync(user, request.Password, true, false);

            if (!result.Succeeded)
            {
                throw new CustomException(new Error
                {
                    ErrorType = ErrorType.InvalidInput,
                    Message   = _localizer["InvalidInput"]
                });
            }

            return(new SignInViewModel
            {
                ProfileDto = _mapper.Map <ProfileDto>(user)
            });
        }
        public async Task <SignUpViewModel> Handle(SignUpCommand request, CancellationToken cancellationToken)
        {
            var duplicateUser = await _userManager.FindByEmailAsync(request.Email.EmailNormalize());

            if (duplicateUser != null)
            {
                if (duplicateUser.EmailConfirmed)
                {
                    throw new CustomException(new Error
                    {
                        Message   = _localizer["DuplicateUser"],
                        ErrorType = ErrorType.DuplicateUser
                    });
                }
                //if somebody signed up and close the app after
                duplicateUser.IsValidating = true;
                string code = ConfirmEmailCodeGenerator.GenerateCode();
                duplicateUser.ValidationCode = code;

                await _context.SaveChangesAsync(cancellationToken);

                _emailService.SendEmail(request.Email,
                                        "Farakhu", "EmailVerification", "EmailVerification",
                                        code);

                return(new SignUpViewModel());
            }
            switch (request.UserType)
            {
            case UserType.Instructor:
                var duplicateInstructor = _context.Instructors.
                                          FirstOrDefault(i => i.InstructorId == request.Id);
                if (duplicateInstructor != null)
                {
                    throw new CustomException(new Error
                    {
                        Message   = _localizer["DuplicateUser"],
                        ErrorType = ErrorType.DuplicateUser
                    });
                }
                break;

            case UserType.Student:
                var duplicateStudent = _context.Students.
                                       FirstOrDefault(s => s.StudentId == request.Id);
                if (duplicateStudent != null)
                {
                    throw new CustomException(new Error
                    {
                        Message   = _localizer["DuplicateUser"],
                        ErrorType = ErrorType.DuplicateUser
                    });
                }
                break;
            }

            BaseUser user = null;

            switch (request.UserType)
            {
            case UserType.Instructor:
                user = new Instructor
                {
                    Email        = request.Email.EmailNormalize(),
                    FirstName    = request.FirstName,
                    LastName     = request.LastName,
                    InstructorId = request.Id
                };
                break;

            case UserType.Student:
                user = new Student
                {
                    Email     = request.Email.EmailNormalize(),
                    FirstName = request.FirstName,
                    LastName  = request.LastName,
                    StudentId = request.Id
                };
                break;
            }

            var avatar = await _context.Files.FirstOrDefaultAsync(a => a.Id == "smiley.png", cancellationToken);

            user.AvatarId = avatar.Id;


            user.IsValidating = true;
            string validationCode = ConfirmEmailCodeGenerator.GenerateCode();

            user.ValidationCode = validationCode;

            var result = await _userManager.CreateAsync(user, request.Password);

            if (!result.Succeeded)
            {
                throw new CustomException(new Error
                {
                    Message   = _localizer["Unexpected"],
                    ErrorType = ErrorType.Unexpected
                });
            }

            await _userManager.AddToRoleAsync(user, request.UserType.ToString().Normalize());



            await _context.SaveChangesAsync(cancellationToken);

            _emailService.SendEmail(request.Email,
                                    "Farakhu", "EmailVerification", "EmailVerification",
                                    validationCode);

            return(new SignUpViewModel());
        }