Customization
Learn how to customize the @perseidesjs/auth-otp
plugin in your Medusa app
There are three ways to integrate the auth-otp plugin into your Medusa application, each offering different levels of customization.
Method 1: Simple Direct Integration
For the most straightforward setup, simply add the plugin to your medusa-config.ts
file:
With this approach, the plugin will use its default configuration.
Method 2: Custom Options
When you need more control over how OTP works in your application, you can provide custom options:
This configuration gives you the ability to fine-tune the OTP experience.
Method 3: Advanced Integration with Auth Module
For advanced use cases, such as creating accounts with OTP-only authentication (no passwords), we can extend Medusa’s Auth Module:
This configuration creates a new authentication system where OTP becomes a first-class authentication method alongside traditional password authentication.
When using the otp
provider, you’re planning to add a new Auth Provider to your Medusa apps, allowing you to create new accounts with OTP-only authentication, if your goal is just to have OTP as a secondary authentication method, do not use this method
and stick to the plugin approach only.
Default options
When you don’t specify options, the plugin uses these sensible defaults:
digits
: The number of digits in the OTP to generate.ttl
: The time to live for the OTP in seconds
Accessors
The accessorsPerActor
configuration controls how the plugin identifies and retrieves actors (users, customers or any custom actor) when processing OTP requests. This configuration specifies two critical fields for each actor type:
The two key properties are:
-
accessor
: Defines which field is used to initially locate the actor in the database when a request is made. In the example above, customers would be looked up by theiremail
field. -
entityIdAccessor
: Specifies which field from the actor object should be used to find the corresponding auth identity.
When processing an OTP request, the plugin:
- Uses the
accessor
field to find the actor record - Extracts the value from the
entityIdAccessor
field from that actor record - Uses this extracted value to query for matching auth identities
For example, if you’ve registered customers using the standard emailpass
provider (which uses email as the entity ID), but want to allow OTP authentication via phone numbers, you would configure:
This tells the system: “Find the customer by their phone number, then look up their auth identity using their email field.”
You can also use an array of accessors to support multiple ways to identify the actor:
In that case, the plugin will try to find the actor using the first accessor in the array, and if it doesn’t find it, it will try the next one.
The default configuration uses email for both finding actors and determining their identity:
HTTP Options
The http
configuration provides options for handling HTTP requests:
-
alwaysReturnSuccess
: Determines whether the plugin should always return a success response, even if an error occurs. This helps prevent data leakage by ensuring that the response is not affected by errors. -
warnOnError
: Logs a warning when an error occurs during OTP generation. This helps you catch and handle errors that might occur during OTP generation.
Was this page helpful?