Ejemplo n.º 1
0
        public API_User Login(Data.LoginUser loginUser)
        {
            RestRequest request = new RestRequest(API_BASE_URL + "login");

            request.AddJsonBody(loginUser);
            IRestResponse <API_User> response = client.Post <API_User>(request);

            if (response.ResponseStatus != ResponseStatus.Completed)
            {
                Console.WriteLine("An error occurred communicating with the server.");
                return(null);
            }
            else if (!response.IsSuccessful)
            {
                if (!string.IsNullOrWhiteSpace(response.Data.Message))
                {
                    Console.WriteLine("An error message was received: " + response.Data.Message);
                }
                else
                {
                    Console.WriteLine("An error response was received from the server. The status code is " + (int)response.StatusCode);
                }
                return(null);
            }
            else
            {
                client.Authenticator = new JwtAuthenticator(response.Data.Token);
                return(response.Data);
            }
        }
Ejemplo n.º 2
0
        //login endpoints
        public bool Register(Data.LoginUser registerUser)
        {
            RestRequest request = new RestRequest(API_BASE_URL + "login/register");

            request.AddJsonBody(registerUser);
            IRestResponse <API_User> response = client.Post <API_User>(request);

            if (response.ResponseStatus != ResponseStatus.Completed)
            {
                Console.WriteLine("An error occurred communicating with the server.");
                return(false);
            }
            else if (!response.IsSuccessful)
            {
                if (!string.IsNullOrWhiteSpace(response.Data.Message))
                {
                    Console.WriteLine("An error message was received: " + response.Data.Message);
                }
                else
                {
                    Console.WriteLine("An error response was received from the server. The status code is " + (int)response.StatusCode);
                }
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 3
0
        private static void Run()
        {
            int loginRegister = -1;

            while (loginRegister != 1 && loginRegister != 2)
            {
                Console.WriteLine("Welcome to TEnmo!");
                Console.WriteLine("1: Login");
                Console.WriteLine("2: Register");
                Console.Write("Please choose an option: ");

                if (!int.TryParse(Console.ReadLine(), out loginRegister))
                {
                    Console.WriteLine("Invalid input. Please enter only a number.");
                }
                else if (loginRegister == 1)
                {
                    while (!UserService.IsLoggedIn()) //will keep looping until user is logged in
                    {
                        Data.LoginUser loginUser = consoleService.PromptForLogin();
                        API_User       user      = authService.Login(loginUser);
                        if (user != null)
                        {
                            UserService.SetLogin(user);
                            loggedInUserId  = UserService.GetUserId();
                            loggedInAccount = authService.GetAccount(loggedInUserId);
                            userBalance     = loggedInAccount.balance;
                        }
                    }
                }
                else if (loginRegister == 2)
                {
                    bool isRegistered = false;
                    while (!isRegistered) //will keep looping until user is registered
                    {
                        Data.LoginUser registerUser = consoleService.PromptForLogin();
                        isRegistered = authService.Register(registerUser);
                        if (isRegistered)
                        {
                            Console.WriteLine("");
                            Console.WriteLine("Registration successful. You can now log in.");
                            loginRegister = -1; //reset outer loop to allow choice for login
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Invalid selection.");
                }
            }

            MenuSelection();
        }
        public Data.LoginUser PromptForLogin()
        {
            Console.Write("Username: "******"Password: ");

            Data.LoginUser loginUser = new Data.LoginUser
            {
                Username = username,
                Password = password
            };
            return(loginUser);
        }