2018-01-19 15:14:51 +01:00

49 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
const Grant = require('grant-koa');
const mount = require('koa-mount');
module.exports = strapi => {
return {
beforeInitialize: function() {
strapi.config.middleware.load.after.push('provider');
},
initialize: function(cb) {
const grantConfig = Object.assign(_.clone(_.get(strapi.plugins['users-permissions'].config, 'grant', {})), {
server: {
protocol: 'http',
host: 'localhost:1337'
}
});
const grant = new Grant(grantConfig);
strapi.app.use(async (ctx, next) => {
if (_.startsWith(ctx.request.url, '/connect') && ctx.request.method === 'GET') {
const provider = _.last(ctx.request.url.split('/'));
const config = strapi.plugins['users-permissions'].config.grant[provider];
if (_.get(config, 'enabled')) {
await next();
} else {
return ctx.badRequest(null, 'This provider is disabled.');
}
} else {
await next();
}
});
strapi.app.use(mount(grant));
cb();
}
};
};