Skip to content

Instantly share code, notes, and snippets.

View 1mehdifaraji's full-sized avatar
💻

Mehdi 1mehdifaraji

💻
View GitHub Profile
@1mehdifaraji
1mehdifaraji / otp.ts
Created November 11, 2023 03:43
OTP generation and verification with speakeasy library in express js framework
import speakeasy from "speakeasy";
export const generateOtp = (secret: string): string =>
speakeasy.time({
encoding: "base32",
secret,
digits: 4,
step: 300, // expire after 5 mins
});
@1mehdifaraji
1mehdifaraji / auth.ts
Last active November 11, 2023 03:36
Jsonwebtoken jwt token generation and verification with express js framework
import { RequestHandler } from "express";
import jwt, { Secret } from "jsonwebtoken";
import { errors } from "@util/locale/errors";
export const generateToken = (userId: string) =>
jwt.sign(userId, process.env.SECRET as Secret);
export const verifyToken = (async (req, res, next) => {
const bearerHeader = req.headers["authorization"] as string;
@1mehdifaraji
1mehdifaraji / regex.ts
Last active November 11, 2023 03:31
Regexes mostly for iran country
export const regexes = {
iran_melli_code: /^(?!(\d)\1{9})\d{10}$/,
number_only: /^[0-9\b]+$/,
latin_number_only: /^[0-9]+$/,
iran_phone: /^(0)9(0[1-5]|[1 3]\d|2[0-2]|9[0-4]|98)\d{7}$/,
iran_shaba: /^(?:IR)(?=.{24}$)[0-9]*$/,
};
// How to use
// const isValidPhone = regexes.iran_phone.test("09371101600")
@1mehdifaraji
1mehdifaraji / helper.ts
Created November 11, 2023 03:26
Persian price toLocaleString
export const toLocaleString = (
num: number,
options?: Intl.NumberFormatOptions
) => num.toLocaleString("fa-IR", options).replace(/\٬/g, ",");