Ejemplo n.º 1
0
            protected override async Task <ValidationResult> OnValidate(Func <AppState> getState, LoginAction action, IMultiDispatcher dispatcher, CancellationToken cancellationToken)
            {
                dispatcher.DispatchImmediate(new LoginStartedAction());

                string authToken = "";
                List <ValidationDetails> validationDetails = new List <ValidationDetails>();

                if (string.IsNullOrWhiteSpace(action.UserName))
                {
                    validationDetails.Add(new ValidationDetails("UserName", "Username cannot be empty"));
                }
                else if (string.IsNullOrWhiteSpace(action.Password))
                {
                    validationDetails.Add(new ValidationDetails("Password", "Password cannot be empty"));
                }
                else
                {
                    var result = await _loginService.Login(action.UserName, action.Password);

                    if (!result.Success)
                    {
                        validationDetails.Add(new ValidationDetails("*", "Invalid username/password"));
                    }
                    authToken = result.Token;
                }

                if (validationDetails.Count > 0)
                {
                    return(ValidationResult.Failure(new ValidationException("One or more error occured while loging in.", validationDetails.AsEnumerable())));
                }

                dispatcher.DispatchImmediate(new LoginSuccessfulAction(action.UserName, authToken));
                return(ValidationResult.Success());
            }
Ejemplo n.º 2
0
            protected override async Task OnProcess(Func <AppState> getState, LoginAction action, IMultiDispatcher dispatcher, CancellationToken cancellationToken)
            {
                List <Friend> friends = new List <Friend>();

                try
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    dispatcher.DispatchImmediate(new LoadingFriendListAction());
                    var state = getState();

                    friends = await _friendService.GetFriends(state.UserName, state.AuthToken).AsCancelable(cancellationToken).ConfigureAwait(false);
                }
                finally
                {
                    dispatcher.Dispatch(new AddToFriendListAction(friends));
                }
            }