feat: implemented smtp service;

feat: implemented registration emails;
fix: config variables for password length used the same env variable;
refactor: all available config variables added to docker-compose.yml
This commit is contained in:
2025-07-09 23:26:30 +03:00
parent 63b63038d1
commit 15c140db31
8 changed files with 171 additions and 21 deletions

View File

@@ -18,12 +18,14 @@
package services
import (
"easywish/config"
"easywish/internal/database"
errs "easywish/internal/errors"
"easywish/internal/models"
"easywish/internal/utils"
"easywish/internal/utils/enums"
"errors"
"fmt"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
@@ -39,11 +41,12 @@ type AuthService interface {
type authServiceImpl struct {
log *zap.Logger
smtp SmtpService
dbctx database.DbContext
}
func NewAuthService(_log *zap.Logger, _dbctx database.DbContext) AuthService {
return &authServiceImpl{log: _log, dbctx: _dbctx}
func NewAuthService(_log *zap.Logger, _dbctx database.DbContext, _smtp SmtpService) AuthService {
return &authServiceImpl{log: _log, dbctx: _dbctx, smtp: _smtp}
}
func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequest) (bool, error) {
@@ -81,7 +84,7 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
if _, err = db.TXQueries.CreateLoginInformation(db.CTX, database.CreateLoginInformationParams{
UserID: user.ID,
Email: request.Email,
Email: utils.NewPointer(request.Email),
PasswordHash: passwordHash, // Hashed in database
}); err != nil {
@@ -117,16 +120,31 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
zap.String("username", user.Username),
zap.Int64("id", user.ID))
if config.GetConfig().SmtpEnabled {
if err := a.smtp.SendEmail(
request.Email,
"Easywish",
fmt.Sprintf("Your registration code is %s", generatedCode),
); err != nil {
a.log.Error(
"Failed to send registration email",
zap.String("email", request.Email),
zap.String("username", request.Username),
zap.Error(err))
return false, errs.ErrServerError
}
} else {
a.log.Debug(
"Declated registration code for a new user. Enable SMTP in the config to disable this message",
zap.String("username", user.Username),
zap.String("code", generatedCode))
}
helper.Commit()
// TODO: get rid of this when email verification will start working
a.log.Debug(
"Declated registration code for a new user",
zap.String("username", user.Username),
zap.String("code", generatedCode))
// TODO: Send verification email
return true, nil
}