diff --git a/src/Android/MainActivity.cs b/src/Android/MainActivity.cs index 56c90fb54..965707c2b 100644 --- a/src/Android/MainActivity.cs +++ b/src/Android/MainActivity.cs @@ -12,6 +12,7 @@ using Plugin.Fingerprint.Abstractions; using Plugin.Settings.Abstractions; using Plugin.Connectivity.Abstractions; using Acr.UserDialogs; +using PushNotification.Plugin.Abstractions; namespace Bit.Android { @@ -32,7 +33,8 @@ namespace Bit.Android Resolver.Resolve(), Resolver.Resolve(), Resolver.Resolve(), - Resolver.Resolve())); + Resolver.Resolve(), + Resolver.Resolve())); } protected override void OnPause() diff --git a/src/App/App.cs b/src/App/App.cs index e6d470ab2..6bb139a38 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -13,6 +13,7 @@ using Bit.App.Controls; using Plugin.Connectivity.Abstractions; using System.Net; using Acr.UserDialogs; +using PushNotification.Plugin.Abstractions; namespace Bit.App { @@ -25,6 +26,7 @@ namespace Bit.App private readonly IAuthService _authService; private readonly IFingerprint _fingerprint; private readonly ISettings _settings; + private readonly IPushNotification _pushNotification; public App( IAuthService authService, @@ -33,7 +35,8 @@ namespace Bit.App IDatabaseService databaseService, ISyncService syncService, IFingerprint fingerprint, - ISettings settings) + ISettings settings, + IPushNotification pushNotification) { _databaseService = databaseService; _connectivity = connectivity; @@ -42,6 +45,7 @@ namespace Bit.App _authService = authService; _fingerprint = fingerprint; _settings = settings; + _pushNotification = pushNotification; SetStyles(); @@ -64,6 +68,11 @@ namespace Bit.App { await CheckLockAsync(args); }); + + MessagingCenter.Subscribe(Current, "Logout", (sender, args) => + { + Logout(args); + }); } protected async override void OnStart() @@ -166,6 +175,20 @@ namespace Bit.App } } + private void Logout(string logoutMessage) + { + _authService.LogOut(); + Device.BeginInvokeOnMainThread(() => + { + _pushNotification.Unregister(); + }); + Current.MainPage = new HomePage(); + if(!string.IsNullOrWhiteSpace(logoutMessage)) + { + _userDialogs.WarnToast("Logged out", logoutMessage); + } + } + private async Task CheckLockAsync(bool forceLock) { // Only lock if they are logged in diff --git a/src/App/Pages/Lock/LockFingerprintPage.cs b/src/App/Pages/Lock/LockFingerprintPage.cs index 0436d0da8..f5fecff72 100644 --- a/src/App/Pages/Lock/LockFingerprintPage.cs +++ b/src/App/Pages/Lock/LockFingerprintPage.cs @@ -14,7 +14,6 @@ namespace Bit.App.Pages public class LockFingerprintPage : ExtendedContentPage { private readonly IFingerprint _fingerprint; - private readonly IAuthService _authService; private readonly IUserDialogs _userDialogs; private readonly ISettings _settings; private readonly bool _checkFingerprintImmediately; @@ -24,7 +23,6 @@ namespace Bit.App.Pages { _checkFingerprintImmediately = checkFingerprintImmediately; _fingerprint = Resolver.Resolve(); - _authService = Resolver.Resolve(); _userDialogs = Resolver.Resolve(); _settings = Resolver.Resolve(); @@ -79,9 +77,7 @@ namespace Bit.App.Pages return; } - _authService.LogOut(); - await Navigation.PopModalAsync(); - Application.Current.MainPage = new HomePage(); + MessagingCenter.Send(Application.Current, "Logout", (string)null); } public async Task CheckFingerprintAsync() diff --git a/src/App/Pages/Lock/LockPasswordPage.cs b/src/App/Pages/Lock/LockPasswordPage.cs index 0ade4b043..88809241d 100644 --- a/src/App/Pages/Lock/LockPasswordPage.cs +++ b/src/App/Pages/Lock/LockPasswordPage.cs @@ -123,9 +123,7 @@ namespace Bit.App.Pages return; } - _authService.LogOut(); - await Navigation.PopModalAsync(); - Application.Current.MainPage = new HomePage(); + MessagingCenter.Send(Application.Current, "Logout", (string)null); } } } diff --git a/src/App/Pages/Lock/LockPinPage.cs b/src/App/Pages/Lock/LockPinPage.cs index ed438fd82..049a2782e 100644 --- a/src/App/Pages/Lock/LockPinPage.cs +++ b/src/App/Pages/Lock/LockPinPage.cs @@ -102,9 +102,7 @@ namespace Bit.App.Pages return; } - _authService.LogOut(); - await Navigation.PopModalAsync(); - Application.Current.MainPage = new HomePage(); + MessagingCenter.Send(Application.Current, "Logout", (string)null); } } } diff --git a/src/App/Pages/Settings/SettingsAddFolderPage.cs b/src/App/Pages/Settings/SettingsAddFolderPage.cs index ffeafbb1e..0c582ca0f 100644 --- a/src/App/Pages/Settings/SettingsAddFolderPage.cs +++ b/src/App/Pages/Settings/SettingsAddFolderPage.cs @@ -7,6 +7,7 @@ using Bit.App.Resources; using Plugin.Connectivity.Abstractions; using Xamarin.Forms; using XLabs.Ioc; +using System.Linq; namespace Bit.App.Pages { @@ -74,8 +75,16 @@ namespace Bit.App.Pages await saveTask; _userDialogs.HideLoading(); - await Navigation.PopModalAsync(); - _userDialogs.SuccessToast(nameCell.Entry.Text, "New folder created."); + + if(saveTask.Result.Succeeded) + { + await Navigation.PopModalAsync(); + _userDialogs.SuccessToast(nameCell.Entry.Text, "New folder created."); + } + else if(saveTask.Result.Errors.Count() > 0) + { + await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); + } }, ToolbarItemOrder.Default, 0); Title = "Add Folder"; diff --git a/src/App/Pages/Settings/SettingsEditFolderPage.cs b/src/App/Pages/Settings/SettingsEditFolderPage.cs index dd4c6635c..6e22f43fa 100644 --- a/src/App/Pages/Settings/SettingsEditFolderPage.cs +++ b/src/App/Pages/Settings/SettingsEditFolderPage.cs @@ -6,6 +6,7 @@ using Bit.App.Resources; using Plugin.Connectivity.Abstractions; using Xamarin.Forms; using XLabs.Ioc; +using System.Linq; namespace Bit.App.Pages { @@ -87,8 +88,16 @@ namespace Bit.App.Pages await saveTask; _userDialogs.HideLoading(); - await Navigation.PopModalAsync(); - _userDialogs.SuccessToast(nameCell.Entry.Text, "Folder updated."); + + if(saveTask.Result.Succeeded) + { + await Navigation.PopModalAsync(); + _userDialogs.SuccessToast(nameCell.Entry.Text, "Folder updated."); + } + else if(saveTask.Result.Errors.Count() > 0) + { + await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); + } }, ToolbarItemOrder.Default, 0); Title = "Edit Folder"; diff --git a/src/App/Pages/Settings/SettingsPage.cs b/src/App/Pages/Settings/SettingsPage.cs index 8867d984e..0e060fc03 100644 --- a/src/App/Pages/Settings/SettingsPage.cs +++ b/src/App/Pages/Settings/SettingsPage.cs @@ -129,7 +129,7 @@ namespace Bit.App.Pages } }; - if( Device.OS == TargetPlatform.iOS ) + if(Device.OS == TargetPlatform.iOS) { table.RowHeight = -1; table.EstimatedRowHeight = 44; @@ -199,9 +199,7 @@ namespace Bit.App.Pages return; } - _authService.LogOut(); - _pushNotification.Unregister(); - Application.Current.MainPage = new HomePage(); + MessagingCenter.Send(Application.Current, "Logout", (string)null); } private async void ChangeMasterPasswordCell_Tapped(object sender, EventArgs e) diff --git a/src/App/Pages/Vault/VaultAddSitePage.cs b/src/App/Pages/Vault/VaultAddSitePage.cs index fdc620439..da80e19b3 100644 --- a/src/App/Pages/Vault/VaultAddSitePage.cs +++ b/src/App/Pages/Vault/VaultAddSitePage.cs @@ -134,8 +134,15 @@ namespace Bit.App.Pages await saveTask; _userDialogs.HideLoading(); - await Navigation.PopModalAsync(); - _userDialogs.SuccessToast(nameCell.Entry.Text, "New site created."); + if(saveTask.Result.Succeeded) + { + await Navigation.PopModalAsync(); + _userDialogs.SuccessToast(nameCell.Entry.Text, "New site created."); + } + else if(saveTask.Result.Errors.Count() > 0) + { + await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); + } }, ToolbarItemOrder.Default, 0); Title = AppResources.AddSite; diff --git a/src/App/Pages/Vault/VaultEditSitePage.cs b/src/App/Pages/Vault/VaultEditSitePage.cs index 5e8236906..0636db62c 100644 --- a/src/App/Pages/Vault/VaultEditSitePage.cs +++ b/src/App/Pages/Vault/VaultEditSitePage.cs @@ -168,8 +168,16 @@ namespace Bit.App.Pages await saveTask; _userDialogs.HideLoading(); - await Navigation.PopModalAsync(); - _userDialogs.SuccessToast(nameCell.Entry.Text, "Site updated."); + + if(saveTask.Result.Succeeded) + { + await Navigation.PopModalAsync(); + _userDialogs.SuccessToast(nameCell.Entry.Text, "Site updated."); + } + else if(saveTask.Result.Errors.Count() > 0) + { + await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); + } }, ToolbarItemOrder.Default, 0); Title = "Edit Site"; diff --git a/src/App/Services/FolderService.cs b/src/App/Services/FolderService.cs index 945828ddf..10e5eb162 100644 --- a/src/App/Services/FolderService.cs +++ b/src/App/Services/FolderService.cs @@ -6,6 +6,7 @@ using Bit.App.Abstractions; using Bit.App.Models; using Bit.App.Models.Data; using Bit.App.Models.Api; +using Xamarin.Forms; namespace Bit.App.Services { @@ -71,6 +72,11 @@ namespace Bit.App.Services await _folderRepository.UpdateAsync(data); } } + else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden + || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) + { + MessagingCenter.Send(Application.Current, "Logout", (string)null); + } return response; } @@ -82,6 +88,11 @@ namespace Bit.App.Services { await _folderRepository.DeleteAsync(folderId); } + else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden + || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) + { + MessagingCenter.Send(Application.Current, "Logout", (string)null); + } return response; } diff --git a/src/App/Services/SiteService.cs b/src/App/Services/SiteService.cs index 4ff4b06ab..a9e07e3c7 100644 --- a/src/App/Services/SiteService.cs +++ b/src/App/Services/SiteService.cs @@ -6,6 +6,7 @@ using Bit.App.Abstractions; using Bit.App.Models; using Bit.App.Models.Api; using Bit.App.Models.Data; +using Xamarin.Forms; namespace Bit.App.Services { @@ -78,6 +79,11 @@ namespace Bit.App.Services await _siteRepository.UpdateAsync(data); } } + else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden + || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) + { + MessagingCenter.Send(Application.Current, "Logout", (string)null); + } return response; } @@ -89,6 +95,11 @@ namespace Bit.App.Services { await _siteRepository.DeleteAsync(id); } + else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden + || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) + { + MessagingCenter.Send(Application.Current, "Logout", (string)null); + } return response; } diff --git a/src/App/Services/SyncService.cs b/src/App/Services/SyncService.cs index 0ba8ebb17..19537c410 100644 --- a/src/App/Services/SyncService.cs +++ b/src/App/Services/SyncService.cs @@ -56,6 +56,13 @@ namespace Bit.App.Services if(!cipher.Succeeded) { SyncCompleted(false); + + if(cipher.StatusCode == System.Net.HttpStatusCode.Forbidden + || cipher.StatusCode == System.Net.HttpStatusCode.Unauthorized) + { + MessagingCenter.Send(Application.Current, "Logout", (string)null); + } + return false; } @@ -136,6 +143,13 @@ namespace Bit.App.Services if(!ciphers.Succeeded) { SyncCompleted(false); + + if(ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden + || ciphers.StatusCode == System.Net.HttpStatusCode.Unauthorized) + { + MessagingCenter.Send(Application.Current, "Logout", (string)null); + } + return false; } @@ -174,6 +188,13 @@ namespace Bit.App.Services if(!ciphers.Succeeded) { SyncCompleted(false); + + if(ciphers.StatusCode == System.Net.HttpStatusCode.Forbidden + || ciphers.StatusCode == System.Net.HttpStatusCode.Unauthorized) + { + MessagingCenter.Send(Application.Current, "Logout", (string)null); + } + return false; } diff --git a/src/iOS/AppDelegate.cs b/src/iOS/AppDelegate.cs index 4735b1892..35e01398f 100644 --- a/src/iOS/AppDelegate.cs +++ b/src/iOS/AppDelegate.cs @@ -24,6 +24,7 @@ using PushNotification.Plugin; using Plugin.DeviceInfo; using Plugin.Connectivity.Abstractions; using Bit.App.Pages; +using PushNotification.Plugin.Abstractions; namespace Bit.iOS { @@ -48,7 +49,8 @@ namespace Bit.iOS Resolver.Resolve(), Resolver.Resolve(), Resolver.Resolve(), - Resolver.Resolve())); + Resolver.Resolve(), + Resolver.Resolve())); // Appearance stuff