DEV Community

Discussion on: Building a NodeJS Web App Using PassportJS for Authentication

Collapse
 
cristianooo profile image
Cristiano Firmani

Sure! I only get that error when setting up a new account but it actually inserts the data into the database so i moved on from it.
Now when I try to login using the credentials I used when I first set up an account, I get a Cannot Get /[object %20Response]

I've switched up the code a little so I'll show you what mine looks like. I have an ejs file with a link to the script and a button that calls the submit function :


<script type="text/javascript" src="/scripts/login.js"></script>
<button type="button" onclick="submit()" class="btn">Sign in</button>

Then the login.js looks like this:


``` function submit() {

var emailInput = document.getElementById("email").value;
var passwordInput =document.getElementById("password").value;

var userData = {
email: emailInput.trim(),
password: passwordInput.trim()
};

if (!userData.email || !userData.password) {
return;
}

// If we have an email and password we run the loginUser function and clear the form
loginUser(userData.email, userData.password);
emailInput.val("");
passwordInput.val("");
};

// loginUser does a post to our "APIlogin" route and if successful, redirects us the the members page
function loginUser(email, password) {
fetch("/APIlogin", {
method: 'POST',
email: email,
password: password
}).then(function(data) {
window.location.replace(data);
// If there's an error, log the error
}).catch(function(err) {
console.log(err);
});
}```

and then finally the app.js and APIlogin call looks like this respectively:


app.post('/APIlogin', passport.authenticate("local"), APIlogin);

and


APIlogin:(req, res)=> {
res.json("/questions");
}