Reputation: 316
What's happening is that I can't get succesfully loged in into the Node API when hitting the login route from the website via Angular $http.post() request, But it does Works pretty well when testing from postman (rest api test application).
Here is the client side code (api calls):
function($http, $scope) {
$scope.$watch('search', function() {
fetch();
});
$scope.username = 'xxxxxxxxxxxx';
$scope.password = 'xxxxxxxxxxxxxxxx';
function fetch() {
$scope.details="";
$http.post("http://apiadress.demo/auth/login?username=" + $scope.username + "&password=" + $scope.password)
.then(function(response) {
$scope.details = response.data.state;
console.log(response);
});
}
}
this call produces the response:
{
"state": "fail",
"user": null
}
When making the same call from postma client responses the espected:
{
"state": "success",
"user": {
"_id": "xxxxxxxxxxxxxxxxxxxx",
"password": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"username": "xxxxxxxxxx",
"__v": 0,
"created_at": "2016-12-21T21:57:32.663Z"
}
}
My server side routing code is:
//log in
router.post('/login', passport.authenticate('login', {
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
}));
//responses whit successful login state
router.get('/success', function(req, res){
res.send({state: req.user ? 'success' : 'fail', user: req.user ?req.user : null});
});
As you can see the user seems to be authenticeted but no data is being passed on the redirection when the api is called from angular... O.o
Upvotes: 0
Views: 100
Reputation: 185
From the website you are sending user data (username/password) through URL (which is technically for GET requests) but you should send it as POST data since it's a POST request. Here is how you would do this:
$http.post("http://apiadress.demo/auth/login", {
username: $scope.username,
password: $scope.password
}).then( success, failure );
Upvotes: 2