Exemplo n.º 1
0
        public async Task <RegisterResponse> Handle(RegisterRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.Email))
            {
                throw new DuplicateException("Email already exists", ErrorCodes.EmailExists);
            }

            if (await authValidationService.UsernameExists(request.Username))
            {
                throw new DuplicateException("Username already exists", ErrorCodes.UsernameExists);
            }

            var signUpResult = await authService.SignUp(request.Email, request.Password, request.Username);

            if (signUpResult != null)
            {
                var registerToken = signUpResult.User?.Tokens.SingleOrDefault(t => t.Code == signUpResult.Token) ??
                                    throw new TokenException("Register token was not created",
                                                             ErrorCodes.CannotGenerateToken);

                return(await emailSender.Send(EmailMessages.ActivationAccountEmail(signUpResult.User?.Email,
                                                                                   signUpResult.User?.Username,
                                                                                   $"{Configuration.GetValue<string>(AppSettingsKeys.ClientAddress)}account/confirm?userId={signUpResult.User?.Id}&token={registerToken.Code}"))
                    ? new RegisterResponse()
                    : throw new ServiceException("Some error occurred during sending an email message",
                                                 ErrorCodes.SendEmailFailed));
            }

            throw new AuthException("Error occurred during signing up");
        }
Exemplo n.º 2
0
        public async Task <SignUpResponse> Handle(SignUpRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.EmailExists(request.Email))
            {
                throw new DuplicateException("Email address already exists", ErrorCodes.EmailExists);
            }

            if (await authValidationService.UsernameExists(request.Username))
            {
                throw new DuplicateException("Username already exists", ErrorCodes.UsernameExists);
            }

            var user = await identityService.SignUp(request.Email, request.Username, request.Password);

            if (user != null)
            {
                await balanceService.CreateBalanceAccount(user.Id);

                var confirmAccountToken = await identityService.GenerateConfirmAccountToken(user);

                string callbackUrl = $"{Configuration.GetValue<string>(AppSettingsKeys.ClientAddress)}register/confirm?email={user.Email}&token={confirmAccountToken}";

                return(await emailSender.Send(EmailMessages.ActivationAccountEmail(user.Email, user.UserName, callbackUrl))
                    ? new SignUpResponse
                {
                    Token = confirmAccountToken, User = mapper.Map <UserAuthDto>(user)
                }

                    : throw new ServiceException("Account confirmation email sending failed"));
            }

            throw new AuthException("Error occurred during signing up");
        }
Exemplo n.º 3
0
        public async Task <ChangeUsernameResponse> Handle(ChangeUsernameRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.UsernameExists(request.NewUsername))
            {
                throw new DuplicateException("Username already exists", ErrorCodes.UsernameExists);
            }

            return(await profileService.ChangeUsername(request.NewUsername)
                ? new ChangeUsernameResponse()
                : throw new ProfileUpdateException("Username change failed"));
        }
Exemplo n.º 4
0
        public async Task <ChangeUsernameResponse> Handle(ChangeUsernameRequest request, CancellationToken cancellationToken)
        {
            if (await authValidationService.UsernameExists(request.NewUsername))
            {
                throw new DuplicateException("Username already exists", ErrorCodes.UsernameExists);
            }

            var usernameChanged = await accountManager.ChangeUsername(request.NewUsername);

            return(usernameChanged ? new ChangeUsernameResponse()
                : throw new ProfileUpdateException("Changing username failed"));
        }
Exemplo n.º 5
0
        public async Task <GetAuthValidationsResponse> Handle(GetAuthValidationsRequest request, CancellationToken cancellationToken)
        {
            bool isAvailable = request.AuthValidationType switch
            {
                AuthValidationType.Email => !await authValidationService.EmailExists(request.Content),
                AuthValidationType.Username => !await authValidationService.UsernameExists(request.Content),
                _ => false
            };

            return(new GetAuthValidationsResponse {
                IsAvailable = isAvailable
            });
        }
    }
Exemplo n.º 6
0
        public async Task <SignUpResponse> Handle(SignUpRequest request, CancellationToken cancellationToken)
        {
            if (!await captchaService.VerifyCaptcha(request.CaptchaResponse))
            {
                throw new CaptchaException();
            }

            if (await authValidationService.UsernameExists(request.Username) ||
                await authValidationService.EmailExists(request.Email))
            {
                throw new DuplicateException("Account already exists");
            }

            if (await serialService.SerialExists(request.Serial))
            {
                throw new DuplicateException("Serial already exists");
            }

            User referrer = default;

            if (!string.IsNullOrEmpty(request.Referrer))
            {
                referrer = await userService.FindUserByUsername(request.Referrer);
            }

            var response = await authService.SignUp(request.Username, request.Email, request.Password, request.Serial,
                                                    referrer == null? 0 : referrer.Id);

            var(encryptedToken, encryptedEmail) = (cryptoService.Encrypt(response.TokenCode),
                                                   cryptoService.Encrypt(response.User.Email));

            //TODO:Change it on ClientAddress
            string callbackUrl =
                $"{Configuration.GetValue<string>(AppSettingsKeys.ServerAddress)}api/auth/confirm?email={encryptedEmail}&token={encryptedToken}";

            var emailTemplate =
                (await emailTemplateGenerator.FindEmailTemplate(EmailTemplateDictionary.RegisterTemplate))
                .ReplaceParameters(new EmailTemplateParameter("{{username}}", response.User.Username),
                                   new EmailTemplateParameter("{{callbackUrl}}", callbackUrl));

            return(await emailSender.Send(EmailMessages.ActivationAccountEmail(response.User.Email, emailTemplate))
                ? (SignUpResponse) new SignUpResponse
            {
                TokenCode = encryptedToken, User = mapper.Map <UserAuthDto>(response.User)
            }

                   .LogInformation($"User {request.Username} with email {request.Email} signed up")
                : throw new ServiceException("Sending confirmation email failed"));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (await authValidationService.EmailExists(UserRegister.Email))
            {
                ModelState.TryAddModelError(ErrorCodes.EmailExists, "Email address already exists");
                return(Page());
            }

            if (await authValidationService.UsernameExists(UserRegister.Username))
            {
                ModelState.TryAddModelError(ErrorCodes.UsernameExists, "Username already exists");
                return(Page());
            }

            var authResult = await authService.SignUp(UserRegister.Email, UserRegister.Password, UserRegister.Username);

            if (authResult != null)
            {
                var emailSent = await emailSender.Send(Constants.ActivationAccountEmail(UserRegister.Email,
                                                                                        $"{Configuration.GetValue<string>(AppSettingsKeys.ServerAddress)}Auth/ConfirmAccount?userId={authResult.User.Id}&code={authResult.Token}"));

                if (emailSent)
                {
                    Alertify.Push($"Activation email was sent to: {UserRegister.Email}", AlertType.Info);
                    return(Page());
                }

                Alertify.Push("There was a problem during sending activation email", AlertType.Error);
            }

            return(Page());
        }