I am using passport for authentication and pass a lot of different objects and settings when creating and managing users. At the moment my user creation data might look something like this (req.body):
bgcolor: '#FFF',
color: '#000',
culture: 'FI_fi',
email: 'asdf',
name: 'asdf',
'sites[-JR_Ef7nfvTdX1d0uNm7]': '-JR_Ef7nfvTdX1d0uNm7',
expire: '1',
active: 'true',
'ac[createUsers]': 'false',
'ac[modifyShifts]': 'false',
'ac[editUsers]': 'false',
'ac[delUsers]': 'false',
'views[defaultView][name]': 'defaultView',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][location]': '0',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][id]': '-JR_Ef7nfvTdX1d0uNm7',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][undefined][location]': '13',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][location]': '14',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][id]': '-JR_EzF6eZWxGic3DN8U',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][users][0][location]': '0',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][users][0][id]': '0'
Basically the object-data is in strings and not in json-format. I have both bodyParsers setup in main app.js:
passportConfigs(passport);
app.use(cookieParser());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(session({
secret: '...' ,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
And the data is handled atm. in router:
app.post('/add_user', isLoggedIn, function(req, res, next) {
... (req.body) is handled here.
}
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.status(401).send("user not logged in");
return false;
}
The problem is that passport doesn't seem to work correctly without bodyParser.urlencoded, It will error with "missing credentials".
How do I either get passport to work with bodyParser.json() (best solution) or get the urlencoded version to be parsed as JSON nicely?