47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
|
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) PutFirebaseToken(ctx context.Context, req *grpcproto.FirebaseTokenRequest) (resp *grpcproto.FirebaseTokenResponse, err error) {
|
||
|
|
||
|
if req.Token == "" || req.DevicePlatform == "" {
|
||
|
return &grpcproto.FirebaseTokenResponse{
|
||
|
Result: false,
|
||
|
}, nil
|
||
|
}
|
||
|
md, ok := metadata.FromIncomingContext(ctx)
|
||
|
if !ok {
|
||
|
return &grpcproto.FirebaseTokenResponse{
|
||
|
Result: false,
|
||
|
}, status.Errorf(codes.Unauthenticated, "Missing metadata")
|
||
|
}
|
||
|
authHeader, ok := md["authorization"]
|
||
|
if !ok || len(authHeader) == 0 {
|
||
|
return &grpcproto.FirebaseTokenResponse{
|
||
|
Result: false,
|
||
|
}, status.Errorf(codes.Unauthenticated, "Missing authorization header")
|
||
|
}
|
||
|
tokenString := strings.TrimPrefix(authHeader[0], "Bearer ")
|
||
|
id := ExtractIdFromToken(tokenString)
|
||
|
log.Info().
|
||
|
Str("User ID", id).
|
||
|
Msg("PutFirebaseToken")
|
||
|
err = s.Handler.PutFirebase(context.Background(), id, req.Token, req.DevicePlatform)
|
||
|
if err != nil {
|
||
|
return &grpcproto.FirebaseTokenResponse{
|
||
|
Result: false,
|
||
|
}, status.Errorf(codes.Unknown, "Database error")
|
||
|
}
|
||
|
return &grpcproto.FirebaseTokenResponse{
|
||
|
Result: true,
|
||
|
}, nil
|
||
|
}
|