Utopia-Attendance-Software/LoginForm.cs

203 lines
6.4 KiB
C#

using FaceTransfer;
using MaterialSkin.Controls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Windows.Forms;
namespace UtopiaAttService
{
public partial class LoginForm : MaterialForm
{
public static string SERVER_URL = "https://portal.utopiaindustries.pk/uind/rest/auth/user";
public static string filePath = "credentials.txt";
public LoginForm()
{
InitializeComponent();
//Init skin
MaterialSkin.MaterialSkinManager skinManager = MaterialSkin.MaterialSkinManager.Instance;
skinManager.AddFormToManage(this);
skinManager.Theme = MaterialSkin.MaterialSkinManager.Themes.LIGHT;
skinManager.ColorScheme = new MaterialSkin.ColorScheme(MaterialSkin.Primary.Green900, MaterialSkin.Primary.BlueGrey900, MaterialSkin.Primary.Blue500, MaterialSkin.Accent.Orange700, MaterialSkin.TextShade.WHITE);
// Hide maximize button
this.MaximizeBox = false;
}
private async void authenticate(String username, String password)
{
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromMinutes(5);
string paramater= string.Format("{0}/{1}", username, password);
var response = await client.GetAsync(string.Format("{0}/{1}", SERVER_URL, paramater));
string responseString = await response.Content.ReadAsStringAsync();
//Dictionary<string, string> sData = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString);
if (responseString.Equals("NOT FOUND"))
{
ShowMessageBox("Incorrect usename and password");
}
else
{
if (cb_remember.Checked)
{
saveCredentials();
}
Form1 form = new Form1();
form.Show();
this.Hide();
}
}
private void btn_login_Click(object sender, EventArgs e)
{
if (txt_username.Text.Length > 0 && txt_password.Text.Length > 0)
{
authenticate(txt_username.Text, txt_password.Text);
}
else
{
ShowMessageBox("Please enter username and password");
}
}
private void ShowMessageBox(string text)
{
using (var messageBox = new MaterialForm())
{
messageBox.StartPosition = FormStartPosition.CenterParent;
messageBox.MaximizeBox = false;
messageBox.MinimizeBox = false;
messageBox.ShowIcon = true;
messageBox.Size = new Size(350, 200);
messageBox.Text = "Message box";
var Label = new MaterialLabel
{
Text = text,
AutoSize = true,
Location = new Point(50, 100),
ForeColor = Color.Red,
};
var okButton = new MaterialRaisedButton
{
Text = "OK",
Location = new Point(140, 150),
};
okButton.Click += (sender, args) => messageBox.Close();
messageBox.Controls.Add(Label);
messageBox.Controls.Add(okButton);
messageBox.ShowDialog();
}
}
private void saveCredentials()
{
// Get the username and password from your input fields
string username = txt_username.Text;
string password = txt_password.Text;
// Define the file path where you want to save the data
try
{
// If the file exists, delete it before creating a new one
if (File.Exists(filePath))
{
File.Delete(filePath);
}
// Write the username and password to the file
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(username);
writer.WriteLine(password);
}
// MessageBox.Show("Credentials saved successfully.");
}
catch (Exception ex)
{
// MessageBox.Show("Error saving credentials: " + ex.Message);
}
}
private bool tryReadCredentials(out string username, out string password)
{
// Initialize variables to store the values
username = null;
password = null;
try
{
// Check if the file exists
if (File.Exists(filePath))
{
// Read the lines from the file
string[] lines = File.ReadAllLines(filePath);
// Check if there are enough lines (username and password)
if (lines.Length >= 2)
{
username = lines[0];
password = lines[1];
return true; // Successfully read the credentials
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error reading credentials: " + ex.Message);
}
return false; // Failed to read the credentials
}
private bool isPasswordVisible = false; // Variable to track password visibility
private void pb_ShowPassword_Click(object sender, EventArgs e)
{
if (txt_password.Text.Length > 0)
{
if (isPasswordVisible)
{
// Password is currently visible, hide it
txt_password.PasswordChar = '*'; // Set PasswordChar to the masking character (e.g., asterisk)
}
else
{
// Password is currently hidden, show it
txt_password.PasswordChar = '\0'; // Set PasswordChar to an empty character
}
// Toggle the state of isPasswordVisible
isPasswordVisible = !isPasswordVisible;
}
}
}
}