added transaction in registration
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m23s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m23s
This commit is contained in:
parent
3460b1ae87
commit
af256b66b5
@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"git.kanopo.dev/rhythm/rhythm-backend/internal/config"
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/config"
|
||||||
@ -26,16 +28,18 @@ type Service interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
|
pool *pgxpool.Pool
|
||||||
repo usersdb.Querier
|
repo usersdb.Querier
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
log *zap.SugaredLogger
|
log *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(repo usersdb.Querier, cfg *config.Config, log *zap.SugaredLogger) Service {
|
func NewService(repo usersdb.Querier, cfg *config.Config, log *zap.SugaredLogger, pool *pgxpool.Pool) Service {
|
||||||
return &service{
|
return &service{
|
||||||
repo: repo,
|
repo: repo,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
log: log,
|
log: log,
|
||||||
|
pool: pool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,18 +70,29 @@ func (s *service) Login(ctx context.Context, email, passwordPlain string) (*Auth
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Register(ctx context.Context, email, passwordPlain string) (*AuthResult, error) {
|
func (s *service) Register(ctx context.Context, email, passwordPlain string) (*AuthResult, error) {
|
||||||
_, err := s.repo.GetUserByEmail(ctx, email)
|
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
|
txRepo := usersdb.New(tx)
|
||||||
|
|
||||||
|
_, err = txRepo.GetUserByEmail(ctx, email)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s.log.Infof("registration failed: email already exists %v", email)
|
s.log.Infof("registration failed: email already exists %v", email)
|
||||||
return nil, ErrInvalidCredentials
|
return nil, ErrInvalidCredentials
|
||||||
}
|
}
|
||||||
|
if !errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
hash, err := password.HashPassword(passwordPlain)
|
hash, err := password.HashPassword(passwordPlain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := s.repo.CreateUser(ctx, usersdb.CreateUserParams{
|
user, err := txRepo.CreateUser(ctx, usersdb.CreateUserParams{
|
||||||
Email: email,
|
Email: email,
|
||||||
Password: hash,
|
Password: hash,
|
||||||
})
|
})
|
||||||
@ -85,6 +100,10 @@ func (s *service) Register(ctx context.Context, email, passwordPlain string) (*A
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
tokenPair, err := jwt.GenerateTokenPair(user.ID.String(), s.cfg)
|
tokenPair, err := jwt.GenerateTokenPair(user.ID.String(), s.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Error("failed to generate token pair", "error", err)
|
s.log.Error("failed to generate token pair", "error", err)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user