UNPKG

login-auth-services

Version:

Authentication services for Google, GitHub, Microsoft, okta and multi-factor authentication using OTP.

94 lines (93 loc) 4.18 kB
"use strict"; // import nodemailer from "nodemailer"; // import dotenv from "dotenv"; // import crypto from "crypto"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateOTPService = exports.initiateLoginWithEmailService = void 0; // dotenv.config(); // const transporter = nodemailer.createTransport({ // service: "gmail", // auth: { // user: process.env.EMAIL_USER, // pass: process.env.EMAIL_PASSWORD, // }, // }); // let OTPs: Record<string, string> = {}; // export const initiateLoginWithEmailService = async (email: string, userExists: (email: string) => Promise<boolean>) => { // if (typeof userExists === "function" && await userExists(email)) { // const otp = crypto.randomInt(100000, 999999).toString(); // OTPs[email] = otp; // await transporter.sendMail({ // from: process.env.EMAIL_USER, // to: email, // subject: "Your OTP for Login", // text: `Your OTP is: ${otp}`, // }); // return { message: "OTP sent to your email", success: true }; // } // return { message: "User not found", success: false }; // }; // export const validateOTPService = async (email: string, otp: string, getUser: (email: string) => Promise<any>) => { // if (typeof getUser === "function" && OTPs[email] === otp) { // delete OTPs[email]; // const user = await getUser(email); // if (user) { // return { user, message: "Login successful", success: true }; // } // } // return { message: "Invalid OTP or User not found", success: false }; // }; const nodemailer_1 = __importDefault(require("nodemailer")); const dotenv_1 = __importDefault(require("dotenv")); const crypto_1 = __importDefault(require("crypto")); const databaseService_1 = require("./databaseService"); dotenv_1.default.config(); const transporter = nodemailer_1.default.createTransport({ service: "gmail", auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASSWORD, }, }); const OTPs = {}; const initiateLoginWithEmailService = (email) => __awaiter(void 0, void 0, void 0, function* () { const dbService = databaseService_1.DatabaseService.getInstance(); const user = yield dbService.findUserByEmail(email); if (user) { const otp = crypto_1.default.randomInt(100000, 999999).toString(); OTPs[email] = otp; yield transporter.sendMail({ from: process.env.EMAIL_USER, to: email, subject: "Your OTP for Login", text: `Your OTP is: ${otp}`, }); return { message: "OTP sent to your email", success: true }; } return { message: "User not found", success: false }; }); exports.initiateLoginWithEmailService = initiateLoginWithEmailService; const validateOTPService = (email, otp) => __awaiter(void 0, void 0, void 0, function* () { const dbService = databaseService_1.DatabaseService.getInstance(); if (OTPs[email] === otp) { delete OTPs[email]; const user = yield dbService.findUserByEmail(email); if (user) { return { user, message: "Login successful", success: true }; } } return { message: "Invalid OTP or User not found", success: false }; }); exports.validateOTPService = validateOTPService;