sails.js session data not saved into db - node.js

I might be doing something wrong. Please guide me in right direction.
I am trying to implement sails session feature with 'connect-mongo'. I did the implementation same as explained in the docs Sails session mongo. After successful authentication I am trying to save session data into mongoDb. But in my case it is not saved in mongo collection and collection always remains empty.
My configurations in session.js
url: 'mongodb+srv://username:password#cluster0-tkjwp.mongodb.net/mydbname?retryWrites=true',
collection: 'sessions',
auto_reconnect: false,
ssl: false,
stringify: true,
cookie: {
secure: false,
maxAge: 24 * 60 * 60 * 1000
}
and how I am trying to save.
if(user.length && user[0].id){
// save in DB
req.session.authenticated = true;
req.session.authinfo = user[0];
req.session.save(function(err) {
console.log(req.session);
return res.json({
status: 1,
msg: 'Successfull.'
});
})
}else{
return res.send({
status: 0,
msg: 'Invalid'
});
}
Also I am not getting any error

Its working now. Only thing I missed the adapter option. But now I am using it with mysql. Below I posting my working code with MySql.
In session.js
adapter: 'express-mysql-session',
host: 'localhost',
port: 3306,
user: 'root',
password: 'xxxxxxxxx',
database: 'xyz',
ssl: false,
stringify: true,
cookie: {
maxAge: 24 * 60 * 60 * 1000
},

Related

Parse server rest password receiving email problem

I'm trying to implement the rest Password in my application so to do that I have added an email adapter to my server, like:
var parseServer = new ParseServer({
...
verifyUserEmails: false,
emailVerifyTokenValidityDuration: 24 * 60 * 60,
preventLoginWithUnverifiedEmail: false,
emailAdapter: !hasSMTPInfo ? undefined : {
module: 'parse-smtp-template',
options: {
port: env.EMAIL_PORT,
host: env.EMAIL_HOST,
user: env.EMAIL_USER,
password: env.EMAIL_PASSWORD,
fromAddress: env.EMAIL_FROMADDRESS,
template: false,
templatePath: env.EMAIL_TEMPLATEPATH
}
},
Rq: I have used the documentation of parse server api email from [documentation][1]
then I have implement the code of flutter part like:
final user = User(null, null, email);
await user.requestPasswordReset().then((value) async {
print(value.statusCode);
AlertUtils.showSuccessDialog('Veuillez vérifier votre boîte de réception')
.then((_) => Get.toNamed('/login'));
}).catchError((err) {
onDone.call();
AlertUtils.showErrorDialog(err.message.toString());
});
my problem is the status of response is 200 so every thing are right but I can't get any emails of restarting pw.
[1]: https://www.npmjs.com/package/parse-smtp-template

Mongoose throwing `Authentication failed`

I am using Mongoose Ver:4.3
I am able to connect to mongoDb using Studio3T mongo client.
But getting an authentication error when trying to connect using Mongoose within NodeJs.
My code looks as below:
const sslOptions = {
server: {
sslValidate: false,
ssl: true,
sslCert: certFileBuf,
sslKey: certFileBuf,
checkServerIdentity: false,
sslPass: 'mysslPass',
},
user: 'myUser',
pass: 'myPass'
};
connName = mongoose.createConnection("mongodb://server.com:27017/db1?tls=true&authSource=db1", sslOptions);
The above throwing an error:
mongodb\node_modules\mongoose\node_modules\mongodb\lib\utils.js:98
process.nextTick(function() { throw err; });
^
Error [MongoError]: Authentication failed.
set username and password in url
connName = mongoose.createConnection("mongodb://myUser:myPass#server.com:27017/db1?tls=true&authSource=db1", {
sslValidate: false,
ssl: true,
sslCert: certFileBuf,
sslKey: certFileBuf,
checkServerIdentity: false,
sslPass: 'mysslPass',
});

Node.js mssql error: tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true`

I get the error below. How do I fix it?
tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or `false` explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23
Change your database config options to the following:
var config = {
user: 'username',
password: 'password',
server: 'localhost',
database: 'databasename',
"options": {
"encrypt": true,
"enableArithAbort": true
}
};
read issue details here: https://github.com/tediousjs/node-mssql/issues/976
The following worked for me :
const config = {
user: 'sa',
password: '<YOUR_PASSWORD>',
server: '<COMPUTER_NAME>\\SQLEXPRESS',
database: '<DBNAME>',
requestTimeout: 180000, // for timeout setting
connectionTimeout: 180000, // for timeout setting
"options": {
"encrypt": false, // need to stop ssl checking in case of local db
"enableArithAbort": true
}
}
According to tedious docs
and
SET ARITHABORT
enableArithAbort: true // Ends a query when an overflow or divide-by-zero error occurs during query execution.
encrypt: true, // A boolean determining whether or not the connection will be encrypted. Set to true if you're on Windows Azure.
instead of setting in config in your project, set the value in node_modules
node_modules/sequelize/lib/dialects/mssql/connection-manager.js.
options: {
enableArithAbort: true,//<----------set this to true
port: parseInt(config.port, 10),
database: config.database,
trustServerCertificate: true
}

Set user info with cookie-session

I'm using cookie-session to store all my session information.
After my users login, I store his information inside req.session.user like this
req.session.user = _.omit(client.dataValues, [
"password",
"createdAt",
"updatedAt"
]);
Using console.log I can see that everything is setted ok, but when I try to use req.session.user in another route, I get undefined.
My cookie is setted like this
app.use(
cookiesession({
name: "name",
keys: ["key1", "key2"],
maxAge: 24 * 60 * 60 * 1000,
httpOnly: false,
options: {
secure: false,
secret: "secrete",
overwrite: true
}
})
);

mongoose readPreference in options object?

right now i am connecting to mongodb like this:
uri = 'mongodb://mongodomain1.com:1234,mongodomain2.com:1234/mydb?replicaSet=replicasetname&readPreference=secondaryPreferred';
opts = {
user: 'myuser',
pass: 'mypass',
server: {
auto_reconnect: true,
socketOptions: {
keepAlive: 120
}
},
replset: {
socketOptions: {
keepAlive: 120
}
}
};
mongoose.connect(uri, opts);
now i would like to bring the readPreference url parameter into the options object to be consitent and have all connection variables in the options.
i am struggling to find documentation on how to define the readPreference in the object.
same thing the replicaSet parameter in the url. is that equivalent to the rs_name
is there anything else i could/should improve in the way i am connecting to mongodb?

Resources