I'm trying to get a login function working in angularJS+expressJS+node-postgres. I have my front-end http-server running on port 8000, expressJS running on port 3000 and my postgres server running on 5432. All are connected during my attempt.
My issue is that while my code seems to follow where my login button should redirect to my controller, which follows to my authService, which routes my expressJS server, which routes to a login function that uses pg.connect() to connect to my postgres database... When I click my login button, nothing happens. No console error - nothing.
I believe that I have provided all of the code necessary to see this. Hopefully someone can point out why I'm not successfully reaching my postgresql database and retrieving data. Also, the data which I am using is, in fact, stored in my 'db' database in use 'user' table with columns 'email' and 'password' containing the test data. Any insight would be appreciated.
// angularJS front-end code
// login button
<div class="col-md-12">
<button type="submit" class="btn btn-primary" data-ng-click="login()"
ng-disabled="loginForm.$invalid || !loginForm.$dirty">Login</button>
</div>
// loginController.login()
$scope.login = function () {
authService.login($scope.email, $scope.password).then(function (status) {
//$routeParams.redirect will have the route
//they were trying to go to initially
if (!status) {
$scope.errorMessage = 'Unable to login';
return;
}
if (status && $routeParams && $routeParams.redirect) {
path = path + $routeParams.redirect;
}
$location.path(homePath);
});
};
LoginController.$inject = ['$scope', '$location', '$routeParams', 'authService'];
// authFactory.login()
var serviceBase = '127.0.0.1:3000/api/dataservice/'
factory.login = function (email, password) {
return $http.post(serviceBase + 'login', { user: { email: email, password: password } })
.then(function (results) {
var loggedIn = results.data.status;
changeAuth(loggedIn);
return loggedIn;
}
);
};
// expressJS back-end code
// login route in server.js
var baseUrl = '/api/dataservice/';
app.post(baseUrl + 'login', api.login);
//login function in api.js
var pg = require('pg'),
connString = "postgres://user1:abc123@localhost:5432/db";
exports.login = function(req, res) {
if (!req.session.email) { // if not already logged in
pg.connect(connString, function(err, client, done) {
if (err) {
console.error("Error fetching client from pool", err);
} else client.query('SELECT * FROM user WHERE email = $1', req.params.email, function (err, result) {
done();
if (err) {
console.error("Error querying database", err);
} else {
if (req.params.password == result.password && req.params.email == result.email) {
res.json(200, '\"success\": true');
res.cookie('login_token', +new Date(), {path: '/', signed: true, httpOnly: true });
} else {
res.json('\"success\": false, \"message\": \"Invalid email or password\"');
}
}
});
});
}
else {
res.json('\"message\": already logged in');
}
};