/// <summary> /// Log in to a MEGA account. /// </summary> public async void Login() { if (!await NetworkService.IsNetworkAvailableAsync(true)) { return; } // Reset the flag to store if the account has been blocked AccountService.IsAccountBlocked = false; SetWarning(false, string.Empty); SetInputState(); if (!CheckInputParameters()) { return; } var login = new LoginRequestListenerAsync(); login.IsWaiting += OnIsWaiting; this.ControlState = false; this.LoginButtonState = false; this.IsBusy = true; this.ProgressHeaderText = ResourceService.ProgressMessages.GetString("PM_LoginHeader"); this.ProgressText = ResourceService.ProgressMessages.GetString("PM_LoginSubHeader"); LoginResult result = await login.ExecuteAsync(() => this.MegaSdk.login(this.Email, this.Password, login)); if (result == LoginResult.MultiFactorAuthRequired) { await DialogService.ShowAsyncMultiFactorAuthCodeInputDialogAsync(async (string code) => { result = await login.ExecuteAsync(() => this.MegaSdk.multiFactorAuthLogin(this.Email, this.Password, code, login)); if (result == LoginResult.MultiFactorAuthInvalidCode) { DialogService.SetMultiFactorAuthCodeInputDialogWarningMessage(); return(false); } return(true); }); } // Set default error content var errorContent = ResourceService.AppMessages.GetString("AM_LoginFailed"); switch (result) { case LoginResult.Success: SettingsService.SaveSessionToLocker(this.Email, this.MegaSdk.dumpSession()); // Validate product subscription license on background thread Task.Run(() => LicenseService.ValidateLicensesAsync()); // Initialize the DB AppService.InitializeDatabase(); // Fetch nodes from MEGA var fetchNodesResult = await this.FetchNodes(); if (fetchNodesResult != FetchNodesResult.Success) { LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Fetch nodes failed."); if (!AccountService.IsAccountBlocked) { this.ShowFetchNodesFailedAlertDialog(); } return; } // Navigate to the main page to load the main application for the user NavigateService.Instance.Navigate(typeof(MainPage), true, NavigationObject.Create(this.GetType(), NavigationActionType.Login)); return; case LoginResult.UnassociatedEmailOrWrongPassword: errorContent = ResourceService.AppMessages.GetString("AM_WrongEmailPasswordLogin"); break; case LoginResult.TooManyLoginAttempts: // Too many failed login attempts. Wait one hour. errorContent = string.Format(ResourceService.AppMessages.GetString("AM_TooManyFailedLoginAttempts"), DateTime.Now.AddHours(1).ToString("HH:mm:ss")); break; case LoginResult.AccountNotConfirmed: errorContent = ResourceService.AppMessages.GetString("AM_AccountNotConfirmed"); break; case LoginResult.MultiFactorAuthRequired: case LoginResult.MultiFactorAuthInvalidCode: case LoginResult.Unknown: break; default: throw new ArgumentOutOfRangeException(); } this.ControlState = true; this.LoginButtonState = true; this.IsBusy = false; // Show error message SetWarning(true, errorContent); SetInputState(InputState.Warning, InputState.Warning); }
private async void ConfirmAccount(object obj) { SetWarning(false, string.Empty); this.EmailInputState = InputState.Normal; this.PasswordInputState = InputState.Normal; if (!NetworkService.HasInternetAccess(true)) { return; } if (!CheckInputParameters()) { return; } this.ProgressHeaderText = ResourceService.ProgressMessages.GetString("PM_ConfirmAccountHeader"); this.ProgressSubHeaderText = ResourceService.ProgressMessages.GetString("PM_ConfirmAccountSubHeader"); this.ProgressText = ResourceService.ProgressMessages.GetString("PM_Patient"); this.IsBusy = true; this.ControlState = false; this.ConfirmAccountButtonState = false; var confirm = new ConfirmAccountRequestListenerAsync(); var result = await confirm.ExecuteAsync(() => { SdkService.MegaSdk.confirmAccount(ConfirmLink, Password, confirm); }); this.ControlState = true; this.ConfirmAccountButtonState = true; this.IsBusy = false; string messageContent; switch (result) { case ConfirmAccountResult.Success: messageContent = ResourceService.AppMessages.GetString("AM_ConfirmAccountSucces"); break; case ConfirmAccountResult.WrongPassword: messageContent = ResourceService.AppMessages.GetString("AM_WrongPassword"); break; case ConfirmAccountResult.Unknown: messageContent = ResourceService.AppMessages.GetString("AM_ConfirmAccountFailed"); break; default: throw new ArgumentOutOfRangeException(); } await DialogService.ShowAlertAsync( ResourceService.UiResources.GetString("UI_ConfirmAccount"), messageContent); if (result != ConfirmAccountResult.Success) { return; } if (Convert.ToBoolean(SdkService.MegaSdk.isLoggedIn())) { SdkService.MegaSdk.logout(new LogOutRequestListener(false)); } var login = new LoginRequestListenerAsync(); login.IsWaiting += OnIsWaiting; this.ProgressHeaderText = ResourceService.ProgressMessages.GetString("PM_LoginHeader"); this.ProgressSubHeaderText = null; this.ProgressText = ResourceService.ProgressMessages.GetString("PM_LoginSubHeader"); this.IsBusy = true; this.ControlState = false; this.ConfirmAccountButtonState = false; var loginResult = await login.ExecuteAsync(() => { SdkService.MegaSdk.login(this.Email, this.Password, login); }); string errorContent; switch (loginResult) { case LoginResult.Success: SettingsService.SaveSessionToLocker(this.Email, SdkService.MegaSdk.dumpSession()); // Fetch nodes from MEGA var fetchNodesResult = await this.FetchNodes(); if (fetchNodesResult != FetchNodesResult.Success) { LogService.Log(MLogLevel.LOG_LEVEL_ERROR, "Fetch nodes failed."); this.ShowFetchNodesFailedAlertDialog(); NavigateService.Instance.Navigate(typeof(LoginAndCreateAccountPage), true); } else { // Navigate to the main page to load the main application for the user NavigateService.Instance.Navigate(typeof(MainPage), true, NavigationObject.Create(this.GetType(), NavigationActionType.Login)); } return; case LoginResult.UnassociatedEmailOrWrongPassword: errorContent = ResourceService.AppMessages.GetString("AM_WrongEmailPasswordLogin"); break; case LoginResult.TooManyLoginAttempts: // Too many failed login attempts. Wait one hour. errorContent = string.Format(ResourceService.AppMessages.GetString("AM_TooManyFailedLoginAttempts"), DateTime.Now.AddHours(1).ToString("HH:mm:ss")); break; case LoginResult.AccountNotConfirmed: errorContent = ResourceService.AppMessages.GetString("AM_AccountNotConfirmed"); break; case LoginResult.Unknown: errorContent = ResourceService.AppMessages.GetString("AM_LoginFailed"); break; default: throw new ArgumentOutOfRangeException(); } this.ControlState = true; this.ConfirmAccountButtonState = true; this.IsBusy = false; await DialogService.ShowAlertAsync( ResourceService.AppMessages.GetString("AM_LoginFailed_Title"), errorContent); NavigateService.Instance.Navigate(typeof(LoginAndCreateAccountPage), true); }
/// <summary> /// Log in to a MEGA account. /// </summary> public async void Login() { if (!CheckInputParameters()) { return; } this.ControlState = false; this.IsBusy = true; var login = new LoginRequestListenerAsync(); var result = await login.ExecuteAsync(() => this.MegaSdk.login(this.Email, this.Password, login)); if (result == LoginResult.MultiFactorAuthRequired) { await DialogService.ShowAsyncMultiFactorAuthCodeInputDialogAsync(async (string code) => { result = await login.ExecuteAsync(() => this.MegaSdk.multiFactorAuthLogin(this.Email, this.Password, code, login)); if (result == LoginResult.MultiFactorAuthInvalidCode) { DialogService.SetMultiFactorAuthCodeInputDialogWarningMessage(); return(false); } return(true); }); } if (_loginPage != null) { Deployment.Current.Dispatcher.BeginInvoke(() => _loginPage.SetApplicationBar(true)); } this.ControlState = true; this.IsBusy = false; // Set default error content var errorContent = string.Format(Resources.AppMessages.LoginFailed, login.ErrorString); switch (result) { case LoginResult.Success: SettingsService.SaveMegaLoginData(this.Email, this.MegaSdk.dumpSession()); // Validate product subscription license on background thread Task.Run(() => LicenseService.ValidateLicenses()); // Navigate to the main page to load the main application for the user Deployment.Current.Dispatcher.BeginInvoke(() => NavigateService.NavigateTo(typeof(MainPage), NavigationParameter.Login)); return; case LoginResult.UnassociatedEmailOrWrongPassword: errorContent = Resources.AppMessages.WrongEmailPasswordLogin; break; case LoginResult.TooManyLoginAttempts: // Too many failed login attempts. Wait one hour. errorContent = string.Format(Resources.AppMessages.AM_TooManyFailedLoginAttempts, DateTime.Now.AddHours(1).ToString("HH:mm:ss")); break; case LoginResult.AccountNotConfirmed: errorContent = Resources.AppMessages.AM_AccountNotConfirmed; break; case LoginResult.MultiFactorAuthRequired: case LoginResult.MultiFactorAuthInvalidCode: case LoginResult.Unknown: break; default: throw new ArgumentOutOfRangeException(); } // Show error message Deployment.Current.Dispatcher.BeginInvoke(() => { new CustomMessageDialog( AppMessages.LoginFailed_Title, errorContent, App.AppInformation, MessageDialogButtons.Ok).ShowDialog(); }); }