static async Task <string> GetTokenAsync(string[] args, bool createAccount = true) { GetArgument(args, "-token", out var token); string username = null, password = null; if (args.Contains("create") && createAccount) { Console.WriteLine("New Account"); username = WaitForInput("Username : "******"Password : "******"Registering..."); var res = await Client.RegisterAsync(username, password); if (res.Success) { Console.WriteLine("You've created new account \"" + username + "\""); } else { Console.WriteLine($"Failed to create account"); int i = 1; foreach (var msg in res.Messages) { Console.WriteLine($"Reason {i++} : {msg}"); } return(null); } } if (string.IsNullOrWhiteSpace(token)) { if (username == null) { GetArgument(args, "-u", out username); } if (string.IsNullOrWhiteSpace(username)) { username = WaitForInput("Username : "******"-p", out password); } if (string.IsNullOrWhiteSpace(password)) { password = WaitForInput("Password : "******"Failed to login. Make sure you've entered username or password correctly"); return(await GetTokenAsync(args, false)); } } return(token); }
private static async Task <WinofyClient> PrepareClientAsync(HomeConfig config) { var client = new WinofyClient(); if (!string.IsNullOrEmpty(config.AuthorizationToken)) { var result = await client.ValidateTokenAsync(config.Username, config.AuthorizationToken); if (!result.Valid) { config.AuthorizationToken = null; } } while (string.IsNullOrEmpty(config.AuthorizationToken)) { Console.Write("Username : "******"Password : "******"Failed to login. Please check your username and password"); } } client.SetAuthorizationToken(config.AuthorizationToken); if (string.IsNullOrEmpty(config.Device?.Id)) { Console.WriteLine("This device may not be registered"); Console.WriteLine("Tell us the device name and description"); Console.Write("Name : "); var name = Console.ReadLine(); Console.Write("Description : "); var desc = Console.ReadLine(); var device = new Device() { Name = name, Description = desc, Notification = true }; var result = await client.RegisterDeviceAsync(device, true); if (result.Success) { config.Device = device; } else { Console.WriteLine("Failed to register this device. See messages from the server"); Console.WriteLine(result.Message.ToString()); } } using (var sw = new StreamWriter(HomeConfig.ConfigPath)) { sw.Write(JsonConvert.SerializeObject(config, Formatting.Indented)); } return(client); }