Skip to content

Commit

Permalink
if mail disable then assume register user is verified
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHallen122 committed Mar 5, 2025
1 parent 98319d6 commit b64b2a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ NODE_ENV="DEV"


# mail
# Set to false to disable all email functionality
MAIL_ENABLED=True

MAIL_HOST=smtp.example.com
MAIL_USER=user@example.com
MAIL_PASSWORD=topsecret
Expand Down
37 changes: 29 additions & 8 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { MailService } from 'src/mail/mail.service';

@Injectable()
export class AuthService {
private readonly isMailEnabled: boolean;

constructor(
@InjectRepository(User)
private userRepository: Repository<User>,
Expand All @@ -40,7 +42,12 @@ export class AuthService {
private roleRepository: Repository<Role>,
@InjectRepository(RefreshToken)
private refreshTokenRepository: Repository<RefreshToken>,
) {}
) {
// Read the MAIL_ENABLED environment variable, default to 'true'
this.isMailEnabled =
this.configService.get<string>('MAIL_ENABLED', 'true').toLowerCase() ===
'true';
}

async confirmEmail(token: string): Promise<EmailConfirmationResponse> {
try {
Expand Down Expand Up @@ -147,15 +154,29 @@ export class AuthService {
}

const hashedPassword = await hash(password, 10);
const newUser = this.userRepository.create({
username,
email,
password: hashedPassword,
isEmailConfirmed: false,
});

let newUser;
if (this.isMailEnabled) {
newUser = this.userRepository.create({
username,
email,
password: hashedPassword,
isEmailConfirmed: false,
});
} else {
newUser = this.userRepository.create({
username,
email,
password: hashedPassword,
isEmailConfirmed: true,
});
}

await this.userRepository.save(newUser);
await this.sendVerificationEmail(newUser);

if (this.isMailEnabled) {
await this.sendVerificationEmail(newUser);
}

return newUser;
}
Expand Down

0 comments on commit b64b2a2

Please sign in to comment.