silvermobi/servers/grpcapi/server/firebase.go

60 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-12-08 06:35:04 +00:00
package grpcserver
import (
"context"
2024-10-31 19:32:54 +00:00
"strings"
2023-12-08 06:35:04 +00:00
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"
)
2024-10-31 19:32:54 +00:00
func (s SilvermobiGRPCService) PutFirebaseToken(ctx context.Context,
req *grpcproto.FirebaseTokenRequest) (resp *grpcproto.FirebaseTokenResponse, err error) {
var (
ok bool
md metadata.MD
authHeader []string
)
2023-12-08 06:35:04 +00:00
if req.Token == "" || req.DevicePlatform == "" {
return &grpcproto.FirebaseTokenResponse{
Result: false,
}, nil
}
2024-10-31 19:32:54 +00:00
if md, ok = metadata.FromIncomingContext(ctx); !ok {
2023-12-08 06:35:04 +00:00
return &grpcproto.FirebaseTokenResponse{
Result: false,
}, status.Errorf(codes.Unauthenticated, "Missing metadata")
}
2024-10-31 19:32:54 +00:00
authHeader, ok = md["authorization"]
2023-12-08 06:35:04 +00:00
if !ok || len(authHeader) == 0 {
return &grpcproto.FirebaseTokenResponse{
Result: false,
}, status.Errorf(codes.Unauthenticated, "Missing authorization header")
}
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
id := ExtractIdFromToken(tokenString)
log.Info().
Str("User ID", id).
Msg("PutFirebaseToken")
2024-10-31 19:32:54 +00:00
if err = s.Handler.PutFirebase(context.Background(), id, req.Token, req.DevicePlatform); err != nil {
2023-12-08 06:35:04 +00:00
return &grpcproto.FirebaseTokenResponse{
Result: false,
}, status.Errorf(codes.Unknown, "Database error")
}
2024-10-31 19:32:54 +00:00
2023-12-08 06:35:04 +00:00
return &grpcproto.FirebaseTokenResponse{
Result: true,
}, nil
}