feat: kavenegar sdk, otp generation and verifying
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"backend/config"
|
||||
"backend/internal/domain"
|
||||
"backend/pkg/jwt"
|
||||
"backend/pkg/sms"
|
||||
"backend/pkg/validate/common"
|
||||
"backend/pkg/zohal"
|
||||
"context"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
jwt2 "github.com/golang-jwt/jwt/v5"
|
||||
"github.com/kavenegar/kavenegar-go"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -25,7 +27,9 @@ type authService struct {
|
||||
userRepo domain.UserRepo
|
||||
sessionRepo domain.SessionRepo
|
||||
challengeRepo domain.ChallengeRepo
|
||||
otpRepo domain.OTPRepo
|
||||
zohal *zohal.Zohal
|
||||
smsProvider *sms.Kavenegar
|
||||
challengeExp uint
|
||||
cfg config.Config
|
||||
}
|
||||
@@ -41,15 +45,19 @@ func NewAuthService(
|
||||
sessionRepo domain.SessionRepo,
|
||||
challengeRepo domain.ChallengeRepo,
|
||||
challengeExp uint,
|
||||
otpRepo domain.OTPRepo,
|
||||
zohal *zohal.Zohal,
|
||||
smsProvider *sms.Kavenegar,
|
||||
cfg config.Config,
|
||||
) AuthService {
|
||||
return &authService{
|
||||
userRepo: userRepo,
|
||||
sessionRepo: sessionRepo,
|
||||
challengeRepo: challengeRepo,
|
||||
otpRepo: otpRepo,
|
||||
challengeExp: challengeExp,
|
||||
zohal: zohal,
|
||||
smsProvider: smsProvider,
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
||||
@@ -190,6 +198,38 @@ func (s *authService) VerifyKYC(ctx context.Context, userID, nationalID, birthDa
|
||||
shahkarResp.StatusCode, identityResp.StatusCode)
|
||||
}
|
||||
|
||||
func (*authService) SendOTPVerifaction(ctx context.Context, phone string) (string, error) {
|
||||
return "", nil
|
||||
func (s *authService) SendOTPCode(ctx context.Context, phone string) (string, error) {
|
||||
otp := domain.NewOTP(phone)
|
||||
if err := s.otpRepo.Create(ctx, phone, otp); err != nil {
|
||||
return "", err
|
||||
}
|
||||
otpMsg := &sms.OTPMsg{
|
||||
Receptor: otp.Phone,
|
||||
Token: otp.Code,
|
||||
Template: s.cfg.Kavenegar.Template,
|
||||
// TODO: make sure when use VerfiyLookup Params
|
||||
Params: kavenegar.VerifyLookupParam{},
|
||||
}
|
||||
err := s.smsProvider.OTP(otpMsg)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return otp.Code, err
|
||||
}
|
||||
|
||||
func (s *authService) VerifyOTP(ctx context.Context, phone, code string) error {
|
||||
otp, err := s.otpRepo.Get(ctx, phone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if otp == nil {
|
||||
return errors.New("otp code not found or expired")
|
||||
}
|
||||
|
||||
if code != otp.Code {
|
||||
return errors.New("otp code is not valid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user