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

saksh-email-otp

v1.0.4

Published

A module for generating, sending, and validating OTPs (One-Time Passwords) with support for custom email sending logic.

Downloads

12

Readme

saksh email otp

The OTP Verification Module is a tool that helps make your application more secure by using One-Time Passwords (OTPs). These are temporary codes sent to users to verify their identity. The module can generate these codes, send them via email, and check if the codes entered by users are correct. It uses Node.js and MongoDB, making it reliable and scalable for different applications.

One of the best features of this module is that it allows you to use your own email sending service. You can provide a custom function to send OTPs, which means you can use any email provider or SMTP server you prefer. This flexibility ensures that the OTPs are sent out exactly how you want them to be. The module also checks if the email addresses are valid before sending the OTPs, which helps in reducing errors.

The module also tracks user information like IP addresses and device details. This information is used to calculate a "confidence score," which helps determine how likely it is that the OTP request is legitimate. For example, if the IP address and device match the user's previous records, the confidence score will be higher. This score can range from 0 to 100, with higher scores indicating a higher likelihood that the request is genuine. This added layer of security helps protect against unauthorized access.

In summary, the OTP Verification Module is a versatile and powerful tool for adding OTP-based security to your application. It is easy to integrate, allows for custom email sending, and includes advanced security features like confidence scoring. Whether you're building a new app or enhancing an existing one, this module provides the tools you need to ensure secure and reliable user verification.

Installation

npm install saksh-email-otp

Initialization

Initialize Database

To initialize the MongoDB connection, use the sakshInitializeDB function:

const { sakshInitializeDB } = require('saksh-email-otp');

sakshInitializeDB('mongodb://localhost:27017/yourdbname')
.then(() => console.log('Database connected'))
.catch(err => console.error('Database connection error:', err));

Usage

Send OTP

To send an OTP, use the sakshHandleSendOTP function. You can provide a custom email sending callback function:

    const { sakshHandleSendOTP } = require('saksh-email-otp');
    
    // Custom email sending callback function
    const sendEmailCallback = async (email, otp) => {
    const mailer = require('saksh-mailer');
    
    // Example SMTP configuration
    const smtpConfig = {
    host: 'smtp.example.com',
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {
    user: '[email protected]',
    pass: 'userpassword',
    },
    };
    
    const transporter = mailer.createTransport(smtpConfig);
    
    const mailOptions = {
    from: smtpConfig.auth.user,
    to: email,
    subject: 'Your OTP Code',
    text: `Your OTP is: ${otp}`,
    };
    
    const info = await transporter.sendMail(mailOptions);
    console.log('Email sent:', info.response);
    };
    
    // Send OTP
   

   

var deviceInfo = {
    userAgent: 'Mozilla/5.0',
    platform: 'Win32',
    screenResolution: '1920x1080',
    browser: 'Chrome'
};


var ip = '192.168.1.1';

var email = '[email protected]';




   sakshHandleSendOTP(email, ip, deviceInfo, sendEmailCallback).then(result => {


    console.log(result);
});

Validate OTP

To validate an OTP, use the `sakshHandleValidateOTP` function:

    const { sakshHandleValidateOTP } = require('saksh-email-otp');

    // Validate OTP
sakshHandleValidateOTP(email, '762636', ip, deviceInfo).then(result => {
    console.log(result);
});

Functions

sakshInitializeDB(uri)

Initializes the MongoDB connection.

  • uri (string): The MongoDB connection URI.

sakshHandleSendOTP(email, ip, deviceInfo, sendEmailCallback)

Sends an OTP to the specified email address.

  • email (string): The recipient's email address.
  • ip (string): The user's IP address.
  • deviceInfo (object): The user's device information.
  • sendEmailCallback (function): The callback function to send the email.

sakshHandleValidateOTP(email, otp, ip, deviceInfo)

Validates the submitted OTP.

  • email (string): The user's email address.
  • otp (string): The OTP submitted by the user.
  • ip (string): The user's IP address.
  • deviceInfo (object): The user's device information.

Internal Functions

sakshGenerateOTP()

Generates a One-Time Password (OTP).

sakshSendOTPEmail(email, otp, sendEmailCallback)

Sends an OTP via email using a callback function.

sakshIsValidEmail(email)

Validates if the provided email address is in a correct format.

sakshStoreOTP(email, otp, ip, deviceInfo)

Stores the generated OTP along with additional metadata.

sakshCalculateConfidence(email, currentIP, currentDeviceInfo)

Calculates the confidence level of the user's identity based on the provided email, IP address, and device information.

sakshCalculateDistance([lat1, lon1], [lat2, lon2])

Calculates the distance between two geographical coordinates using the Haversine formula.

sakshValidateOTP(email, submittedOTP, currentIP, currentDeviceInfo)

Validates the submitted OTP for a given email address, IP address, and device information.

License

This project is licensed under the MIT License.

Support

Susheel2339 at gmail.com