mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 16:29:34 +00:00
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const passport = require('koa-passport');
|
|
const { isFunction } = require('lodash/fp');
|
|
|
|
const createLocalStrategy = require('./passport/local-strategy');
|
|
|
|
const authEventsMapper = {
|
|
onConnectionSuccess: 'admin.auth.success',
|
|
onConnectionError: 'admin.auth.error',
|
|
};
|
|
|
|
const valueIsFunctionType = ([, value]) => isFunction(value);
|
|
const keyIsValidEventName = ([key]) => {
|
|
return Object.keys(strapi.admin.services.passport.authEventsMapper).includes(key);
|
|
};
|
|
|
|
const getPassportStrategies = () => [createLocalStrategy(strapi)];
|
|
|
|
const registerAuthEvents = () => {
|
|
const { events = {} } = strapi.config.get('server.admin.auth', {});
|
|
const { authEventsMapper } = strapi.admin.services.passport;
|
|
|
|
const eventList = Object.entries(events)
|
|
.filter(keyIsValidEventName)
|
|
.filter(valueIsFunctionType);
|
|
|
|
for (const [eventName, handler] of eventList) {
|
|
strapi.eventHub.on(authEventsMapper[eventName], handler);
|
|
}
|
|
};
|
|
|
|
const init = () => {
|
|
strapi.admin.services.passport
|
|
.getPassportStrategies()
|
|
.forEach(strategy => passport.use(strategy));
|
|
|
|
registerAuthEvents();
|
|
|
|
return passport.initialize();
|
|
};
|
|
|
|
module.exports = {
|
|
init,
|
|
getPassportStrategies,
|
|
authEventsMapper,
|
|
};
|