var config = require('config.json');
var _ = require('lodash');
var jwt = require('jsonwebtoken');
var bcrypt = require('bcryptjs');
var Q = require('q');
var pg= require('pg');
var conString = "postgres://postgres:12345@localhost/alaa";
var service = {};
service.authenticate = authenticate;
service.getById = getById;
service.create = create;
service.update = update;
service.delete = _delete;
module.exports = service;
function authenticate(username, password) {
var deferred = Q.defer();
var conString = "postgres://postgres:12345@localhost/alaa";
pg(conString, function (err, user) {
if (err) deferred.reject(err.name + ': ' + err.message);
if (user && bcrypt.compareSync(password, user.hash)) {
// authentication successful
deferred.resolve(jwt.sign({ sub: user._id }, config.secret));
} else {
// authentication failed
deferred.resolve();
}
});
return deferred.promise;
}
function getById(_id) {
var deferred = Q.defer();
client.query('SELECT * FROM users WHERE id = $1', [id], function (err, user) {
if (err) deferred.reject(err.name + ': ' + err.message);
if (user) {
// return user (without hashed password)
deferred.resolve(_.omit(user, 'hash'));
} else {
// user not found
deferred.resolve();
}
});
return deferred.promise;
}
function create(userParam) {
var deferred = Q.defer();
// validation
pg(conString,
{ username: userParam.username },
function (err, user) {
if (err) deferred.reject(err.name + ': ' + err.message);
if (user) {
// username already exists
deferred.reject('Username "' + userParam.username + '" is already taken');
} else {
createUser();
}
});
function createUser() {
// set user object to userParam without the cleartext password
var user = _.omit(userParam, 'password');
// add hashed password to user object
user.hash = bcrypt.hashSync(userParam.password, 10);
var query = 'INSERT INTO users(user, password) VALUES ($1, $2)';
pg(conString,
function (err, user,doc) {
if (err) deferred.reject(err.name + ': ' + err.message);
deferred.resolve();
});
}
return deferred.promise;
}
function update(_id, userParam) {
var deferred = Q.defer();
// validation
client.query('SELECT * FROM users WHERE id = $1', [id], function (err, user) {
if (err) deferred.reject(err.name + ': ' + err.message);
if (user.username !== userParam.username) {
// username has changed so check if the new username is already taken
db.users.findOne(
{ username: userParam.username },
function (err, user) {
if (err) deferred.reject(err.name + ': ' + err.message);
if (user) {
// username already exists
deferred.reject('Username "' + req.body.username + '" is already taken')
} else {
updateUser();
}
});
} else {
updateUser();
}
});
function updateUser() {
// fields to update
var set = {
firstName: userParam.firstName,
lastName: userParam.lastName,
username: userParam.username,
};
// update password if it was entered
if (userParam.password) {
set.hash = bcrypt.hashSync(userParam.password, 10);
}
pg(conString,
function (err, doc) {
if (err) deferred.reject(err.name + ': ' + err.message);
deferred.resolve();
});
}
return deferred.promise;
}
function _delete(_id) {
var deferred = Q.defer();
client.query('DELETE FROM users WHERE id = $1', [id],
function (err) {
if (err) deferred.reject(err.name + ': ' + err.message);
deferred.resolve();
});
return deferred.promise;
}
我收到此错误
所以我想知道如何正确定义postgres连接以使用登录页面并使用登录页面本教程
http://jasonwatmore.com/post/2015/12/09/mean-stack-user-registration-and-login-example-tutorial
但我的问题是mongodb的教程 我想让登录页面与postgres数据库一起使用