diff --git a/exthernal-login-api/src/middlewares/verifyEmailHandler.js b/exthernal-login-api/src/middlewares/verifyEmailHandler.js index caa253f..2e6b681 100644 --- a/exthernal-login-api/src/middlewares/verifyEmailHandler.js +++ b/exthernal-login-api/src/middlewares/verifyEmailHandler.js @@ -1,9 +1,8 @@ -import Redis from 'ioredis'; import { GeneralService } from '../share/generalservice.js'; +import redis from '../utils/redis.js'; // import { sendError } from './response.js'; export async function verifyEmailHandler(req, res) { - const redis = new Redis(); const generalService = new GeneralService(); try { diff --git a/exthernal-login-api/src/services/otpverifyservice.js b/exthernal-login-api/src/services/otpverifyservice.js index 7bf59f6..234a1f5 100644 --- a/exthernal-login-api/src/services/otpverifyservice.js +++ b/exthernal-login-api/src/services/otpverifyservice.js @@ -1,24 +1,24 @@ -import Redis from 'ioredis'; import crypto from 'crypto'; import { sendError } from '../utils/response.js'; import { GeneralService } from '../share/generalservice.js'; +import redis from '../utils/redis.js'; export class OTPVerifyService { constructor() { - this.redis = new Redis(); + // this.redis = new redis(); this.generalService = new GeneralService(); } async verifyOtp(email, otp) { - const storedOtp = await this.redis.get(`otp:${email}`); + const storedOtp = await redis.get(`otp:${email}`); if (!storedOtp || storedOtp !== otp) { throw sendError('รหัส OTP ไม่ถูกต้องหรือหมดอายุ', 'Invalid OTP'); } - await this.redis.del(`otp:${email}`); + await redis.del(`otp:${email}`); const resetToken = crypto.randomBytes(32).toString('hex'); - await this.redis.set(`reset:${email}`, resetToken, 'EX', 600); // TTL 10 นาที + await redis.set(`reset:${email}`, resetToken, 'EX', 600); // TTL 10 นาที this.generalService.devhint(1, 'otpverifyservice.js', `OTP Verified → Reset Token issued (${email})`); diff --git a/exthernal-login-api/src/services/registerservice.js b/exthernal-login-api/src/services/registerservice.js index 2b93131..70e838e 100644 --- a/exthernal-login-api/src/services/registerservice.js +++ b/exthernal-login-api/src/services/registerservice.js @@ -1,14 +1,14 @@ -import Redis from 'ioredis'; import bcrypt from 'bcrypt'; import crypto from 'crypto'; import nodemailer from 'nodemailer'; import { GeneralService } from '../share/generalservice.js'; import { sendError } from '../utils/response.js'; +import redis from '../utils/redis.js'; export class RegisterService { constructor() { - this.redis = new Redis(); + // this.redis = new Redis(); this.generalService = new GeneralService(); } @@ -31,7 +31,7 @@ export class RegisterService { const payload = JSON.stringify({ fname, lname, hashedPwd, token, database }); - await this.redis.set(`verify:${email}`, payload, 'EX', 86400); // 24h + await redis.set(`verify:${email}`, payload, 'EX', 86400); // 24h const verifyUrl = `http://localhost:1012/login/verify-email?token=${token}&email=${encodeURIComponent(email)}&organization=${database}`; diff --git a/exthernal-login-api/src/services/resetpasswordservice.js b/exthernal-login-api/src/services/resetpasswordservice.js index bfaecfd..de4f341 100644 --- a/exthernal-login-api/src/services/resetpasswordservice.js +++ b/exthernal-login-api/src/services/resetpasswordservice.js @@ -1,23 +1,23 @@ -import Redis from 'ioredis'; import bcrypt from 'bcrypt'; import { sendError } from '../utils/response.js'; import { GeneralService } from '../share/generalservice.js'; +import redis from '../utils/redis.js'; export class ResetPasswordService { constructor() { - this.redis = new Redis(); + // this.redis = new Redis(); this.generalService = new GeneralService(); } async resetPassword(email, token, newPassword) { let database = ''; - const storedToken = await this.redis.get(`reset:${email}`); + const storedToken = await redis.get(`reset:${email}`); if (!storedToken || storedToken !== token) { throw sendError('Token ไม่ถูกต้องหรือหมดอายุ', 'Invalid or expired token'); } - await this.redis.del(`reset:${email}`); + await redis.del(`reset:${email}`); // อัปเดตรหัสผ่านในฐานข้อมูลจริง const hashedPwd = await bcrypt.hash(newPassword, 10); diff --git a/exthernal-login-api/src/services/verifyemailservice.js b/exthernal-login-api/src/services/verifyemailservice.js index 02a05f5..e296bff 100644 --- a/exthernal-login-api/src/services/verifyemailservice.js +++ b/exthernal-login-api/src/services/verifyemailservice.js @@ -1,17 +1,17 @@ -import Redis from 'ioredis'; import { GeneralService } from '../share/generalservice.js'; +import redis from '../utils/redis.js'; import { sendError } from '../utils/response.js'; export class VerifyEmailService { constructor() { - this.redis = new Redis(); + // this.redis = new Redis(); this.generalService = new GeneralService(); } async verifyAndCreate({ email, token, schema = 'nuttakit' }) { // ✅ STEP 1: โหลด payload จาก Redis const key = `verify:${email}`; - const stored = await this.redis.get(key); + const stored = await redis.get(key); if (!stored) { throw sendError('ลิงก์หมดอายุหรือไม่ถูกต้อง', 'Verification link expired or invalid', 400); } @@ -20,7 +20,7 @@ export class VerifyEmailService { try { parsed = JSON.parse(stored); } catch (ex) { - await this.redis.del(key).catch(() => {}); + await redis.del(key).catch(() => {}); throw sendError('ข้อมูลการยืนยันไม่ถูกต้อง', 'Invalid verify payload', 400); } @@ -36,7 +36,7 @@ export class VerifyEmailService { const checkResult = await this.generalService.executeQueryParam(schema, checkSql, [email]); if (checkResult && checkResult.length > 0) { - await this.redis.del(key).catch(() => {}); + await redis.del(key).catch(() => {}); throw sendError('อีเมลนี้ถูกใช้แล้วในองค์กรนี้', 'Email already registered in this organization', 400); } @@ -49,7 +49,7 @@ export class VerifyEmailService { await this.generalService.executeQueryParam(schema, insertSql, params); // ✅ STEP 5: ลบ Redis Key (เคลียร์ payload) - await this.redis.del(key).catch(() => {}); + await redis.del(key).catch(() => {}); this.generalService.devhint(2, 'verifyemailservice.js', `✅ Account verified (${email})`);