parent
d1f9f85a9e
commit
4108506fc5
@ -1,53 +1,141 @@ |
||||
using Justice.Helpers; |
||||
using Justice.Models; |
||||
using Org.BouncyCastle.Crypto.Generators; |
||||
using System; |
||||
using Microsoft.Maui.Storage; |
||||
using System.ComponentModel; |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
namespace Justice.Views |
||||
{ |
||||
public partial class RegistrationPage : ContentPage |
||||
public partial class RegistrationPage : ContentPage, INotifyPropertyChanged |
||||
{ |
||||
private readonly DatabaseHelper _dbHelper; |
||||
private string _identityFilePath; |
||||
|
||||
private bool _isBusy; |
||||
public bool IsBusy |
||||
{ |
||||
get => _isBusy; |
||||
set |
||||
{ |
||||
_isBusy = value; |
||||
OnPropertyChanged(); |
||||
} |
||||
} |
||||
|
||||
public RegistrationPage() |
||||
{ |
||||
InitializeComponent(); |
||||
_dbHelper = new DatabaseHelper(); |
||||
RolePicker.SelectedIndexChanged += (s, e) => |
||||
{ |
||||
AuthorityTypeEntry.IsVisible = RolePicker.SelectedItem?.ToString() == "AuthorityUser"; |
||||
}; |
||||
BindingContext = this; |
||||
} |
||||
|
||||
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"); |
||||
return; |
||||
} |
||||
var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>> |
||||
{ |
||||
{ 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 user = new User |
||||
var result = await FilePicker.PickAsync(options); |
||||
|
||||
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(), |
||||
Password = hashedPassword, |
||||
Role = RolePicker.SelectedItem.ToString(), |
||||
AuthorityType = AuthorityTypeEntry.IsVisible ? AuthorityTypeEntry.Text.Trim() : null |
||||
}; |
||||
await DisplayAlert("Error", $"Failed to select file: {ex.Message}", "OK"); |
||||
} |
||||
} |
||||
|
||||
private async void OnRegisterClicked(object sender, EventArgs e) |
||||
{ |
||||
if (IsBusy) return; |
||||
IsBusy = true; |
||||
|
||||
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>(); |
||||
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 DisplayAlert("Success", "Registration successful!", "OK"); |
||||
|
||||
await DisplayAlert("Success", "User registered successfully.", "OK"); |
||||
await Navigation.PopAsync(); |
||||
// Redirect to Login Page |
||||
await Shell.Current.GoToAsync("//LoginPage"); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
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