npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strapi-otp-plugin

v1.0.1

Published

A custom plugin for sending OTP and generating JWT token

Downloads

7

Readme

OTP Plugin for Strapi

A Strapi plugin that provides functionality for generating and validating OTPs (One-Time Passwords) for phone number verification. This plugin uses Twilio to send OTPs via SMS, making it easy to add OTP-based authentication to your Strapi application.

Features

  • Generate OTP and send it to a phone number using Twilio.
  • Validate OTP with expiration time and usage status.
  • Customizable OTP expiration duration.
  • Log and manage OTP requests through the Strapi admin panel.

Prerequisites

Before using this plugin, you must have a Twilio account. You'll need the following credentials from Twilio:

  • TWILIO_ACCOUNT_SID
  • TWILIO_AUTH_TOKEN
  • TWILIO_PHONE_NUMBER Make sure to store these in your .env file for use.

Installation

Step 1: Install via NPM To install the plugin, run the following command in your project:

npm install otp-plugin 

Step 2: Enable the Plugin In your config/plugins.js file, enable the otp-plugin:

module.exports = {
  'otp-plugin': {
    enabled: true,
    resolve: './node_modules/otp-plugin',
  },
}; 

Step 3: Configure Environment Variables You need to configure the following environment variables in your .env file to enable Twilio for sending OTPs via SMS:

TWILIO_ACCOUNT_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_PHONE_NUMBER=your_twilio_phone_number

Step 4: Configure HTTPS Agent (Optional) If your environment requires custom HTTPS settings (e.g., for self-signed certificates), configure the HTTPS agent in server/config/index.js:

const https = require('https');

const agent = new https.Agent({
  rejectUnauthorized: false,
});

module.exports = {
  http: {
    agent,
  },
};

Usage

This plugin provides several API endpoints to generate, validate, and manage OTP logins. Below are the key endpoints:

Generate OTP

Generates an OTP and sends it to the specified phone number.

  • Method: POST
  • Path: /otp-logins/generate
  • Body Parameters:
    • phoneNumber (string): The phone number to which the OTP should be sent.

Example:

curl -X POST http://localhost:1337/otp-logins/generate \
-H "Content-Type: application/json" \
-d '{"phoneNumber": "1234567890"}'

Validate OTP

Validates the OTP for the given phone number.

  • Method: POST
  • Path: /otp-logins/validate
  • Body Parameters:
    • phoneNumber (string): The phone number associated with the OTP.
    • otpCode (string): The OTP code to be validated.

Example:

curl -X POST http://localhost:1337/otp-logins/validate \
-H "Content-Type: application/json" \
-d '{"phoneNumber": "1234567890", "otpCode": "123456"}'

More Endpoints

Find OTP Logins

Retrieve a list of all OTP login records.

  • Method: GET
  • Path: /otp-logins

Find One OTP Login

Retrieve a specific OTP login record by ID.

  • Method: GET
  • Path: /otp-logins/:id

Create OTP Login

Manually create a new OTP login record.

  • Method: POST
  • Path: /otp-logins

Update OTP Login

Update an existing OTP login record by ID.

  • Method: PUT
  • Path: /otp-logins/:id

Delete OTP Login

Delete an OTP login record by ID.

  • Method: DELETE
  • Path: /otp-logins/:id

Strapi Admin Panel

The OTP plugin creates a content type called OtpLogin in your Strapi admin panel. You can view and manage OTP login records, including phone numbers, OTP codes, expiration times, and usage status.

Services

The plugin exposes services that can be called from other parts of your Strapi application:

Generate and Create OTP

Generates and sends an OTP to the given phone number and logs the request in the database.

const otpService = strapi.plugin('otp-plugin').service('otp');
await otpService.generateAndCreateOtp('1234567890');

Validate OTP

Validates the OTP for the given phone number.

const isValid = await otpService.validateOtp('1234567890', '123456');

License

This plugin is licensed under the MIT License.