46 lines
946 B
Go
46 lines
946 B
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.logidex.ru/fakz9/logidex-id/internal/api/auth/domain"
|
|
"github.com/redis/rueidis"
|
|
)
|
|
|
|
type authRepo struct {
|
|
redisClient rueidis.Client
|
|
}
|
|
|
|
func (a authRepo) GetOtpRequest(ctx context.Context, uuid string) (*string, error) {
|
|
redisClient := a.redisClient
|
|
|
|
resp, err := redisClient.Do(ctx, redisClient.B().Get().Key("otp:"+uuid).Build()).ToString()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp == "" {
|
|
// create error
|
|
return nil, &domain.ErrOtpNotFound{Uuid: uuid}
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|
|
|
|
func (a authRepo) SaveOtpRequest(ctx context.Context, uuid string, code string) error {
|
|
redisClient := a.redisClient
|
|
|
|
err := redisClient.Do(ctx, redisClient.B().Set().Key("otp:"+uuid).Value(code).Ex(120*time.Second).Build()).Error()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func NewAuthRepo(redisClient rueidis.Client) domain.AuthRepository {
|
|
return &authRepo{redisClient: redisClient}
|
|
}
|