Spaces:
Runtime error
Runtime error
Akshar2325 commited on
Commit Β·
fcc750f
1
Parent(s): 11cad63
fix(auth): improve robustness of user signup and email flow
Browse files- γauthγmake welcome email sending non-blocking to prevent signup failures
- γemailγenhance error handling for email API to manage non-json responses
feat(email): add detailed logging for traceability
- log email payloads, raw API responses, and success/error states
- improves debugging capabilities for email delivery issues
refactor(db): remove unused clientId field from UserSession
- clean up the prisma schema by removing the obsolete `clientId` column
Files changed (3)
109 unmodified lines110111112113114115116109 unmodified linesipAddress String?userAgent String? @db.TextgeoIpCountry String?clientId String?status SESSION_STATUS @default(CURRENT)loginAt DateTime? @db.Timestamp(3)logoutAt DateTime? @db.Timestamp(3)109 unmodified lines110111112113114115109 unmodified linesipAddress String?userAgent String? @db.TextgeoIpCountry String?status SESSION_STATUS @default(CURRENT)loginAt DateTime? @db.Timestamp(3)logoutAt DateTime? @db.Timestamp(3)
340 unmodified lines341342343344345346347348349350351352340 unmodified lines},});// Send welcome emailawait this.emailService.sendWelcomeEmail(email,user.firstName || 'User',user.lastName || '',);return {user: updatedUser,340 unmodified lines341342343344345346347348349350351352353354355356357340 unmodified lines},});// Send welcome email (don't block profile completion if email fails)try {await this.emailService.sendWelcomeEmail(email,user.firstName || 'User',user.lastName || '',);} catch (emailError) {// Log error but don't fail the requestconsole.error('Failed to send welcome email:', emailError);}return {user: updatedUser,
6 unmodified lines789101112133 unmodified lines171819202122232425262728293031323334353637383940414243444546474849505152534 unmodified lines585960616263641 unmodified line666768697071727374757677787980818283848586878889909192936 unmodified linesprivate readonly logger = new Logger(EmailService.name);private readonly config: EmailConfig;constructor(private configService: ConfigService) {this.config = new EmailConfig(configService);}3 unmodified linesfirstName?: string,): Promise<void> {try {const greeting = firstName ? `Hi ${firstName}` : 'Hello';const response = await fetch('https://api.brevo.com/v3/smtp/email', {method: 'POST',headers: {'api-key': this.config.apiKey,'Content-Type': 'application/json',},body: JSON.stringify({sender: {email: this.config.otpEmail,name: 'StreamFlix',},to: [{ email, name: firstName || email }],templateId: this.config.otpTemplateId,params: {GREETING: greeting,OTP: otp,},}),});if (!response.ok) {const error = await response.json();this.logger.error('Failed to send OTP email', error);throw new Error('Failed to send OTP email');}this.logger.log(`OTP email sent to ${email}`);} catch (error) {this.logger.error('Error sending OTP email', error);throw error;}}4 unmodified lineslastName: string,): Promise<void> {try {const fullName = `${firstName} ${lastName}`;const response = await fetch('https://api.brevo.com/v3/smtp/email', {method: 'POST',1 unmodified line'api-key': this.config.apiKey,'Content-Type': 'application/json',},body: JSON.stringify({sender: {email: this.config.welcomeEmail,name: 'StreamFlix',},to: [{ email, name: fullName }],templateId: this.config.welcomeTemplateId,params: {FIRSTNAME: fullName,},}),});if (!response.ok) {const error = await response.json();this.logger.error('Failed to send welcome email', error);throw new Error('Failed to send welcome email');}this.logger.log(`Welcome email sent to ${email}`);} catch (error) {this.logger.error('Error sending welcome email', error);throw error;}}6 unmodified lines789101112133 unmodified lines171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970714 unmodified lines7677787980818283848586878889909192939495969798991001011021 unmodified line1041051061071081091101111121131141151161171181191201211221231241251261271281296 unmodified linesprivate readonly logger = new Logger(EmailService.name);private readonly config: EmailConfig;constructor(configService: ConfigService) {this.config = new EmailConfig(configService);}3 unmodified linesfirstName?: string,): Promise<void> {try {this.logger.log(`π§ Attempting to send OTP email to: ${email}`);const greeting = firstName ? `Hi ${firstName}` : 'Hello';const payload = {sender: {email: this.config.otpEmail,name: 'StreamFlix',},to: [{ email, name: firstName || email }],templateId: this.config.otpTemplateId,params: {GREETING: greeting,OTP: otp,},};this.logger.log('π§ OTP Email payload:', {sender: payload.sender,to: payload.to,templateId: payload.templateId,params: payload.params,});const response = await fetch('https://api.brevo.com/v3/smtp/email', {method: 'POST',headers: {'api-key': this.config.apiKey,'Content-Type': 'application/json',},body: JSON.stringify(payload),});const responseText = await response.text();this.logger.log(`π§ Raw Response: ${responseText}`);if (!response.ok) {let error;try {error = JSON.parse(responseText);} catch {error = responseText;}this.logger.error('π§ Failed to send OTP email:', error);throw new Error('Failed to send OTP email');}this.logger.log(`π§ β OTP email sent successfully to ${email}`);} catch (error) {this.logger.error('π§ β Error sending OTP email:', error);throw error;}}4 unmodified lineslastName: string,): Promise<void> {try {this.logger.log(`π§ Attempting to send welcome email to: ${email}`);const fullName = `${firstName} ${lastName}`.trim();const payload = {sender: {email: this.config.welcomeEmail,name: 'StreamFlix',},to: [{ email, name: fullName || email }],templateId: this.config.welcomeTemplateId,params: {FIRSTNAME: fullName || firstName || 'User',},};this.logger.log('π§ Welcome Email payload:', {sender: payload.sender,to: payload.to,templateId: payload.templateId,params: payload.params,});const response = await fetch('https://api.brevo.com/v3/smtp/email', {method: 'POST',1 unmodified line'api-key': this.config.apiKey,'Content-Type': 'application/json',},body: JSON.stringify(payload),});const responseText = await response.text();this.logger.log(`π§ Raw Response: ${responseText}`);if (!response.ok) {let error;try {error = JSON.parse(responseText);} catch {error = responseText;}this.logger.error('π§ Failed to send welcome email:', error);throw new Error('Failed to send welcome email');}this.logger.log(`π§ β Welcome email sent successfully to ${email}`);} catch (error) {this.logger.error('π§ β Error sending welcome email:', error);throw error;}}
prisma/schema.prisma-1
src/modules/user/auth/auth.service.ts+11-6
src/shared/modules/email/email.service.ts+69-33