payloadcms_otp_plugin
Version:
A comprehensive One-Time Password (OTP) authentication plugin for Payload CMS that enables secure passwordless authentication via SMS and email
97 lines (96 loc) • 4.08 kB
JavaScript
// import { loginWithMobileEndpointHandler, resendOtpEndpointHandler, submitOtpEndpointHandler } from './endpoints/customEndpointHandler.js'
import { OtpCode } from './collections/OtpCode.js';
import { enhanceUsersCollection } from './collections/enhanceUserCollection.js';
import { sendOtpEndpointHandler, loginWithMobileEndpointHandler, getOtpConfigEndpointHandler } from './endpoints/customEndpointHandler.js';
import { otpTranslation } from './translation/index.js';
export const otpPlugin = (pluginOptions)=>(config)=>{
if (!config.collections) {
config.collections = [];
}
// Create OTP Code collection with plugin options
// const otpCodeCollection = createOtpCodeCollection({
// ...pluginOptions.otpCodeCollection,
// expiredTime: pluginOptions.expiredTime,
// })
config.collections.push(OtpCode);
// Enhance collections specified in plugin options
if (pluginOptions.collections) {
config.collections = config.collections.map((collection)=>{
if (pluginOptions.collections && pluginOptions.collections[collection.slug]) {
// If it's the users collection, enhance it with OTP functionality
if (collection.slug === 'users') {
return enhanceUsersCollection(collection);
}
// For other collections, you could add other enhancements here
}
return collection;
});
}
/**
* If the plugin is disabled, we still want to keep added collections/fields so the database schema is consistent which is important for migrations.
* If your plugin heavily modifies the database schema, you may want to remove this property.
*/ if (pluginOptions.disabled) {
return config;
}
if (!config.endpoints) {
config.endpoints = [];
}
if (!config.admin) {
config.admin = {};
}
if (!config.admin.components) {
config.admin.components = {};
}
if (!config.admin.components.beforeDashboard) {
config.admin.components.beforeDashboard = [];
}
// Add translations if i18n is configured
if (config.i18n && config.i18n.translations) {
// Merge translations if i18n already exists
config.i18n.translations = {
...config.i18n.translations,
en: {
...config.i18n.translations?.en,
...otpTranslation.en
},
ar: {
...config.i18n.translations?.ar,
...otpTranslation.ar
}
};
}
// New service-integrated endpoints
config.endpoints.push({
handler: sendOtpEndpointHandler,
method: 'post',
path: "/otp/send"
});
config.endpoints.push({
handler: loginWithMobileEndpointHandler,
method: 'post',
path: "/otp/login"
});
config.endpoints.push({
handler: getOtpConfigEndpointHandler,
method: 'get',
path: "/otp/config"
});
// Store the hook in payload for access in endpoints
const incomingOnInit = config.onInit;
config.onInit = async (payload)=>{
// Ensure we are executing any existing onInit functions before running our own.
if (incomingOnInit) {
await incomingOnInit(payload);
}
// Store the plugin configuration in payload for component access
payload.otpPluginConfig = pluginOptions;
// Store the afterSetOtp hook in payload for endpoint access
if (pluginOptions.afterSetOtp) {
payload.otpPluginHooks = {
afterSetOtp: pluginOptions.afterSetOtp
};
}
};
return config;
};
//# sourceMappingURL=index.js.map