From 0760ab8fe276a8e4a782e8bb9ecd8d0f7ac114a4 Mon Sep 17 00:00:00 2001 From: masoodk Date: Sun, 14 Jan 2024 20:20:09 +0330 Subject: [PATCH] feat(auth): add remove code from redis --- config.yml | 2 +- docker-compose.yaml | 5 +++-- repository/mysql/dbconfig.yml | 2 +- repository/redis/redis_otp/delete_code.go | 19 +++++++++++++++++++ .../benefactor/benefactor/login_register.go | 8 ++++++++ service/benefactor/benefactor/service.go | 1 + 6 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 repository/redis/redis_otp/delete_code.go diff --git a/config.yml b/config.yml index dc69487..4e90d9f 100644 --- a/config.yml +++ b/config.yml @@ -8,7 +8,7 @@ http_server: port: 1313 mysql: - port: 3308 + port: 3306 host: localhost db_name: niki_db username: niki diff --git a/docker-compose.yaml b/docker-compose.yaml index 7093b28..fd124ec 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,10 +4,10 @@ services: mysql: image: mysql:8.0 ports: - - "3308:3306" + - "3306:3306" container_name: niki-database volumes: - - dbdata:/var/lib/mysql + - niki-db-data:/data restart: always command: [ 'mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci' ] environment: @@ -34,3 +34,4 @@ services: volumes: dbdata: niki-redis-data: + niki-db-data: diff --git a/repository/mysql/dbconfig.yml b/repository/mysql/dbconfig.yml index 561a3f7..addf2ad 100644 --- a/repository/mysql/dbconfig.yml +++ b/repository/mysql/dbconfig.yml @@ -1,5 +1,5 @@ production: dialect: mysql - datasource: niki:nikiappt0lk2o20(localhost:3308)/niki_db?parseTime=true + datasource: niki:nikiappt0lk2o20(localhost:3306)/niki_db?parseTime=true dir: repository/mysql/migration table: gorp_migrations diff --git a/repository/redis/redis_otp/delete_code.go b/repository/redis/redis_otp/delete_code.go new file mode 100644 index 0000000..85a6a51 --- /dev/null +++ b/repository/redis/redis_otp/delete_code.go @@ -0,0 +1,19 @@ +package redisotp + +import ( + "context" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (d DB) DeleteCodeByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) { + const op = "redisotp.GetCodeByPhoneNumber" + + success, err := d.adapter.Client().Del(ctx, phoneNumber).Result() + if err != nil { + return false, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + if success != 1 { + return false, nil + } + return true, nil +} diff --git a/service/benefactor/benefactor/login_register.go b/service/benefactor/benefactor/login_register.go index 2b2845a..d3bf72a 100644 --- a/service/benefactor/benefactor/login_register.go +++ b/service/benefactor/benefactor/login_register.go @@ -19,6 +19,14 @@ func (s Service) LoginOrRegister(ctx context.Context, req benefactoreparam.Login return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgOtpCodeIsNotValid).WithKind(richerror.KindForbidden) } + deleted, dErr := s.redisOtp.DeleteCodeByPhoneNumber(ctx, req.PhoneNumber) + if dErr != nil { + return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) + } + if !deleted { + //TODO - add to log + } + isExist, benefactor, rErr := s.repo.IsExistBenefactorByPhoneNumber(ctx, req.PhoneNumber) if rErr != nil { return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected) diff --git a/service/benefactor/benefactor/service.go b/service/benefactor/benefactor/service.go index c806190..4614483 100644 --- a/service/benefactor/benefactor/service.go +++ b/service/benefactor/benefactor/service.go @@ -26,6 +26,7 @@ type RedisOtp interface { IsExistPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) SaveCodeWithPhoneNumber(ctx context.Context, phoneNumber string, code string, expireTime time.Duration) error GetCodeByPhoneNumber(ctx context.Context, phoneNumber string) (string, error) + DeleteCodeByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) } type SmsProviderClient interface {