[server][cast] Generate alphaNumeric deviceCode

This commit is contained in:
Neeraj Gupta 2024-05-03 12:29:32 +05:30
parent 48f24d48b5
commit 99b13d18b0
3 changed files with 4 additions and 15 deletions

View file

@ -9,8 +9,7 @@ type CastRequest struct {
}
type RegisterDeviceRequest struct {
DeviceCode *string `json:"deviceCode"`
PublicKey string `json:"publicKey" binding:"required"`
PublicKey string `json:"publicKey" binding:"required"`
}
type AuthContext struct {

View file

@ -28,7 +28,7 @@ func NewController(castRepo *castRepo.Repository,
}
func (c *Controller) RegisterDevice(ctx *gin.Context, request *cast.RegisterDeviceRequest) (string, error) {
return c.CastRepo.AddCode(ctx, request.DeviceCode, request.PublicKey, network.GetClientIP(ctx))
return c.CastRepo.AddCode(ctx, request.PublicKey, network.GetClientIP(ctx))
}
func (c *Controller) GetPublicKey(ctx *gin.Context, deviceCode string) (string, error) {

View file

@ -8,24 +8,14 @@ import (
"github.com/ente-io/stacktrace"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"strings"
)
type Repository struct {
DB *sql.DB
}
func (r *Repository) AddCode(ctx context.Context, code *string, pubKey string, ip string) (string, error) {
var codeValue string
var err error
if code == nil || *code == "" {
codeValue, err = random.GenerateSixDigitOtp()
if err != nil {
return "", stacktrace.Propagate(err, "")
}
} else {
codeValue = strings.TrimSpace(*code)
}
func (r *Repository) AddCode(ctx context.Context, pubKey string, ip string) (string, error) {
codeValue, err := random.GenerateAlphaNumString(6)
_, err = r.DB.ExecContext(ctx, "INSERT INTO casting (code, public_key, id, ip) VALUES ($1, $2, $3, $4)", codeValue, pubKey, uuid.New(), ip)
if err != nil {
return "", err