Exemplo n.º 1
0
        public IActionResult RefreshToken([FromBody] JToken jsonBody)
        {
            var response = new LoginResponse {
                Result = ResultType.Error
            };

            var refreshTokenValue = jsonBody.Value <string>("refreshToken");

            if (string.IsNullOrWhiteSpace(refreshTokenValue))
            {
                response.Messages.Add(_localizationService.GetResource("account.token.refreshtoken.nullrefreshtoken"));
                return(BadRequest(response));
            }

            var token = _tokenStoreService.FindToken(refreshTokenValue);

            if (token == null)
            {
                response.Messages.Add(_localizationService.GetResource("account.token.refreshtoken.nulltoken"));
                return(Unauthorized(response));
            }


            var jwtToken = _tokenFactoryService.CreateJwtTokens(token.Customer);

            _tokenStoreService.AddCustomerToken(token.Customer, jwtToken.RefreshTokenSerial, jwtToken.AccessToken, _tokenFactoryService.GetRefreshTokenSerial(refreshTokenValue));

            response.Result       = ResultType.Success;
            response.AccessToken  = jwtToken.AccessToken;
            response.RefreshToken = jwtToken.RefreshToken;

            return(Ok(response));
        }
        public IActionResult Login([FromBody] LoginRequest model)
        {
            var response = new LoginResponse {
                Result = ResultType.Error
            };

            if (ModelState.IsValid)
            {
                model.Mobile = model.Mobile;
                var loginResult = _customerRegistrationService.ValidateCustomer(model.Mobile, model.Password);

                switch (loginResult)
                {
                case CustomerLoginResults.Successful:
                {
                    var customer = _customerService.GetCustomerByPhone(model.Mobile);

                    //sign in new customer
                    var token = _tokenFactoryService.CreateJwtTokens(customer);
                    _tokenStoreService.AddCustomerToken(customer, token.RefreshTokenSerial, token.AccessToken, null);

                    //raise event
                    _eventPublisher.Publish(new CustomerLoggedinEvent(customer));

                    response.Result       = ResultType.Success;
                    response.AccessToken  = token.AccessToken;
                    response.RefreshToken = token.RefreshToken;

                    return(Ok(response));
                }

                case CustomerLoginResults.CustomerNotExist:
                    response.Messages.Add(
                        _localizationService.GetResource("account.login.wrongcredentials.customernotexist"));
                    break;

                case CustomerLoginResults.Deleted:
                    response.Messages.Add(
                        _localizationService.GetResource("account.login.wrongcredentials.deleted"));
                    break;

                case CustomerLoginResults.NotActive:
                    response.Messages.Add(
                        _localizationService.GetResource("account.login.wrongcredentials.notactive"));
                    break;

                case CustomerLoginResults.NotRegistered:
                    response.Messages.Add(
                        _localizationService.GetResource("account.login.wrongcredentials.notregistered"));
                    break;

                case CustomerLoginResults.LockedOut:
                    response.Messages.Add(
                        _localizationService.GetResource("account.login.wrongcredentials.lockedout"));
                    break;

                case CustomerLoginResults.WrongPassword:
                    response.Messages.Add(
                        _localizationService.GetResource("account.login.wrongcredentials.wrongcustomernameorpassword"));
                    break;

                default:
                    response.Messages.Add(
                        _localizationService.GetResource("account.login.wrongcredentials"));
                    break;
                }
            }
            response.Messages.AddRange(ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
            return(Unauthorized(response));
        }