This commit is contained in:
Dominic Wong
2021-03-25 22:53:48 +00:00
parent 81252c1611
commit 72aca7ee64
8 changed files with 56 additions and 35 deletions

View File

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