C
C#15mo ago
gamer50082

✅ my ui and backend not working together

private const string ServerUrl = "http://144.24.154.156:5445/api/login"; // replace with your Node.js server URL

public Form3()
{
InitializeComponent();
}

private async void button1_Click(object sender, EventArgs e)
{
var username = textBox1.Text;
var password = textBox2.Text;

var client = new HttpClient();

var requestData = new Dictionary<string, string>
{
{"username", username},
{"password", password}
};
var requestContent = new FormUrlEncodedContent(requestData);

var response = await client.PostAsync(ServerUrl, requestContent);

var responseContent = await response.Content.ReadAsStringAsync();

if (!response.IsSuccessStatusCode)
{
MessageBox.Show("Login failed. Please check your username and password.");
return;
}

var responseData = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseContent);

var token = responseData["token"];
var accountType = responseData["accountType"];

if (accountType == "basic")
{
var form1 = new Form1(token);
form1.Show();
}
else if (accountType == "premium")
{
var form2 = new Form2(token);
form2.Show();
}

this.Hide();
}

private void button2_Click(object sender, EventArgs e)
{
var registerForm = new RegisterForm();
registerForm.Show();
this.Hide();
}
private const string ServerUrl = "http://144.24.154.156:5445/api/login"; // replace with your Node.js server URL

public Form3()
{
InitializeComponent();
}

private async void button1_Click(object sender, EventArgs e)
{
var username = textBox1.Text;
var password = textBox2.Text;

var client = new HttpClient();

var requestData = new Dictionary<string, string>
{
{"username", username},
{"password", password}
};
var requestContent = new FormUrlEncodedContent(requestData);

var response = await client.PostAsync(ServerUrl, requestContent);

var responseContent = await response.Content.ReadAsStringAsync();

if (!response.IsSuccessStatusCode)
{
MessageBox.Show("Login failed. Please check your username and password.");
return;
}

var responseData = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseContent);

var token = responseData["token"];
var accountType = responseData["accountType"];

if (accountType == "basic")
{
var form1 = new Form1(token);
form1.Show();
}
else if (accountType == "premium")
{
var form2 = new Form2(token);
form2.Show();
}

this.Hide();
}

private void button2_Click(object sender, EventArgs e)
{
var registerForm = new RegisterForm();
registerForm.Show();
this.Hide();
}
9 Replies
gamer50082
gamer5008215mo ago
u can try to integrate it with ur ui' this is just the login part havent put the reg part which will be a diff form use a random user and pass there isnt any user created yet
Angius
Angius15mo ago
What do you mean "not working together"?
gamer50082
gamer5008215mo ago
Ui works fine without the code But when I add it It breaks After removing the code it just doesn’t work anymore You can test if you want Login require 2 textbox and a button The other button is to show register form
private void btnRegister_Click(object sender, EventArgs e)
{
// Get the input values from the text boxes
string username = txtUsername.Text;
string password = txtPassword.Text;

// Create a new RestSharp client and request
var client = new RestClient("http://your-auth-server-url.com");
var request = new RestRequest("/api/register", Method.POST);

// Add the username and password to the request body
request.AddParameter("username", username);
request.AddParameter("password", password);

// Execute the request and parse the response
var response = client.Execute(request);
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);

// Check if the registration was successful
if (jsonResponse.success == true)
{
// Show the appropriate form based on the user's account type
string accountType = jsonResponse.accountType;
if (accountType == "basic")
{
Form1 form1 = new Form1();
form1.Show();
}
else if (accountType == "premium")
{
Form2 form2 = new Form2();
form2.Show();
}

// Close the registration form
this.Close();
}
else
{
// Display the error message returned by the server
string errorMessage = jsonResponse.message;
lblError.Text = errorMessage;
}
}
private void btnRegister_Click(object sender, EventArgs e)
{
// Get the input values from the text boxes
string username = txtUsername.Text;
string password = txtPassword.Text;

// Create a new RestSharp client and request
var client = new RestClient("http://your-auth-server-url.com");
var request = new RestRequest("/api/register", Method.POST);

// Add the username and password to the request body
request.AddParameter("username", username);
request.AddParameter("password", password);

// Execute the request and parse the response
var response = client.Execute(request);
dynamic jsonResponse = JsonConvert.DeserializeObject(response.Content);

// Check if the registration was successful
if (jsonResponse.success == true)
{
// Show the appropriate form based on the user's account type
string accountType = jsonResponse.accountType;
if (accountType == "basic")
{
Form1 form1 = new Form1();
form1.Show();
}
else if (accountType == "premium")
{
Form2 form2 = new Form2();
form2.Show();
}

// Close the registration form
this.Close();
}
else
{
// Display the error message returned by the server
string errorMessage = jsonResponse.message;
lblError.Text = errorMessage;
}
}
The reg part
Angius
Angius15mo ago
Well, the code isn't the best, but it should, generally, work. Maybe besides that async void method, you probably want to look into dispatchers there What exactly is the issue, again? Do you get error 500?
gamer50082
gamer5008215mo ago
The login code doesn’t work Can’t build Because of a lot of error I am on mobile right now
Angius
Angius15mo ago
Ah, now we're getting somewhere, compilation errors
gamer50082
gamer5008215mo ago
You can try to make the ui and add the code It wouldnt work
Angius
Angius15mo ago
Nothing in this code seems broken tbh
gamer50082
gamer5008215mo ago
Could just be me Can you try the code and make a ui And send it here http://144.24.154.156:5445/auth/register http://144.24.154.156:5445/auth/login Ratelimit 10 request per 1 min Idk if it’s http or https because hosting on ptero Most likely http Because ip and port Send username and password Receive accountType and token