feat: delete users - not tested
This commit is contained in:
130
backend/src/features/auth/domain/auth.service.ts
Normal file
130
backend/src/features/auth/domain/auth.service.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* @ai-summary Auth service business logic
|
||||
* @ai-context Coordinates between Auth0 Management API and local user profile database
|
||||
*/
|
||||
|
||||
import { auth0ManagementClient } from '../../../core/auth/auth0-management.client';
|
||||
import { UserProfileRepository } from '../../user-profile/data/user-profile.repository';
|
||||
import { logger } from '../../../core/logging/logger';
|
||||
import {
|
||||
SignupRequest,
|
||||
SignupResponse,
|
||||
VerifyStatusResponse,
|
||||
ResendVerificationResponse,
|
||||
} from './auth.types';
|
||||
|
||||
export class AuthService {
|
||||
constructor(private userProfileRepository: UserProfileRepository) {}
|
||||
|
||||
/**
|
||||
* Create a new user account
|
||||
* 1. Create user in Auth0 (which automatically sends verification email)
|
||||
* 2. Create local user profile with emailVerified=false
|
||||
*/
|
||||
async signup(request: SignupRequest): Promise<SignupResponse> {
|
||||
const { email, password } = request;
|
||||
|
||||
try {
|
||||
// Create user in Auth0 Management API
|
||||
// Auth0 automatically sends verification email on user creation
|
||||
const auth0UserId = await auth0ManagementClient.createUser({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
logger.info('Auth0 user created', { auth0UserId, email });
|
||||
|
||||
// Create local user profile
|
||||
const userProfile = await this.userProfileRepository.create(
|
||||
auth0UserId,
|
||||
email,
|
||||
undefined // displayName is optional
|
||||
);
|
||||
|
||||
logger.info('User profile created', { userId: userProfile.id, email });
|
||||
|
||||
return {
|
||||
userId: auth0UserId,
|
||||
email,
|
||||
message: 'Account created successfully. Please check your email to verify your account.',
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Signup failed', { email, error });
|
||||
|
||||
// Check for duplicate email error from Auth0
|
||||
if (error instanceof Error && error.message.includes('already exists')) {
|
||||
throw new Error('Email already exists');
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check email verification status
|
||||
* Queries Auth0 for current verification status and updates local database if changed
|
||||
*/
|
||||
async getVerifyStatus(auth0Sub: string): Promise<VerifyStatusResponse> {
|
||||
try {
|
||||
// Get user details from Auth0
|
||||
const userDetails = await auth0ManagementClient.getUser(auth0Sub);
|
||||
|
||||
logger.info('Retrieved user verification status from Auth0', {
|
||||
auth0Sub,
|
||||
emailVerified: userDetails.emailVerified,
|
||||
});
|
||||
|
||||
// Update local database if verification status changed
|
||||
const localProfile = await this.userProfileRepository.getByAuth0Sub(auth0Sub);
|
||||
|
||||
if (localProfile && localProfile.emailVerified !== userDetails.emailVerified) {
|
||||
await this.userProfileRepository.updateEmailVerified(
|
||||
auth0Sub,
|
||||
userDetails.emailVerified
|
||||
);
|
||||
logger.info('Local email verification status updated', {
|
||||
auth0Sub,
|
||||
emailVerified: userDetails.emailVerified,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
emailVerified: userDetails.emailVerified,
|
||||
email: userDetails.email,
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to get verification status', { auth0Sub, error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resend verification email
|
||||
* Calls Auth0 Management API to trigger verification email
|
||||
*/
|
||||
async resendVerification(auth0Sub: string): Promise<ResendVerificationResponse> {
|
||||
try {
|
||||
// Check if already verified
|
||||
const verified = await auth0ManagementClient.checkEmailVerified(auth0Sub);
|
||||
|
||||
if (verified) {
|
||||
logger.info('Email already verified, skipping resend', { auth0Sub });
|
||||
return {
|
||||
message: 'Email is already verified',
|
||||
};
|
||||
}
|
||||
|
||||
// Request Auth0 to resend verification email
|
||||
await auth0ManagementClient.resendVerificationEmail(auth0Sub);
|
||||
|
||||
logger.info('Verification email resent', { auth0Sub });
|
||||
|
||||
return {
|
||||
message: 'Verification email sent. Please check your inbox.',
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to resend verification email', { auth0Sub, error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user