silvermobi/servers/grpcapi/server/account.go

36 lines
1.1 KiB
Go
Raw Normal View History

2023-12-08 06:35:04 +00:00
package grpcserver
import (
"context"
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"
"strings"
)
func (s SilvermobiGRPCService) GetAccountInfo(ctx context.Context, req *grpcproto.AccountInfoRequest) (res *grpcproto.AccountInfoResponse, err error) {
md, ok := metadata.FromIncomingContext(ctx)
if !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)
account, err := s.Handler.GetAccountInfos(context.Background(), id)
log.Info().
Str("ID", account.ID).
Msg("GetAccountInfo")
if err != nil {
return nil, err
}
return &grpcproto.AccountInfoResponse{
FirstName: account.FirstName,
LastName: account.LastName,
}, nil
}