private LoginRequest GetLoginInfo()
		{
			var info = new LoginRequest();
			info.Email = NewAccount.Email;
			info.Password = NewAccount.Password;
			return info;
		}
		public override async void ViewDidLoad()
		{
			base.ViewDidLoad();

			View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();

			View.AddConstraints(
				LogoImage.WithSameCenterX(View),
				LogoImage.AtTopOf(View, 50),
				LogoImage.AtLeftOf(View, 20),
				LogoImage.AtRightOf(View, 20),
				LogoImage.AtBottomOf(View, 250),

				LoginButton.WithSameWidth(View),
				//LoginButton.AtTopOf(View, 350),
				//LoginButton.AtBottomOf(View, 400),

				SignUpButton.WithSameWidth(View)
				//SignUpButton.AtTopOf(View, 480),
				//SignUpButton.AtBottomOf(View, 550)
			);

			var creds = KeychainUtil.GetCredential();
			if (creds.Success)
			{
				if (RestAPI.Session == null)
				{
					var loginRequest = new LoginRequest()
					{
						Email = creds.Value.Username,
						Password = creds.Value.Password
					};
					LoginResult result;
					using (new NetworkActivityUtil())
						result = await AccountManager.Login(loginRequest);
					if (!result.Success)
					{
						// Login request failed, so don't do anything. User will have to sign in or up.
						Console.WriteLine("Authetication failed with credentials in keychain.");
						KeychainUtil.ClearCredential();
						RestAPI.Session = null;
						Database.DB.ClearDatabase();
						return;
					}
					RestAPI.Session = result.Session;
					KeychainUtil.StoreCredential(Credential.Create(loginRequest.Email, loginRequest.Password));
				}
				PerformSegue("SegueWithCredentials", this);
			}
		}
		public static ValidationResult ValidateLogin(LoginRequest loginReq) {
			List<string> invalidThings = new List<string> ();

			if (!StringIsValid (loginReq.Email)) {
				invalidThings.Add ("enter valid email");
			}

			if (!StringIsValid(loginReq.Password) || loginReq.Password.Length < MINIMUM_PASSWORD_LENGTH) {
				invalidThings.Add ("password must be " + MINIMUM_PASSWORD_LENGTH + " or more characters");
			}

			if (invalidThings.Count == 0) {
				return ValidationResult.ValidAccount ();
			} else {
				string msg = string.Join (", ", invalidThings);
				return ValidationResult.InvalidAccount (msg);
			}
		}
		public async void Login_BadPassword() {
			var loginRequest = new LoginRequest ();
			loginRequest.Email = "*****@*****.**";
			loginRequest.Password = Guid.NewGuid ().ToString ();
			var result = await AccountManager.Login (loginRequest);

			result.Success.Should ().BeFalse ();
			result.Session.Should ().BeNull ();
		}
		public async void Login_BadEmail() {
			var loginRequest = new LoginRequest ();
			loginRequest.Email = "test-" + RandomInt () + "@fake-email-address-here.com";
			loginRequest.Password = "******";
			var result = await AccountManager.Login (loginRequest);

			result.Success.Should ().BeFalse ();
			result.Session.Should ().BeNull ();
		}
		public async void Login() {
			var loginRequest = new LoginRequest ();
			loginRequest.Email = "*****@*****.**";
			loginRequest.Password = "******";
			var result = await AccountManager.Login (loginRequest);

			result.Success.Should ().BeTrue ();
			result.Session.Should ().NotBeNullOrWhiteSpace ();
		}
 private LoginRequest LoginFromForm()
 {
     var result = new LoginRequest();
     result.Email = LoginEmail.Text;
     result.Password = LoginPassword.Text;
     return result;
 }
		public static async Task<LoginResult> Login(LoginRequest loginRequest) {
			Debug.WriteLine ("Logging in user: {0}", loginRequest);

			// FIXME: check that both fields are set and valid. (thomasvandoren, 2016-02-22)

			var qs = RestUtil.QueryString (
				RestUtil.QueryParam("data[username]", loginRequest.Email),
				RestUtil.QueryParam("data[password]", loginRequest.Password),
				RestUtil.QueryParam("data[new_session]", "yes")
			);

			var url = RestUtil.GetUrl ("/webservice/rest/authenticate_username" + qs);

			var responseBody = await RestAPI.restUtil.GetRequest (url);
			if (!responseBody.isPresent ()) {
				return LoginResult.Failed ("Failed to retrieve login response body.");
			}

			var optionalResponse = RestUtil.ParseJson<LoginResponse> (responseBody.get());
			if (!optionalResponse.isPresent ()) {
				return LoginResult.Failed (string.Format("Failed to parse Login JSON response body: {0}", responseBody.get()));
			}
			var response = optionalResponse.get ();
			Debug.WriteLine ("Login response: {0}", response);

			if (!response.Success) {
				Debug.WriteLine ("Login failed with message: {0}", response.Message);
				return LoginResult.Failed (response.Message);
			}

			if (response.Data.Count < 1) {
				return LoginResult.Failed ("Login response did not contain anything in data field.");
			}

			return LoginResult.Succeeded (response.Data [0].SessionId);
		}