angular-sailsjs-boilerplate icon indicating copy to clipboard operation
angular-sailsjs-boilerplate copied to clipboard

401 error when using Mongo

Open jes-carr opened this issue 10 years ago • 6 comments

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.

jes-carr avatar Jul 24 '15 16:07 jes-carr

+1

vcardins avatar Aug 10 '15 06:08 vcardins

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.

tarlepp avatar Aug 11 '15 16:08 tarlepp

Sorry, I didn't exactly understand. Could you guide a workaround on this?

vcardins avatar Aug 11 '15 17:08 vcardins

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.

tarlepp avatar Aug 11 '15 18:08 tarlepp

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();
});
...

vcardins avatar Aug 13 '15 06:08 vcardins

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.

tarlepp avatar Aug 13 '15 20:08 tarlepp