angular-sailsjs-boilerplate
angular-sailsjs-boilerplate copied to clipboard
401 error when using Mongo
You can reproduce it just by changing the connection from sails-disk to sails-mongo. When trying to log in you get a 401 (Unauthorized). It seems that the variable user is false, but not sure where is the issue.
+1
This is basically fixture problem, mongo doesn't use integer ID's like SQL based databases. So tl:dr when database is initialized created users don't have necessary passport relations attached.
Sorry, I didn't exactly understand. Could you guide a workaround on this?
See fixtures for User and Passport models, especially that Passport fixture, see that it relies to numeric id columns which are not supported by mongo.
So quick fix is to manually alter your mongo db data OR make a PR which fixes this, there is some solutions on barrels repo to make assocs work with fixtures, but basically I haven't had time to make this fix - yet.
By updating it manually works, but of course is not ideal. I tried creating assocs fixtures but it seems that there is a bug with barrels: http://screencast.com/t/YQhoZ5bC. There is an open issue for that: https://github.com/bredikhin/barrels/issues/31. I managed it to work by using another lib: pow-mongodb-fixtures. Not ideal cause I had to put all the data in a single file, maybe there is another way to load per file ... It solves the problem temporary, but I'd like to hear what would be the best solution to this.
load-db.js
...
var conn = sails.config.connections.someMongodbServer.database;
var fixtures = require('pow-mongodb-fixtures').connect(conn);
//File (loads content from data.js file)
var dir = '../../test/fixtures/js/';
fixtures.load(dir +'data.js', function(){
next();
});
...
Yeah, this one needs some serious work. Basically those fixtures should work as the same with any adapter, otherwise those aren't working like those should.
But basically this is only problem when creating new app when there isn't any data on db yet. And if your app is using mongo or something else you propably create fixtures using some specified tool.