I have created a basic login page using React with a button for users to click and log in. I also set up a postgreSQL database of username and passwords on my Heroku server. Now, I just need to authenticate users by checking if the inputted credentials match what is in my database. I tried to do this in the Authenticate() function. Also, Authenticate() is being called in the button via onClick.
Right now, I have basic authentication using jQuery Ajax set up (thanks to this Stackoverflow post: Use basic authentication with jQuery and Ajax). However, when I press the button on my page, nothing happens.
Because I haven't specified my URL! But... I don't have a URL because it's a database on a server. Also, how am I going to check through my database to see if there's a match? Finally, I will need to reroute the user to another page if it is a success.
Authenticate() {
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
// Adds a HTTP header with the Authentication information
beforeSend: function(xhr) {
xhr.setRequestHeader(
"Authorization",
"Basic " + btoa(email + ":" + password)
);
},
type: "GET",
url: "",
dataType: "json",
async: false,
data: '{"email": "' + email + '", "password" : "' + password + '"}',
// Successful authentication
success: function() {
alert("Successfully authenticated!"); // TODO: Set appropriate auth cookie
},
// Display error message
complete: function(jqXHR) {
// 401: Unauthorized
if (jqXHR.status == "401") {
var message = jqXHR.status + " : " + jqXHR.statusText;
alert(message);
}
}
});
}
I am a bit of a noob, hence why I have so many questions. Please bear with me. Thanks!