JSTP is a simple TypeScript package for generating and managing secure one-time passwords (OTPs) with Redis integration. JSTP provides flexible token generation with customizable formats and a Redis-based storage.
- Secure token generation in specified formats and lengths
- Redis-backed storage with automatic expiration
- Customizable token expiration
- Automatic token deletion after verification
- TypeScript support
npm install @ajiboladavid/jstp
- Node.js 14.x or later
- Redis instance
First, configure your Redis client before using the package
import { createClient } from "redis";
export const getRedisClient = async() => {
const client = createClient({
url: process.env.REDIS_URL
});
await client.connect();
return client;
}
Create a JSTP instance and generate tokens:
import Jstp from "jstp";
import { getRedisClient } from "./redisConfig";
const client = await getRedisClient();
const otpService = new Jstp(client);
const handleOtpGeneration = async () => {
try {
const token = await otpService.generateOTP({
length: 7,
format: "alphanumeric",
expiresIn: 3000,
identifier:"user1234"
});
console.log("token =>", token);
} catch (error) {
console.error("error =>", error);
}
};
Verify generated tokens:
try {
const isValid = await otpService.verifyOTP({
identifier: "user1234",
token: "ABC123" // Token to verify
});
if (isValid) {
console.log("Token is valid");
} else {
console.log("Token is invalid or expired");
}
} catch (error) {
console.error("error =>", error);
}
Syntax | Type | Description | Required |
---|---|---|---|
length | Number | Token length (4-12 characters) | Yes |
format | "numeric", "alphabetic", "alphanumeric" | Token character format | Yes |
expiresIn | number | Token expiration time in seconds | Yes |
identifier | string | Unique identifier for the token | Yes |
- numeric: Numbers only (0-9)
- alphabetic: Capital letters only (A-Z)
- alphanumeric: Numbers and capital letters
This package is licensed under the MIT License