1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-28 22:23:35 +00:00

Added register page and accounts repo. Switch to color instead of bg image.

This commit is contained in:
Kyle Spearrin
2016-06-25 20:54:17 -04:00
parent e7fef012b8
commit e38dbff152
14 changed files with 290 additions and 35 deletions

View File

@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using Bit.App.Models.Api;
namespace Bit.App.Abstractions
{
public interface IAccountsApiRepository
{
Task<ApiResult> PostRegisterAsync(RegisterRequest requestObj);
}
}

View File

@@ -42,8 +42,6 @@ namespace Bit.App
MainPage = new HomePage();
}
MainPage.BackgroundColor = Color.FromHex("ecf0f5");
MessagingCenter.Subscribe<Application, bool>(Current, "Lock", async (sender, args) =>
{
await CheckLockAsync(args);

View File

@@ -35,6 +35,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Abstractions\Repositories\IAccountsApiRepository.cs" />
<Compile Include="Abstractions\Repositories\IDeviceApiRepository.cs" />
<Compile Include="Abstractions\Services\IAppIdService.cs" />
<Compile Include="Abstractions\Services\IAuthService.cs" />
@@ -70,6 +71,7 @@
<Compile Include="Models\Api\Request\DeviceTokenRequest.cs" />
<Compile Include="Models\Api\Request\FolderRequest.cs" />
<Compile Include="Models\Api\Request\DeviceRequest.cs" />
<Compile Include="Models\Api\Request\RegisterRequest.cs" />
<Compile Include="Models\Api\Request\SiteRequest.cs" />
<Compile Include="Models\Api\Request\TokenRequest.cs" />
<Compile Include="Models\Api\Request\TokenTwoFactorRequest.cs" />
@@ -91,6 +93,7 @@
<Compile Include="Models\Site.cs" />
<Compile Include="Models\Page\VaultViewSitePageModel.cs" />
<Compile Include="Pages\HomePage.cs" />
<Compile Include="Pages\RegisterPage.cs" />
<Compile Include="Pages\SettingsPinPage.cs" />
<Compile Include="Pages\LockPinPage.cs" />
<Compile Include="Pages\MainPage.cs" />
@@ -102,6 +105,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Abstractions\Repositories\ISiteRepository.cs" />
<Compile Include="Repositories\ApiRepository.cs" />
<Compile Include="Repositories\AccountsApiRepository.cs" />
<Compile Include="Repositories\BaseApiRepository.cs" />
<Compile Include="Abstractions\Repositories\IApiRepository.cs" />
<Compile Include="Abstractions\Repositories\IFolderApiRepository.cs" />

View File

@@ -38,4 +38,38 @@ namespace Bit.App.Models.Api
return result;
}
}
public class ApiResult
{
private List<ApiError> m_errors = new List<ApiError>();
public bool Succeeded { get; private set; }
public IEnumerable<ApiError> Errors => m_errors;
public HttpStatusCode StatusCode { get; private set; }
public static ApiResult Success(HttpStatusCode statusCode)
{
return new ApiResult
{
Succeeded = true,
StatusCode = statusCode
};
}
public static ApiResult Failed(HttpStatusCode statusCode, params ApiError[] errors)
{
var result = new ApiResult
{
Succeeded = false,
StatusCode = statusCode
};
if(errors != null)
{
result.m_errors.AddRange(errors);
}
return result;
}
}
}

View File

@@ -0,0 +1,10 @@
namespace Bit.App.Models.Api
{
public class RegisterRequest
{
public string Name { get; set; }
public string Email { get; set; }
public string MasterPasswordHash { get; set; }
public string MasterPasswordHint { get; set; }
}
}

View File

@@ -25,7 +25,6 @@ namespace Bit.App.Pages
Init();
}
public void Init()
{
var logo = new Image
@@ -48,7 +47,7 @@ namespace Bit.App.Pages
var createAccountButton = new Button
{
Text = "Create Account",
//Command = new Command(async () => await RegisterAsync()),
Command = new Command(async () => await RegisterAsync()),
VerticalOptions = LayoutOptions.End,
HorizontalOptions = LayoutOptions.Fill,
Style = (Style)Application.Current.Resources["btn-primary"],
@@ -74,12 +73,17 @@ namespace Bit.App.Pages
Title = "bitwarden";
Content = buttonStackLayout;
BackgroundImage = "bg.png";
BackgroundColor = Color.FromHex("ecf0f5");
}
public async Task LoginAsync()
{
await Navigation.PushModalAsync(new ExtendedNavigationPage(new LoginPage()));
}
public async Task RegisterAsync()
{
await Navigation.PushModalAsync(new ExtendedNavigationPage(new RegisterPage()));
}
}
}

View File

@@ -44,8 +44,9 @@ namespace Bit.App.Pages
var table = new ExtendedTableView
{
BackgroundColor = Color.FromHex("ecf0f5"),
Intent = TableIntent.Settings,
EnableScrolling = false,
EnableScrolling = true,
HasUnevenRows = true,
EnableSelection = false,
Root = new TableRoot
@@ -73,6 +74,7 @@ namespace Bit.App.Pages
ToolbarItems.Add(loginToolbarItem);
Title = AppResources.Bitwarden;
Content = table;
BackgroundColor = Color.FromHex("ecf0f5");
}
protected override void OnAppearing()

View File

@@ -0,0 +1,144 @@
using System;
using System.Linq;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Models.Api;
using Bit.App.Resources;
using Xamarin.Forms;
using XLabs.Ioc;
using Acr.UserDialogs;
using System.Threading.Tasks;
namespace Bit.App.Pages
{
public class RegisterPage : ContentPage
{
private ICryptoService _cryptoService;
private IUserDialogs _userDialogs;
private IAccountsApiRepository _accountsApiRepository;
public RegisterPage()
{
_cryptoService = Resolver.Resolve<ICryptoService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_accountsApiRepository = Resolver.Resolve<IAccountsApiRepository>();
Init();
}
public FormEntryCell NameCell { get; set; }
public FormEntryCell EmailCell { get; set; }
public FormEntryCell PasswordCell { get; set; }
public FormEntryCell ConfirmPasswordCell { get; set; }
public FormEntryCell PasswordHintCell { get; set; }
private void Init()
{
PasswordHintCell = new FormEntryCell("Master Password Hint (optional)");
ConfirmPasswordCell = new FormEntryCell("Re-type Master Password", IsPassword: true, nextElement: PasswordHintCell.Entry);
PasswordCell = new FormEntryCell(AppResources.MasterPassword, IsPassword: true, nextElement: ConfirmPasswordCell.Entry);
NameCell = new FormEntryCell("Your Name", nextElement: PasswordCell.Entry);
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: NameCell.Entry, entryKeyboard: Keyboard.Email);
PasswordHintCell.Entry.ReturnType = Enums.ReturnType.Done;
PasswordHintCell.Entry.Completed += Entry_Completed;
var table = new ExtendedTableView
{
BackgroundColor = Color.FromHex("ecf0f5"),
Intent = TableIntent.Settings,
EnableScrolling = true,
HasUnevenRows = true,
EnableSelection = false,
Root = new TableRoot
{
new TableSection()
{
EmailCell,
NameCell,
PasswordCell,
ConfirmPasswordCell,
PasswordHintCell
}
}
};
var loginToolbarItem = new ToolbarItem("Submit", null, async () =>
{
await Register();
}, ToolbarItemOrder.Default, 0);
if(Device.OS == TargetPlatform.iOS)
{
table.RowHeight = -1;
table.EstimatedRowHeight = 70;
ToolbarItems.Add(new DismissModalToolBarItem(this, "Cancel"));
}
ToolbarItems.Add(loginToolbarItem);
Title = "Create Account";
Content = table;
BackgroundColor = Color.FromHex("ecf0f5");
}
protected override void OnAppearing()
{
base.OnAppearing();
EmailCell.Entry.Focus();
}
private async void Entry_Completed(object sender, EventArgs e)
{
await Register();
}
private async Task Register()
{
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress), AppResources.Ok);
return;
}
if(string.IsNullOrWhiteSpace(NameCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, "Your Name"), AppResources.Ok);
return;
}
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, "Your Name"), AppResources.Ok);
return;
}
if(ConfirmPasswordCell.Entry.Text != PasswordCell.Entry.Text)
{
await DisplayAlert(AppResources.AnErrorHasOccurred, "Password confirmation is not correct.", AppResources.Ok);
return;
}
var key = _cryptoService.MakeKeyFromPassword(PasswordCell.Entry.Text, EmailCell.Entry.Text);
var request = new RegisterRequest
{
Name = NameCell.Entry.Text,
Email = EmailCell.Entry.Text,
MasterPasswordHash = _cryptoService.HashPasswordBase64(key, PasswordCell.Entry.Text),
MasterPasswordHint = !string.IsNullOrWhiteSpace(PasswordHintCell.Entry.Text) ? PasswordHintCell.Entry.Text : null
};
var responseTask = _accountsApiRepository.PostRegisterAsync(request);
_userDialogs.ShowLoading("Creating account...", MaskType.Black);
var response = await responseTask;
_userDialogs.HideLoading();
if(!response.Succeeded)
{
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
return;
}
_userDialogs.SuccessToast("Account Created", "Your new account has been created! You may now log in.");
await Navigation.PopModalAsync();
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models.Api;
namespace Bit.App.Repositories
{
public class AccountsApiRepository : BaseApiRepository, IAccountsApiRepository
{
protected override string ApiRoute => "accounts";
public virtual async Task<ApiResult> PostRegisterAsync(RegisterRequest requestObj)
{
var requestMessage = new TokenHttpRequestMessage(requestObj)
{
Method = HttpMethod.Post,
RequestUri = new Uri(Client.BaseAddress, string.Concat(ApiRoute, "/register")),
};
var response = await Client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync(response);
}
return ApiResult.Success(response.StatusCode);
}
}
}

View File

@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Bit.App.Models.Api;
using ModernHttpClient;
@@ -27,21 +25,7 @@ namespace Bit.App.Repositories
{
try
{
var errors = new List<ApiError>();
if(response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
var responseContent = await response.Content.ReadAsStringAsync();
var errorResponseModel = JsonConvert.DeserializeObject<ErrorResponse>(responseContent);
foreach(var valError in errorResponseModel.ValidationErrors)
{
foreach(var errorMessage in valError.Value)
{
errors.Add(new ApiError { Message = errorMessage });
}
}
}
var errors = await ParseErrorsAsync(response);
return ApiResult<T>.Failed(response.StatusCode, errors.ToArray());
}
catch(JsonReaderException)
@@ -49,5 +33,38 @@ namespace Bit.App.Repositories
return ApiResult<T>.Failed(response.StatusCode, new ApiError { Message = "An unknown error has occured." });
}
public async Task<ApiResult> HandleErrorAsync(HttpResponseMessage response)
{
try
{
var errors = await ParseErrorsAsync(response);
return ApiResult.Failed(response.StatusCode, errors.ToArray());
}
catch(JsonReaderException)
{ }
return ApiResult.Failed(response.StatusCode, new ApiError { Message = "An unknown error has occured." });
}
private async Task<List<ApiError>> ParseErrorsAsync(HttpResponseMessage response)
{
var errors = new List<ApiError>();
if(response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
var responseContent = await response.Content.ReadAsStringAsync();
var errorResponseModel = JsonConvert.DeserializeObject<ErrorResponse>(responseContent);
foreach(var valError in errorResponseModel.ValidationErrors)
{
foreach(var errorMessage in valError.Value)
{
errors.Add(new ApiError { Message = errorMessage });
}
}
}
return errors;
}
}
}