feat(auth): add remove code from redis

This commit is contained in:
Masood Keshvari 2024-01-14 20:20:09 +03:30
parent 866c5b42e1
commit 0760ab8fe2
6 changed files with 33 additions and 4 deletions

View File

@ -8,7 +8,7 @@ http_server:
port: 1313
mysql:
port: 3308
port: 3306
host: localhost
db_name: niki_db
username: niki

View File

@ -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:

View File

@ -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

View File

@ -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
}

View File

@ -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)

View File

@ -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 {