silvermobi/servers/grpcapi/server/account.go

52 lines
1.2 KiB
Go

package grpcserver
import (
"context"
"strings"
"git.coopgo.io/coopgo-apps/silvermobi/models"
grpcproto "git.coopgo.io/coopgo-apps/silvermobi/servers/grpcapi/proto"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func (s SilvermobiGRPCService) GetAccountInfo(ctx context.Context,
req *grpcproto.AccountInfoRequest) (res *grpcproto.AccountInfoResponse, err error) {
var (
md metadata.MD
account *models.Account
authHeader []string
ok bool
)
if md, ok = metadata.FromIncomingContext(ctx); !ok {
return nil, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
authHeader, ok = md["authorization"]
if !ok || len(authHeader) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
id := ExtractIdFromToken(tokenString)
if account, err = s.Handler.GetAccountInfos(context.Background(), id); err != nil {
return nil, err
}
log.Info().
Str("ID", account.ID).
Msg("GetAccountInfo")
return &grpcproto.AccountInfoResponse{
FirstName: account.FirstName,
LastName: account.LastName,
}, nil
}