You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.7 KiB
54 lines
1.7 KiB
6 months ago
|
using Justice.Helpers;
|
||
|
using Justice.Models;
|
||
|
using Org.BouncyCastle.Crypto.Generators;
|
||
|
using System;
|
||
|
|
||
|
namespace Justice.Views
|
||
|
{
|
||
|
public partial class RegistrationPage : ContentPage
|
||
|
{
|
||
|
private readonly DatabaseHelper _dbHelper;
|
||
|
|
||
|
public RegistrationPage()
|
||
|
{
|
||
|
InitializeComponent();
|
||
|
_dbHelper = new DatabaseHelper();
|
||
|
RolePicker.SelectedIndexChanged += (s, e) =>
|
||
|
{
|
||
|
AuthorityTypeEntry.IsVisible = RolePicker.SelectedItem?.ToString() == "AuthorityUser";
|
||
|
};
|
||
|
}
|
||
|
|
||
|
private async void OnRegisterClicked(object sender, EventArgs e)
|
||
|
{
|
||
|
if (string.IsNullOrWhiteSpace(UsernameEntry.Text) || string.IsNullOrWhiteSpace(PasswordEntry.Text) || RolePicker.SelectedItem == null)
|
||
|
{
|
||
|
await DisplayAlert("Error", "All fields are required.", "OK");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var hashedPassword = BCrypt.Net.BCrypt.HashPassword(PasswordEntry.Text.Trim());
|
||
|
var user = new User
|
||
|
{
|
||
|
Username = UsernameEntry.Text.Trim(),
|
||
|
Password = hashedPassword,
|
||
|
Role = RolePicker.SelectedItem.ToString(),
|
||
|
AuthorityType = AuthorityTypeEntry.IsVisible ? AuthorityTypeEntry.Text.Trim() : null
|
||
|
};
|
||
|
|
||
|
try
|
||
|
{
|
||
|
await _dbHelper.InitializeAsync<User>();
|
||
|
await _dbHelper.InsertAsync(user);
|
||
|
|
||
|
await DisplayAlert("Success", "User registered successfully.", "OK");
|
||
|
await Navigation.PopAsync();
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
await DisplayAlert("Error", $"Failed to register: {ex.Message}", "OK");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|