fixed problem with then catch chain (Promise)...
This commit is contained in:
@@ -14,13 +14,29 @@ var options = {
|
|||||||
// user: process.env.UDB,
|
// user: process.env.UDB,
|
||||||
// pass: process.env.PDB,
|
// pass: process.env.PDB,
|
||||||
useMongoClient: true,
|
useMongoClient: true,
|
||||||
|
useNewUrlParser: true,
|
||||||
promiseLibrary: require('bluebird')
|
promiseLibrary: require('bluebird')
|
||||||
|
|
||||||
|
// useCreateIndex: true,
|
||||||
|
// useFindAndModify: false,
|
||||||
|
// autoIndex: false, // Don't build indexes
|
||||||
|
// reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
|
||||||
|
// reconnectInterval: 500, // Reconnect every 500ms
|
||||||
|
// poolSize: 10, // Maintain up to 10 socket connections
|
||||||
|
// // If not connected, return errors immediately rather than waiting for reconnect
|
||||||
|
// bufferMaxEntries: 0,
|
||||||
|
// connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
|
||||||
|
// socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
|
||||||
|
// family: 4 // Use IPv4, skip trying IPv6
|
||||||
|
|
||||||
|
// keepAlive: true, // keepAlive is true by default since mongoose 5.2.0
|
||||||
|
// keepAliveInitialDelay: 300000 // keepAliveInitialDelay is the number of milliseconds to wait before initiating keepAlive on the socket.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// mongoose.connect(process.env.MONGODB_URI + '?authSource=admin', { options })
|
// mongoose.connect(process.env.MONGODB_URI + '?authSource=admin', { options })
|
||||||
mongoose.connect(process.env.MONGODB_URI, { options })
|
mongoose.connect(process.env.MONGODB_URI, options)
|
||||||
.then(() =>
|
.then(() =>
|
||||||
console.log('connection succesful ' + process.env.MONGODB_URI + ' db: ' + process.env.DATABASE)
|
console.log('connection succesful ' + process.env.MONGODB_URI + ' db: ' + process.env.DATABASE)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -10,12 +10,10 @@ var authenticate = (req, res, next) => {
|
|||||||
const useragent = req.get('User-Agent');
|
const useragent = req.get('User-Agent');
|
||||||
const access = 'auth ' + useragent;
|
const access = 'auth ' + useragent;
|
||||||
|
|
||||||
tools.mylog("TOKEN = ", token);
|
|
||||||
tools.mylog("USER-AGENT = ", useragent);
|
|
||||||
|
|
||||||
User.findByToken(token, access).then((user) => {
|
User.findByToken(token, access).then((user) => {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
tools.mylog("TOKEN NOT FOUND! Maybe Connected to other Page");
|
tools.mylog("TOKEN " + token + " NOT FOUND! (Maybe Connected to other Page) ACCESS: '" + access + "'");
|
||||||
return Promise.reject(server_constants.RIS_CODE_HTTP_INVALID_TOKEN);
|
return Promise.reject(server_constants.RIS_CODE_HTTP_INVALID_TOKEN);
|
||||||
// res.status().send();
|
// res.status().send();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ UserSchema.methods.generateAuthToken = function (req) {
|
|||||||
|
|
||||||
return user.save()
|
return user.save()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("TOKEN CREATO IN LOGIN : " + token)
|
console.log("TOKEN CREATO IN LOGIN : " + token);
|
||||||
return token;
|
return token;
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ router.get('/:username', (req, res) => {
|
|||||||
router.post('/login', (req, res) => {
|
router.post('/login', (req, res) => {
|
||||||
var body = _.pick(req.body, ['username', 'password', 'idapp', 'keyappid', 'lang']);
|
var body = _.pick(req.body, ['username', 'password', 'idapp', 'keyappid', 'lang']);
|
||||||
var user = new User(body);
|
var user = new User(body);
|
||||||
|
const subs = _.pick(req.body, ['subs']);
|
||||||
|
|
||||||
tools.mylog("LOGIN: username: " + user.username + " pwd = " + user.password);
|
tools.mylog("LOGIN: username: " + user.username + " pwd = " + user.password);
|
||||||
|
|
||||||
@@ -104,9 +105,9 @@ router.post('/login', (req, res) => {
|
|||||||
tools.mylogshow("NOT FOUND !");
|
tools.mylogshow("NOT FOUND !");
|
||||||
res.status(404).send({ code: server_constants.RIS_CODE_LOGIN_ERR });
|
res.status(404).send({ code: server_constants.RIS_CODE_LOGIN_ERR });
|
||||||
}
|
}
|
||||||
|
return user
|
||||||
})
|
})
|
||||||
.then(user => {
|
.then(user => {
|
||||||
let mytoken = '';
|
|
||||||
return user.generateAuthToken(req).then((token) => {
|
return user.generateAuthToken(req).then((token) => {
|
||||||
var usertosend = User();
|
var usertosend = User();
|
||||||
usertosend.username = user.username;
|
usertosend.username = user.username;
|
||||||
@@ -119,47 +120,50 @@ router.post('/login', (req, res) => {
|
|||||||
|
|
||||||
// tools.mylog("usertosend:");
|
// tools.mylog("usertosend:");
|
||||||
// tools.mylog(usertosend);
|
// tools.mylog(usertosend);
|
||||||
return usertosend
|
return { usertosend, token }
|
||||||
})
|
})
|
||||||
.then((usertosend) => {
|
.then((myris) => {
|
||||||
const useragent = req.get('User-Agent');
|
const useragent = req.get('User-Agent');
|
||||||
const access = 'auth ' + useragent;
|
const access = 'auth ' + useragent;
|
||||||
// if (usertosend.tokens.length > 0)
|
// if (usertosend.tokens.length > 0)
|
||||||
// access = usertosend.tokens[usertosend.tokens.length - 1].access;
|
// access = usertosend.tokens[usertosend.tokens.length - 1].access;
|
||||||
|
return existSubScribe(myris.usertosend.userId, access).then(subscribe => {
|
||||||
|
let subscriptionModel = null;
|
||||||
|
if (subscribe === null) {
|
||||||
|
// Doesn't exist, so save sub passed in INPUT
|
||||||
|
subscriptionModel = new Subscription(subs);
|
||||||
|
subscriptionModel.userId = myris.usertosend.userId;
|
||||||
|
subscriptionModel.access = access;
|
||||||
|
|
||||||
return existSubScribe(usertosend.userId, access)
|
}
|
||||||
.then(subscribe => {
|
return {usertosend: myris.usertosend, token: myris.token, subscriptionModel }
|
||||||
if (subscribe === null) {
|
}).then(myris => {
|
||||||
// Doesn't exist, so save sub passed in INPUT
|
if (myris.subscriptionModel !== null) {
|
||||||
let subscriptionModel = new Subscription(req.body.subs);
|
return myris.subscriptionModel.save().then((err, subscription) => {
|
||||||
subscriptionModel.userId = usertosend._id;
|
if (err) {
|
||||||
subscriptionModel.access = access;
|
console.error(`Error occurred while saving subscription. Err: ${err}`);
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Technical error occurred'
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Send 201 - resource created
|
||||||
|
// res.status(201).json({ data: 'Subscription saved.' });
|
||||||
|
// res.send({ data: 'Subscription saved.' });
|
||||||
|
|
||||||
return subscriptionModel.save((err, subscription) => {
|
tools.sendBackNotif(subscription, req.body.options)
|
||||||
if (err) {
|
}
|
||||||
console.error(`Error occurred while saving subscription. Err: ${err}`);
|
return { usertosend: myris.usertosend, token: myris.token }
|
||||||
res.status(500).json({
|
});
|
||||||
error: 'Technical error occurred'
|
} else {
|
||||||
});
|
return { usertosend: myris.usertosend, token: myris.token }
|
||||||
} else {
|
}
|
||||||
// Send 201 - resource created
|
}).catch(e => {
|
||||||
// res.status(201).json({ data: 'Subscription saved.' });
|
console.log('Err subscribing:', e);
|
||||||
// res.send({ data: 'Subscription saved.' });
|
})
|
||||||
|
}).then(myris => {
|
||||||
|
console.log('res', myris.token, myris.usertosend);
|
||||||
|
|
||||||
tools.sendBackNotif(subscription, req.body.options)
|
res.header('x-auth', myris.token).send({ usertosend: myris.usertosend, code: server_constants.RIS_CODE_OK });
|
||||||
}
|
|
||||||
|
|
||||||
return { usertosend }
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return { usertosend }
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.then((usertosend) => {
|
|
||||||
console.log('res', mytoken, usertosend);
|
|
||||||
|
|
||||||
res.header('x-auth', mytoken).send({ usertosend, code: server_constants.RIS_CODE_OK });
|
|
||||||
// tools.mylog("TROVATOOO!");
|
// tools.mylog("TROVATOOO!");
|
||||||
|
|
||||||
tools.mylog('FINE LOGIN')
|
tools.mylog('FINE LOGIN')
|
||||||
|
|||||||
Reference in New Issue
Block a user