Pass Firebase project_id explicitly from service account JSON.
Parse FCM_SERVICE_ACCOUNT_KEY, validate project_id, and provide firebase.Config{ProjectID} during FCM client initialization to prevent missing-project-id messaging failures.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
4509fe2dc0
commit
7e75d79dc8
|
|
@ -95,12 +95,25 @@ func (s *Service) initFCMClient() error {
|
||||||
|
|
||||||
// Prepare client options; if a service account JSON string is provided, use it.
|
// Prepare client options; if a service account JSON string is provided, use it.
|
||||||
var opts []option.ClientOption
|
var opts []option.ClientOption
|
||||||
|
var fbConfig *firebase.Config
|
||||||
if s.config.FCMServiceAccountKey != "" {
|
if s.config.FCMServiceAccountKey != "" {
|
||||||
|
var sa struct {
|
||||||
|
ProjectID string `json:"project_id"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(s.config.FCMServiceAccountKey), &sa); err != nil {
|
||||||
|
return fmt.Errorf("invalid FCM_SERVICE_ACCOUNT_KEY JSON: %w", err)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(sa.ProjectID) == "" {
|
||||||
|
return fmt.Errorf("FCM_SERVICE_ACCOUNT_KEY is missing project_id")
|
||||||
|
}
|
||||||
|
fbConfig = &firebase.Config{
|
||||||
|
ProjectID: strings.TrimSpace(sa.ProjectID),
|
||||||
|
}
|
||||||
opts = append(opts, option.WithCredentialsJSON([]byte(s.config.FCMServiceAccountKey)))
|
opts = append(opts, option.WithCredentialsJSON([]byte(s.config.FCMServiceAccountKey)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize Firebase app
|
// Initialize Firebase app
|
||||||
app, err := firebase.NewApp(ctx, nil, opts...)
|
app, err := firebase.NewApp(ctx, fbConfig, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to initialize Firebase app: %w", err)
|
return fmt.Errorf("failed to initialize Firebase app: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user