mirror of
https://github.com/strapi/strapi.git
synced 2025-08-03 14:28:40 +00:00
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
'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();
|
||
}
|
||
};
|
||
};
|