2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Bit.App.Abstractions;
|
2016-06-22 06:54:51 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
using Bit.App.Resources;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-07-30 20:28:13 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-08-21 02:19:10 +03:00
|
|
|
|
using PushNotification.Plugin.Abstractions;
|
2017-05-30 21:13:53 +03:00
|
|
|
|
using Bit.App.Utilities;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
namespace Bit.App.Pages
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-28 02:53:31 +03:00
|
|
|
|
public class LoginPage : ExtendedContentPage
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private IAuthService _authService;
|
|
|
|
|
private IUserDialogs _userDialogs;
|
2016-07-01 03:08:34 +03:00
|
|
|
|
private ISyncService _syncService;
|
2016-07-30 20:28:13 +03:00
|
|
|
|
private ISettings _settings;
|
2016-08-04 07:06:09 +03:00
|
|
|
|
private IGoogleAnalyticsService _googleAnalyticsService;
|
2016-08-21 02:19:10 +03:00
|
|
|
|
private IPushNotification _pushNotification;
|
2016-08-06 18:57:05 +03:00
|
|
|
|
private readonly string _email;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
|
2016-08-06 18:57:05 +03:00
|
|
|
|
public LoginPage(string email = null)
|
2016-08-09 02:00:36 +03:00
|
|
|
|
: base(updateActivity: false)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-08-06 18:57:05 +03:00
|
|
|
|
_email = email;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_authService = Resolver.Resolve<IAuthService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
2016-07-01 03:08:34 +03:00
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
2016-07-30 20:28:13 +03:00
|
|
|
|
_settings = Resolver.Resolve<ISettings>();
|
2016-08-04 07:06:09 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-08-21 02:19:10 +03:00
|
|
|
|
_pushNotification = Resolver.Resolve<IPushNotification>();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Init();
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
public FormEntryCell PasswordCell { get; set; }
|
|
|
|
|
public FormEntryCell EmailCell { get; set; }
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2016-07-23 22:32:11 +03:00
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
|
|
|
|
|
|
2017-05-30 21:13:53 +03:00
|
|
|
|
var padding = Helpers.OnPlatform(
|
2016-08-14 04:43:15 +03:00
|
|
|
|
iOS: new Thickness(15, 20),
|
|
|
|
|
Android: new Thickness(15, 8),
|
|
|
|
|
WinPhone: new Thickness(15, 20));
|
2016-07-03 07:27:10 +03:00
|
|
|
|
|
2016-11-09 05:29:24 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.MasterPassword, isPassword: true,
|
2016-07-03 07:27:10 +03:00
|
|
|
|
useLabelAsPlaceholder: true, imageSource: "lock", containerPadding: padding);
|
2016-06-28 05:17:49 +03:00
|
|
|
|
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
|
2016-07-03 07:27:10 +03:00
|
|
|
|
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope",
|
|
|
|
|
containerPadding: padding);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var lastLoginEmail = _settings.GetValueOrDefault(Constants.LastLoginEmail, string.Empty);
|
2016-08-06 18:57:05 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(_email))
|
|
|
|
|
{
|
|
|
|
|
EmailCell.Entry.Text = _email;
|
|
|
|
|
}
|
|
|
|
|
else if(!string.IsNullOrWhiteSpace(lastLoginEmail))
|
2016-07-30 20:28:13 +03:00
|
|
|
|
{
|
|
|
|
|
EmailCell.Entry.Text = lastLoginEmail;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
PasswordCell.Entry.ReturnType = Enums.ReturnType.Go;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
var table = new ExtendedTableView
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Intent = TableIntent.Settings,
|
2016-07-23 07:40:17 +03:00
|
|
|
|
EnableScrolling = false,
|
2016-06-24 06:03:00 +03:00
|
|
|
|
HasUnevenRows = true,
|
2016-07-23 07:40:17 +03:00
|
|
|
|
EnableSelection = true,
|
|
|
|
|
NoFooter = true,
|
2017-04-20 21:18:16 +03:00
|
|
|
|
//NoHeader = true,
|
2016-07-23 07:40:17 +03:00
|
|
|
|
VerticalOptions = LayoutOptions.Start,
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Root = new TableRoot
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2017-04-20 21:18:16 +03:00
|
|
|
|
new TableSection(" ")
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
EmailCell,
|
|
|
|
|
PasswordCell
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-08-18 07:20:21 +03:00
|
|
|
|
var forgotPasswordButton = new ExtendedButton
|
2016-06-24 06:03:00 +03:00
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Text = AppResources.GetPasswordHint,
|
2016-07-23 07:40:17 +03:00
|
|
|
|
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
|
|
|
|
|
Margin = new Thickness(15, 0, 15, 25),
|
2016-08-18 07:20:21 +03:00
|
|
|
|
Command = new Command(async () => await ForgotPasswordAsync()),
|
|
|
|
|
Uppercase = false,
|
|
|
|
|
BackgroundColor = Color.Transparent
|
2016-07-23 07:40:17 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var layout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
Children = { table, forgotPasswordButton },
|
2017-05-30 21:13:53 +03:00
|
|
|
|
Spacing = Helpers.OnPlatform(iOS: 0, Android: 10, WinPhone: 0)
|
2016-07-23 07:40:17 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var scrollView = new ScrollView { Content = layout };
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2017-05-30 21:13:53 +03:00
|
|
|
|
if(Device.RuntimePlatform == Device.iOS)
|
2016-06-24 06:03:00 +03:00
|
|
|
|
{
|
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = 70;
|
2016-11-26 00:54:33 +03:00
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel, () =>
|
2016-07-23 22:32:11 +03:00
|
|
|
|
{
|
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", false);
|
|
|
|
|
}));
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-07-23 07:40:17 +03:00
|
|
|
|
var loginToolbarItem = new ToolbarItem(AppResources.LogIn, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
await LogIn();
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
ToolbarItems.Add(loginToolbarItem);
|
|
|
|
|
Title = AppResources.Bitwarden;
|
2016-07-23 07:40:17 +03:00
|
|
|
|
Content = scrollView;
|
2016-11-26 00:54:33 +03:00
|
|
|
|
NavigationPage.SetBackButtonTitle(this, AppResources.LogIn);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2017-02-15 08:28:05 +03:00
|
|
|
|
PasswordCell.InitEvents();
|
|
|
|
|
EmailCell.InitEvents();
|
|
|
|
|
|
|
|
|
|
PasswordCell.Entry.Completed += Entry_Completed;
|
2016-07-23 22:32:11 +03:00
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
|
2016-07-30 20:28:13 +03:00
|
|
|
|
|
2016-08-06 20:14:58 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(_email))
|
2016-07-30 20:28:13 +03:00
|
|
|
|
{
|
2016-08-06 20:14:58 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-19 07:27:37 +03:00
|
|
|
|
PasswordCell.Entry.FocusWithDelay();
|
2016-08-06 20:14:58 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-19 07:27:37 +03:00
|
|
|
|
EmailCell.Entry.FocusWithDelay();
|
2016-08-06 20:14:58 +03:00
|
|
|
|
}
|
2016-07-30 20:28:13 +03:00
|
|
|
|
}
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
|
|
|
|
PasswordCell.Dispose();
|
|
|
|
|
EmailCell.Dispose();
|
|
|
|
|
PasswordCell.Entry.Completed -= Entry_Completed;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 06:06:33 +03:00
|
|
|
|
private async void Entry_Completed(object sender, EventArgs e)
|
2016-06-24 06:03:00 +03:00
|
|
|
|
{
|
2016-06-24 06:06:33 +03:00
|
|
|
|
await LogIn();
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-07-23 22:32:11 +03:00
|
|
|
|
|
2016-07-23 07:40:17 +03:00
|
|
|
|
private async Task ForgotPasswordAsync()
|
|
|
|
|
{
|
|
|
|
|
await Navigation.PushAsync(new PasswordHintPage());
|
|
|
|
|
}
|
2016-05-03 00:50:16 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private async Task LogIn()
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
|
|
|
|
AppResources.EmailAddress), AppResources.Ok);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
|
|
|
|
AppResources.MasterPassword), AppResources.Ok);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.LoggingIn, MaskType.Black);
|
2017-04-20 05:04:43 +03:00
|
|
|
|
var result = await _authService.TokenPostAsync(EmailCell.Entry.Text, PasswordCell.Entry.Text);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
2017-04-20 05:04:43 +03:00
|
|
|
|
if(!result.Success)
|
2016-06-22 06:54:51 +03:00
|
|
|
|
{
|
2017-04-20 05:04:43 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, result.ErrorMessage, AppResources.Ok);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
return;
|
2016-06-22 06:54:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 23:35:29 +03:00
|
|
|
|
PasswordCell.Entry.Text = string.Empty;
|
|
|
|
|
|
2017-04-20 05:04:43 +03:00
|
|
|
|
if(result.TwoFactorRequired)
|
2017-02-06 17:39:07 +03:00
|
|
|
|
{
|
|
|
|
|
_googleAnalyticsService.TrackAppEvent("LoggedIn To Two-step");
|
2017-06-27 23:18:32 +03:00
|
|
|
|
await Navigation.PushAsync(new LoginTwoFactorPage(EmailCell.Entry.Text, result));
|
2017-02-06 17:39:07 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 07:32:37 +03:00
|
|
|
|
_googleAnalyticsService.TrackAppEvent("LoggedIn");
|
2016-06-24 06:03:00 +03:00
|
|
|
|
|
2017-05-30 21:13:53 +03:00
|
|
|
|
if(Device.RuntimePlatform == Device.Android)
|
2016-08-21 02:19:10 +03:00
|
|
|
|
{
|
|
|
|
|
_pushNotification.Register();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 07:40:24 +03:00
|
|
|
|
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
|
2017-02-06 17:39:07 +03:00
|
|
|
|
Application.Current.MainPage = new MainPage();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|