Exemplo n.º 1
0
        public async Task <AuthResultModel> SocialAuthAsync(string email, LoginProvider loginProvider)
        {
            var customerProfileResult = await _customerProfileClient.CustomerProfiles.GetByEmailAsync(
                new GetByEmailRequestModel { Email = email, IncludeNotVerified = true, IncludeNotActive = true });

            if (customerProfileResult.ErrorCode == CustomerProfileErrorCodes.CustomerProfileDoesNotExist)
            {
                return new AuthResultModel {
                           Error = ServicesError.LoginNotFound
                }
            }
            ;

            var customerProfileLoginProvider = (CustomerProfile.Client.Models.Enums.LoginProvider)loginProvider;

            if (!customerProfileResult.Profile.LoginProviders.Contains(customerProfileLoginProvider))
            {
                return new AuthResultModel {
                           Error = ServicesError.LoginExistsWithDifferentProvider
                }
            }
            ;

            if (customerProfileResult.Profile.Status != CustomerProfileStatus.Active)
            {
                return new AuthResultModel {
                           Error = ServicesError.CustomerProfileDeactivated
                }
            }
            ;

            var customerId = customerProfileResult.Profile.CustomerId;

            if (await IsCustomerBlockedAsync(customerId))
            {
                _log.Warning(nameof(SocialAuthAsync), "Attempt to social log in when blocked.", null, customerId);

                return(new AuthResultModel {
                    Error = ServicesError.CustomerBlocked
                });
            }

            var session = await _sessionsServiceClient.SessionsApi.AuthenticateAsync(customerId, new CreateSessionRequest());

            return(new AuthResultModel
            {
                CustomerId = customerId,
                Token = session.SessionToken,
            });
        }
Exemplo n.º 2
0
        public async Task <RegistrationResultModel> SocialRegisterAsync(RegistrationRequestDto request, LoginProvider loginProvider)
        {
            var isEmailAllowed = _emailRestrictionsService.IsEmailAllowed(request.Email);

            if (!isEmailAllowed)
            {
                return new RegistrationResultModel {
                           Error = ServicesError.EmailIsNotAllowed
                }
            }
            ;

            var(profileExists, errorResult) = await CheckIfProfileExists(request.Email);

            if (profileExists)
            {
                return new RegistrationResultModel {
                           Error = errorResult
                }
            }
            ;

            var customerId = Guid.NewGuid().ToString();
            var customerProfileLoginProvider = (CPLoginProvider)loginProvider;

            var customerProfileResult = await CreateCustomerProfileAsync(request, customerId, customerProfileLoginProvider);

            if (customerProfileResult == CustomerProfileErrorCodes.InvalidCountryOfNationalityId)
            {
                return new RegistrationResultModel {
                           Error = ServicesError.InvalidCountryOfNationalityId
                }
            }
            ;

            if (customerProfileResult == CustomerProfileErrorCodes.CustomerProfileAlreadyExistsWithDifferentProvider ||
                customerProfileResult == CustomerProfileErrorCodes.CustomerProfileAlreadyExists)
            {
                return new RegistrationResultModel {
                           Error = ServicesError.AlreadyRegistered
                }
            }
            ;

            await CreateCustomerWallet(customerId);

            if (!string.IsNullOrEmpty(request.ReferralCode))
            {
                await _customersRegistrationReferralDataRepository.AddAsync(customerId, request.ReferralCode);
            }

            _log.Info(message: "Successfully social registered customer", context: customerId);

            return(new RegistrationResultModel {
                CustomerId = customerId
            });
        }