diff --git a/services/auth/auth.service.ts b/services/auth/auth.service.ts index ae90e26..a643100 100644 --- a/services/auth/auth.service.ts +++ b/services/auth/auth.service.ts @@ -1,13 +1,20 @@ import { v4 as uuidv4 } from 'uuid'; -import { dbService } from '../../services/couchdb.factory'; import { AuthenticatedUser } from './auth.types'; import { EmailVerificationService } from './emailVerification.service'; +import { dbService } from '../couchdb.factory'; + const emailVerificationService = new EmailVerificationService(); const authService = { async register(email: string, password: string, username?: string) { try { + // Check if user already exists + const existingUser = await dbService.findUserByEmail(email); + if (existingUser) { + throw new Error('User already exists'); + } + // Create user with password const user = await dbService.createUserWithPassword( email, @@ -24,7 +31,7 @@ const authService = { return { user, verificationToken }; } catch (error) { if (error.message.includes('already exists')) { - throw new Error('An account with this email already exists'); + throw new Error('User already exists'); } throw error; } @@ -57,6 +64,11 @@ const authService = { ); } + // Check if email is verified + if (!user.emailVerified) { + throw new Error('Email verification required'); + } + // Simple password verification (in production, use bcrypt) console.warn('🔍 Comparing passwords:', { inputPassword: input.password, @@ -66,7 +78,7 @@ const authService = { if (user.password !== input.password) { console.warn('❌ Password mismatch'); - throw new Error('Invalid password'); + throw new Error('Invalid credentials'); } console.warn('✅ Login successful for:', user.email); @@ -221,7 +233,6 @@ const authService = { throw new Error('Password must be at least 6 characters long'); } - // Update password const updatedUser = await dbService.changeUserPassword( resetToken.userId, newPassword diff --git a/services/auth/emailVerification.service.ts b/services/auth/emailVerification.service.ts index 0ff9978..32b8035 100644 --- a/services/auth/emailVerification.service.ts +++ b/services/auth/emailVerification.service.ts @@ -2,6 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import { EmailVerificationToken, AuthenticatedUser } from './auth.types'; import { mailgunService } from '../mailgun.service'; import { AccountStatus } from './auth.constants'; +import { dbService } from '../couchdb.factory'; const TOKEN_EXPIRY_HOURS = 24; @@ -62,7 +63,6 @@ export class EmailVerificationService { } // Find the user (in production, this would be a proper database lookup) - const { dbService } = await import('../couchdb'); const user = await dbService.findUserByEmail(verificationToken.email); return user as AuthenticatedUser; @@ -70,7 +70,6 @@ export class EmailVerificationService { async markEmailVerified(user: AuthenticatedUser): Promise { // Update user in database - const { dbService } = await import('../couchdb'); const updatedUser = { ...user, emailVerified: true,