parent
d1f9f85a9e
commit
4108506fc5
@ -1,53 +1,141 @@ |
|||||||
using Justice.Helpers; |
using Justice.Helpers; |
||||||
using Justice.Models; |
using Justice.Models; |
||||||
using Org.BouncyCastle.Crypto.Generators; |
using Microsoft.Maui.Storage; |
||||||
using System; |
using System.ComponentModel; |
||||||
|
using System.Runtime.CompilerServices; |
||||||
|
|
||||||
namespace Justice.Views |
namespace Justice.Views |
||||||
{ |
{ |
||||||
public partial class RegistrationPage : ContentPage |
public partial class RegistrationPage : ContentPage, INotifyPropertyChanged |
||||||
{ |
{ |
||||||
private readonly DatabaseHelper _dbHelper; |
private readonly DatabaseHelper _dbHelper; |
||||||
|
private string _identityFilePath; |
||||||
|
|
||||||
|
private bool _isBusy; |
||||||
|
public bool IsBusy |
||||||
|
{ |
||||||
|
get => _isBusy; |
||||||
|
set |
||||||
|
{ |
||||||
|
_isBusy = value; |
||||||
|
OnPropertyChanged(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
public RegistrationPage() |
public RegistrationPage() |
||||||
{ |
{ |
||||||
InitializeComponent(); |
InitializeComponent(); |
||||||
_dbHelper = new DatabaseHelper(); |
_dbHelper = new DatabaseHelper(); |
||||||
RolePicker.SelectedIndexChanged += (s, e) => |
BindingContext = this; |
||||||
{ |
|
||||||
AuthorityTypeEntry.IsVisible = RolePicker.SelectedItem?.ToString() == "AuthorityUser"; |
|
||||||
}; |
|
||||||
} |
} |
||||||
|
|
||||||
private async void OnRegisterClicked(object sender, EventArgs e) |
private async void OnChooseFileClicked(object sender, EventArgs e) |
||||||
{ |
{ |
||||||
if (string.IsNullOrWhiteSpace(UsernameEntry.Text) || string.IsNullOrWhiteSpace(PasswordEntry.Text) || RolePicker.SelectedItem == null) |
try |
||||||
{ |
{ |
||||||
await DisplayAlert("Error", "All fields are required.", "OK"); |
var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>> |
||||||
return; |
{ |
||||||
} |
{ DevicePlatform.Android, new[] { "*/*" } }, // Allow all files on Android |
||||||
|
{ DevicePlatform.iOS, new[] { "public.data" } }, |
||||||
|
{ DevicePlatform.WinUI, new[] { "*" } } |
||||||
|
}); |
||||||
|
|
||||||
|
var options = new PickOptions |
||||||
|
{ |
||||||
|
PickerTitle = "Select Identity Document", |
||||||
|
FileTypes = customFileType |
||||||
|
}; |
||||||
|
|
||||||
var hashedPassword = BCrypt.Net.BCrypt.HashPassword(PasswordEntry.Text.Trim()); |
var result = await FilePicker.PickAsync(options); |
||||||
var user = new User |
|
||||||
|
if (result != null) |
||||||
|
{ |
||||||
|
_identityFilePath = result.FullPath; |
||||||
|
IdentityFileLabel.Text = $"Selected File: {result.FileName}"; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
IdentityFileLabel.Text = "No file selected"; |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
{ |
{ |
||||||
Username = UsernameEntry.Text.Trim(), |
await DisplayAlert("Error", $"Failed to select file: {ex.Message}", "OK"); |
||||||
Password = hashedPassword, |
} |
||||||
Role = RolePicker.SelectedItem.ToString(), |
} |
||||||
AuthorityType = AuthorityTypeEntry.IsVisible ? AuthorityTypeEntry.Text.Trim() : null |
|
||||||
}; |
private async void OnRegisterClicked(object sender, EventArgs e) |
||||||
|
{ |
||||||
|
if (IsBusy) return; |
||||||
|
IsBusy = true; |
||||||
|
|
||||||
try |
try |
||||||
{ |
{ |
||||||
|
var username = UsernameEntry?.Text?.Trim(); |
||||||
|
var password = PasswordEntry?.Text?.Trim(); |
||||||
|
var confirmPassword = ConfirmPasswordEntry?.Text?.Trim(); |
||||||
|
|
||||||
|
// Validate inputs |
||||||
|
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(confirmPassword)) |
||||||
|
{ |
||||||
|
await DisplayAlert("Error", "All fields are required.", "OK"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(_identityFilePath)) |
||||||
|
{ |
||||||
|
await DisplayAlert("Error", "Please upload an identity document.", "OK"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (password != confirmPassword) |
||||||
|
{ |
||||||
|
await DisplayAlert("Error", "Passwords do not match.", "OK"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// Hash the password asynchronously |
||||||
|
var hashedPassword = await Task.Run(() => BCrypt.Net.BCrypt.HashPassword(password)); |
||||||
|
|
||||||
|
var user = new User |
||||||
|
{ |
||||||
|
Username = username, |
||||||
|
Password = hashedPassword, |
||||||
|
Role = "End User", // Fixed role for End User |
||||||
|
IdentityFilePath = _identityFilePath // Save file path |
||||||
|
}; |
||||||
|
|
||||||
|
// Save to database |
||||||
await _dbHelper.InitializeAsync<User>(); |
await _dbHelper.InitializeAsync<User>(); |
||||||
|
var existingUser = await _dbHelper.FindAsync<User>("SELECT * FROM User WHERE Username = ?", username); |
||||||
|
|
||||||
|
if (existingUser != null) |
||||||
|
{ |
||||||
|
await DisplayAlert("Error", "Username already exists.", "OK"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
await _dbHelper.InsertAsync(user); |
await _dbHelper.InsertAsync(user); |
||||||
|
await DisplayAlert("Success", "Registration successful!", "OK"); |
||||||
|
|
||||||
await DisplayAlert("Success", "User registered successfully.", "OK"); |
// Redirect to Login Page |
||||||
await Navigation.PopAsync(); |
await Shell.Current.GoToAsync("//LoginPage"); |
||||||
} |
} |
||||||
catch (Exception ex) |
catch (Exception ex) |
||||||
{ |
{ |
||||||
await DisplayAlert("Error", $"Failed to register: {ex.Message}", "OK"); |
await DisplayAlert("Error", $"Failed to register: {ex.Message}", "OK"); |
||||||
} |
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
IsBusy = false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// INotifyPropertyChanged Implementation |
||||||
|
public event PropertyChangedEventHandler PropertyChanged; |
||||||
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) |
||||||
|
{ |
||||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue