-แก้ไขข้อผิดำลาก redis

This commit is contained in:
2025-11-14 10:10:04 +07:00
parent b57513d346
commit 32d98b37aa
5 changed files with 19 additions and 20 deletions

View File

@@ -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 {

View File

@@ -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})`);

View File

@@ -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}`;

View File

@@ -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);

View File

@@ -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})`);