Chnage password

This commit is contained in:
Bartosz Chyży 2020-06-24 00:42:55 +02:00
parent baa2eac499
commit b85b295cb5
9 changed files with 148 additions and 22 deletions

View File

@ -14,7 +14,7 @@
<Label x:Name="ErrorMessageLabel"
TextColor="#d22"
Margin="0,10" HorizontalOptions="Center"/>
<Button Text="AuthorizeAsync" Command="{Binding AuthorizeCommand}"/>
<Button Text="Authorize" Command="{Binding AuthorizeCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -41,7 +41,7 @@ namespace SafeMessageStorage.Pages
private async void _viewModel_AuthorizationSucceeded(object sender, EventArgs e)
{
await Navigation.PushAsync(new MessagesListPage(_messageStorage));
await Navigation.PushAsync(new MessagesListPage(_messageStorage, _authorizationService));
}
private async void _viewModel_AuthorizationFailed(object sender, string e)

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SafeMessageStorage.Pages.ChangePasswordPage"
Title="Change password">
<ContentPage.Content>
<StackLayout VerticalOptions="CenterAndExpand" Padding="40">
<Label Text="Current password"/>
<Entry IsPassword="True" Text="{Binding CurrentPassword}"/>
<Label Text="New password"/>
<Entry IsPassword="True" Text="{Binding NewPassword}"/>
<Label Text="Repeat password"/>
<Entry IsPassword="True" Text="{Binding RepeatPassword}"/>
<Label x:Name="ErrorMessageLabel"
TextColor="#d22"
Margin="0,10" HorizontalOptions="Center"/>
<Button Text="Set password" Command="{Binding SetPasswordCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SafeMessageStorage.Services.AuthorizationService;
using SafeMessageStorage.Services.MessageStorageService;
using SafeMessageStorage.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SafeMessageStorage.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ChangePasswordPage : ContentPage
{
private ChangePasswordViewModel _viewModel => this.BindingContext as ChangePasswordViewModel;
private IMessageStorageService _messageStorage;
public ChangePasswordPage(IAuthorizationService authorizationService, IMessageStorageService messageStorage)
{
InitializeComponent();
BindingContext = new ChangePasswordViewModel(authorizationService);
_messageStorage = messageStorage;
_viewModel.PasswordSetFailed += _viewModel_PasswordSetFailed;
_viewModel.PasswordSetSucceeded += _viewModel_PasswordSetSucceeded;
}
private async void _viewModel_PasswordSetSucceeded(object sender, EventArgs e)
{
await _messageStorage.DeleteAllMessagesAsync();
await Navigation.PopAsync();
}
private async void _viewModel_PasswordSetFailed(object sender, string e)
{
ErrorMessageLabel.Text = e;
await ErrorMessageLabel.FadeTo(1);
await Task.Delay(1000);
await ErrorMessageLabel.FadeTo(0);
}
}
}

View File

@ -17,24 +17,6 @@
<TextCell Text="{Binding Title}" />
</DataTemplate>
</ListView.ItemTemplate>
<!--Custom View Cells-->
<!--
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Text}"
d:Text="{Binding .}"
Style="{DynamicResource ListItemTextStyle}" />
<Label Text="{Binding Detail}"
d:Text="Detail"
Style="{DynamicResource ListItemDetailTextStyle}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
-->
</ListView>
<Button Text="AddMessage" Clicked="Button_OnClicked"></Button>
</StackLayout>

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using SafeMessageStorage.Models;
using SafeMessageStorage.Services.AuthorizationService;
using SafeMessageStorage.Services.MessageStorageService;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
@ -14,16 +15,25 @@ namespace SafeMessageStorage.Pages
public partial class MessagesListPage : ContentPage
{
private IMessageStorageService _messageStorage;
private IAuthorizationService _authorizationService;
private bool _initialized;
public ObservableCollection<Message> Messages { get; set; }
public MessagesListPage(IMessageStorageService messageStorage)
public MessagesListPage(IMessageStorageService messageStorage, IAuthorizationService authorizationService)
{
InitializeComponent();
_messageStorage = messageStorage;
_authorizationService = authorizationService;
Messages = new ObservableCollection<Message>();
BindingContext = this;
ToolbarItems.Add(new ToolbarItem("Change password", "", () => ChangePassword()));
}
private async void ChangePassword()
{
await Navigation.PushAsync(new ChangePasswordPage(_authorizationService, _messageStorage));
}
protected override async void OnAppearing()

View File

@ -22,6 +22,9 @@
<EmbeddedResource Update="Pages\AuthorizationPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Pages\ChangePasswordPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Pages\MessageDetailPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>

View File

@ -49,7 +49,9 @@ namespace SafeMessageStorage.Services.AuthorizationService
if (!passwordMatch)
return false;
await Xamarin.Essentials.SecureStorage.SetAsync(_passwordKey, _hashProvider.GetHashString(newPassword));
var passwordHash = _hashProvider.GetHashString(newPassword);
AuthorizedUserHash = passwordHash;
await Xamarin.Essentials.SecureStorage.SetAsync(_passwordKey, passwordHash);
return true;
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using SafeMessageStorage.Services.AuthorizationService;
using Xamarin.Forms;
namespace SafeMessageStorage.ViewModels
{
public class ChangePasswordViewModel : ViewModelBase
{
private IAuthorizationService _authorizationService;
public ChangePasswordViewModel(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
private string _currentPassword;
public string CurrentPassword
{
get { return _currentPassword; }
set { _currentPassword = value; OnPropertyChanged(); }
}
private string _newPassword;
public string NewPassword
{
get { return _newPassword; }
set { _newPassword = value; OnPropertyChanged(); }
}
private string _repeatPassword;
public string RepeatPassword
{
get { return _repeatPassword; }
set { _repeatPassword = value; OnPropertyChanged(); }
}
public event EventHandler<string> PasswordSetFailed;
public event EventHandler PasswordSetSucceeded;
public ICommand SetPasswordCommand => new Command(() => SetPassword(_currentPassword,_newPassword, _repeatPassword));
private async void SetPassword(string currentPassword, string password, string repeatPassword)
{
if(!await _authorizationService.AuthorizeAsync(currentPassword))
PasswordSetFailed?.Invoke(this, "Current password doesn't match");
if (!password.Equals(repeatPassword))
{
PasswordSetFailed?.Invoke(this, "Passwords are not equal");
}
else
{
if (await _authorizationService.ChangePasswordAsync(currentPassword, password))
PasswordSetSucceeded?.Invoke(this, EventArgs.Empty);
}
}
}
}