mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-16 13:04:34 +00:00
codes
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/micro/v3/service/auth"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/codes/proto"
|
||||
@@ -10,6 +11,10 @@ import (
|
||||
)
|
||||
|
||||
func (c *Codes) Verify(ctx context.Context, req *pb.VerifyRequest, rsp *pb.VerifyResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.Code) == 0 {
|
||||
return ErrMissingCode
|
||||
@@ -18,9 +23,14 @@ func (c *Codes) Verify(ctx context.Context, req *pb.VerifyRequest, rsp *pb.Verif
|
||||
return ErrMissingIdentity
|
||||
}
|
||||
|
||||
db, err := c.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
// lookup the code
|
||||
var code Code
|
||||
if err := c.DB.Where(&Code{Code: req.Code, Identity: req.Identity}).First(&code).Error; err == gorm.ErrRecordNotFound {
|
||||
if err := db.Where(&Code{Code: req.Code, Identity: req.Identity}).First(&code).Error; err == gorm.ErrRecordNotFound {
|
||||
return ErrInvalidCode
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading code from database: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user