public void OnAnotherUser(VOGController ctrl)
 {
     var authSelectData = new VOGStateAuthSelect.AuthSelectData();
     authSelectData.EnableCancel = false;
     ctrl.StartTransition(AuthSelectorScreen, authSelectData);
 }
    private void AuthenticatePrimary()
    {
        StoredAuthenticationKey userAuthKey = null;
        userAuthKey = GetStoredUserAuthenticationKey();

        if (userAuthKey != null && UserSelectorScreen != null)
        {
            //Show user selector screen if we have login credentials
            UserSelectorScreen.OnDataChanged(userAuthKey);
            StartTransition(UserSelectorScreen);
        }
        else
        {
            //Otherwise, show the authentication screen
            var authSelectData = new VOGStateAuthSelect.AuthSelectData();
            authSelectData.EnableCancel = false;
            AuthenticationSelectorScreen.OnDataChanged(authSelectData);
            StartTransition(AuthenticationSelectorScreen);
        }
    }
    public IEnumerator OnSignIn(VOGControllerAuth ctrl)
    {
        try
        {
            ctrl.DisableInput();

            var authSelectData = new VOGStateAuthSelect.AuthSelectData();
            authSelectData.EnableCancel = false;

            //Make sure we have UserAuth object
            if (UserAuth == null || string.IsNullOrEmpty(UserAuth.AuthKey))
            {
                ctrl.ShowMessageDialog("Doh!", "An error occurred during authentication. Please try again", () =>
                {
                    ctrl.StartTransition(AuthSelectorScreen, authSelectData);
                });
                yield break;
            }

            //Try to authenticate user
            ApiCall call = VostopiaClient.Authentication.BeginSignInAuthKey(UserAuth.AuthKey);
            IEnumerator e = call.Wait();
            while (e.MoveNext()) { yield return e.Current; }
            if (!VostopiaClient.Authentication.EndSignIn(call))
            {
                ctrl.ShowMessageDialog("Session Expired", "Your authentication session has expired. Please sign in again.", () =>
                {
                    ctrl.StartTransition(AuthSelectorScreen, authSelectData);
                });
                yield break;
            }

            //Continue to auth completed screen
            ctrl.AuthenticationCompleted();
        }
        finally
        {
            ctrl.EnableInput();
        }
    }
    /**
     * Authenticate when we're in the Linked Account mode. If we have a valid authentication
     * token in AuthenticationSettings, use it to automatically log in user. If we have an invalid
     * authentication token, force the same user to log in again. If we don't have any authentication token,
     * let the user login or create a new vostopia user.
     */
    public IEnumerator AuthenticateLinkedAccount()
    {
        StoredAuthenticationKey userAuthKey = null;
        if (!string.IsNullOrEmpty(AuthenticationSettings.AuthenticationToken))
        {
            try
            {
                userAuthKey = JObject.Parse(AuthenticationSettings.AuthenticationToken).ToObject<StoredAuthenticationKey>();
            }
            catch (System.Exception ex)
            {
                Debug.LogWarning("Unable to parse AuthenticationToken, " + ex.ToString());
            }
        }

        if (userAuthKey != null)
        {
            //If we have auth key, try to authenticate automatically
            ApiCall call = VostopiaClient.Authentication.BeginSignInAuthKey(userAuthKey.AuthKey);
            IEnumerator e = call.Wait();
            while (e.MoveNext()) { yield return e.Current; }
            if (VostopiaClient.Authentication.EndSignIn(call))
            {
                //authentication completed successfully, nothing more to do
                AuthenticationCompleted();
                yield break;
            }

            //If we failed to log in automatically, find the user and get him to supply the password
            call = VostopiaClient.Authentication.BeginFindUser(userAuthKey.UserName);
            e = call.Wait();
            while (e.MoveNext()) { yield return e.Current; }
            VostopiaUser user = VostopiaClient.Authentication.EndFindUser(call);
            if (user != null)
            {
                var passwordData = new VOGStateAuthPassword.PasswordData();
                passwordData.CancelOnBack = true;
                passwordData.User = user;
                StartTransition(PasswordScreen, passwordData);
                yield break;
            }
        }

        //Otherwise, show the authentication screen
        var authSelectData = new VOGStateAuthSelect.AuthSelectData();
        authSelectData.EnableCancel = true;
        StartTransition(AuthenticationSelectorScreen, authSelectData);
    }