I am currently trying to display an error message to my HTML page whenever a user fails a login. I am not using flash, or any template engine to render the message.
What I am trying exactly to do is to send a message whenever the POST form fails to log a user and try to catch it with my Angular Front End. The main problem that I am facing is actually sending a correct message.
Here is my NodeJS Backend:
function isLoggedIn(req, res, next) {
console.log('here is Authenticated', req.isAuthenticated())
if (req.isAuthenticated()){
return next();
}else{
res.status(401).json({"message": "You are not authorized to access this page. Please log in."});
console.log("routes Print log You Cannot Log in!");
}
}
Here is my POST Form:
app.post('/login',
passport.authenticate('local', {failWithError: true }),
function(req, res) {
res.redirect('/index');
});
I am not sure how to set a message to be send on the HTML page, since when I use: res.status(401).json({"message": "You are not authorized to access this page. Please log in."}); instead of {failWithError: true }
On my Client side, I am using AngularJS and I'm trying to catch the message like this:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("/login").then(function(res){
// Do stuff if login is successful
})
.catch(function(res) {
$scope.message = res.data.message;
})
});
How can I send a failed login message from my Node Backend without using Flash or any template engine? Am I catching the message correctly???
via coderJoe
No comments:
Post a Comment