Пример #1
0
        private static async Task LoginAsync()
        {
            while (!HasToken)
            {
                string email;
                string password;
                int    loginOption = OptionSelection("Enter an option to log in:", LoginOptions);
                switch (loginOption)
                {
                case 1:
                    Console.Write("\nEmail: ");
                    email = Console.ReadLine();
                    Console.Write("Username: "******"Password: "******"\nLoading...");
                    API = new AIDungeon();
                    try
                    {
                        var response = await API.RegisterAsync(email, username, password);

                        if (response.Errors != null)
                        {
                            Console.Write($"En error occurred: {response.Errors[0].Message}");
                        }
                        else
                        {
                            API.Token = response.Data.CreateAccount.AccessToken;
                            Console.Write("Registered Successfully. You should check your E-Mail to verify your account.");
                            HasToken = true;
                        }
                    }
                    catch (HttpRequestException e)
                    {
                        Console.WriteLine($"An error occurred: {e.Message}");
                    }
                    await Task.Delay(5000);

                    break;

                case 2:
                    Console.Write("\nEmail: ");
                    email = Console.ReadLine();
                    Console.Write("Password: "******"\nLogging in...");
                    API = new AIDungeon();
                    LoginResponse loginResponse;
                    try
                    {
                        loginResponse = await API.LoginAsync(email, password);
                    }
                    catch (HttpRequestException e)
                    {
                        Console.WriteLine($"An error occurred: {e.Message}");
                    }
                    await Task.Delay(5000);

                    break;

                case 3:
                    string token;
                    while (true)
                    {
                        Console.Write("\nEnter a token (Enter 'r' to return): ");
                        token = Console.ReadLine()?.Trim();
                        if (string.IsNullOrWhiteSpace(token))
                        {
                            Console.WriteLine("You must enter a token.");
                        }
                        else if (token.ToLowerInvariant() == "r")
                        {
                            break;
                        }
                        else if (!Guid.TryParse(token, out _))
                        {
                            Console.WriteLine("Invalid token.");
                        }
                        else
                        {
                            API      = new AIDungeon(token);
                            HasToken = true;
                            break;
                        }
                    }
                    break;

                case 4:
                    Environment.Exit(0);
                    break;
                }

                // Create config file
                if (HasToken)
                {
                    UserConfig = new Config(API.Token, true);
                    SaveConfig();
                }
            }
        }