Exemplo n.º 1
0
        //TODO: Simplified async event firing/handling
        /// <inheritdoc />
        protected override void OnEventFired(object source, EventArgs args)
        {
            //We should not do async OnEventFired because we will get silent failures.
            UnityExtended.UnityMainThreadContext.PostAsync(async() =>
            {
                JWTModel jwtModel = null;

                //TODO: Validate username and password
                //We can't do error code supression with refit anymore, so we have to do this crap.
                try
                {
                    jwtModel = await AuthService.TryAuthenticate(BuildAuthRequestModel())
                               .ConfigureAwait(false);
                }
                catch (ApiException e)
                {
                    jwtModel = e.GetContentAs <JWTModel>();

                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}");
                    }
                }
                finally
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug($"Auth Response for User: {UsernameText.Text} Result: {jwtModel?.isTokenValid} OptionalError: {jwtModel?.Error} OptionalErrorDescription: {jwtModel?.ErrorDescription}");
                    }

                    //Even if it's null, we should broadcast the event.
                    OnAuthenticationResultRecieved?.Invoke(this, new AuthenticationResultEventArgs(jwtModel));
                }
            });
        }
        protected override void OnEventFired(object source, EventArgs args)
        {
            Console.WriteLine($"Auth: {UserNameField.Text}:{PasswordField.Text}");
            Task.Factory.StartNew(async() =>
            {
                PlayerAccountJWTModel jwtModel = null;

                //TODO: Validate username and password
                //We can't do error code supression with refit anymore, so we have to do this crap.
                try
                {
                    jwtModel = await AuthService.TryAuthenticate(new AuthenticationRequestModel(UserNameField.Text, PasswordField.Text))
                               .ConfigureAwait(false);
                }
                catch (ApiException e)
                {
                    jwtModel = e.GetContentAs <PlayerAccountJWTModel>();

                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}");
                    }
                }
                catch (Exception e)
                {
                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}\n\nStack: {e.StackTrace}");
                    }
                }
                finally
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug($"Auth Response for User: {UserNameField.Text} Result: {jwtModel?.isTokenValid} OptionalError: {jwtModel?.Error} OptionalErrorDescription: {jwtModel?.ErrorDescription}");
                    }

                    if (jwtModel != null && jwtModel.isTokenValid)
                    {
                        AuthTokenRepository.Update(jwtModel.AccessToken);

                        GameQueueable.Enqueue(() =>
                        {
                            //Even if it's null, we should broadcast the event.
                            OnAuthenticationResultRecieved?.Invoke(this, new AuthenticationResultEventArgs(jwtModel));
                        });
                    }
                }
            });
        }
Exemplo n.º 3
0
        //TODO: Simplified async event firing/handling
        /// <inheritdoc />
        protected override void OnEventFired(object source, EventArgs args)
        {
            //We should not do async OnEventFired because we will get silent failures.
            UnityAsyncHelper.UnityMainThreadContext.PostAsync(async() =>
            {
                PlayerAccountJWTModel PlayerAccountJWTModel = null;

                //TODO: Validate username and password
                //We can't do error code supression with refit anymore, so we have to do this crap.
                try
                {
                    PlayerAccountJWTModel = await AuthService.TryAuthenticate(BuildAuthRequestModel())
                                            .ConfigureAwaitFalse();
                }
                catch (ApiException e)
                {
                    PlayerAccountJWTModel = await e.GetContentAsAsync <PlayerAccountJWTModel>();

                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}");
                    }

                    //failed and null response.
                    //Non-null response but failed.
                    ErrorPublisher.PublishEvent(this, new GeneralErrorEncounteredEventArgs("Login Failed", $"Error Code {(int)e.StatusCode}. Reason: {e.ReasonPhrase}. {e.Message}", null));
                }
                catch (Exception e)
                {
                    if (Logger.IsErrorEnabled)
                    {
                        Logger.Error($"Encountered Auth Error: {e.Message}\n\nStack: {e.StackTrace}");
                    }

                    //failed and null response.
                    //Non-null response but failed.
                    ErrorPublisher.PublishEvent(this, new GeneralErrorEncounteredEventArgs("Login Failed", $"Reason: Unknown Server Error", null));
                }
                finally
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug($"Auth Response for User: {UsernameText.Text} Result: {PlayerAccountJWTModel?.isTokenValid} OptionalError: {PlayerAccountJWTModel?.Error} OptionalErrorDescription: {PlayerAccountJWTModel?.ErrorDescription}");
                    }

                    //Even if it's null, we should broadcast the event.
                    OnAuthenticationResultRecieved?.Invoke(this, new AuthenticationResultEventArgs(PlayerAccountJWTModel));
                }
            });
        }
 public void DispatchAuthenticationResult(PlayerAccountJWTModel model)
 {
     OnAuthenticationResultRecieved?.Invoke(this, new AuthenticationResultEventArgs(model));
 }