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.
56 lines
1.7 KiB
56 lines
1.7 KiB
6 months ago
|
using Justice.Helpers;
|
||
|
using Justice.Models;
|
||
|
using Org.BouncyCastle.Crypto.Generators;
|
||
|
using System;
|
||
|
using BCrypt.Net;
|
||
|
|
||
|
namespace Justice.Views
|
||
|
{
|
||
|
public partial class LoginPage : ContentPage
|
||
|
{
|
||
|
private readonly DatabaseHelper _dbHelper;
|
||
|
|
||
|
public LoginPage()
|
||
|
{
|
||
|
InitializeComponent();
|
||
|
_dbHelper = new DatabaseHelper();
|
||
|
}
|
||
|
|
||
|
private async void OnLoginClicked(object sender, EventArgs e)
|
||
|
{
|
||
|
if (string.IsNullOrWhiteSpace(UsernameEntry.Text) || string.IsNullOrWhiteSpace(PasswordEntry.Text))
|
||
|
{
|
||
|
await DisplayAlert("Error", "Username and password are required.", "OK");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
try
|
||
|
{
|
||
|
var user = await _dbHelper.FindAsync<User>("SELECT * FROM User WHERE Username = ?", UsernameEntry.Text.Trim());
|
||
|
|
||
|
if (user == null || !BCrypt.Net.BCrypt.Verify(PasswordEntry.Text.Trim(), user.Password))
|
||
|
{
|
||
|
await DisplayAlert("Error", "Invalid username or password.", "OK");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
await DisplayAlert("Success", $"Welcome {user.Username}!", "OK");
|
||
|
|
||
|
// Navigate based on role
|
||
|
if (user.Role == "EndUser")
|
||
|
{
|
||
|
await Navigation.PushAsync(new DashboardPage());
|
||
|
}
|
||
|
else if (user.Role == "AuthorityUser")
|
||
|
{
|
||
|
await Navigation.PushAsync(new AuthorityDashboardPage());
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
await DisplayAlert("Error", $"Failed to login: {ex.Message}", "OK");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|