/**
* @module flitter-auth/Helpers
*/
const uuid = require('uuid/v4')
module.exports = exports = {
/**
* Recursively merges two objects.
* Will preserve sub-objects and override sub-keys.
* Object 1 is the base, and object 2 is applied over it as overrides.
* @param {Object} obj1
* @param {Object} obj2
* @return {Object} - the merged object
*/
deep_merge(obj1, obj2){
for ( let key in obj2 ){
if ( !Object.keys(obj1).includes(key) ) obj1[key] = obj2[key]
else if ( typeof obj1[key] !== 'object' ) obj1[key] = obj2[key]
else if ( typeof obj2[key] !== 'object' ) obj1[key] = obj2[key]
else obj1[key] = this.deep_merge(obj1[key], obj2[key])
}
return obj1
},
/**
* Guarantees that the req.session.auth object exists.
* If not, creates a default template. Inject the user if possible.
* @param {express/request} req - the incoming request
*/
guarantee_auth_session(req){
if ( !req.session.auth ){
req.session.auth = {
uuid: uuid(),
flow: false,
user_id: false,
}
}
return req
}
}