@nitra/cap 7.1.1 → 7.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/save-token/index.js +25 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitra/cap",
3
- "version": "7.1.1",
3
+ "version": "7.1.3",
4
4
  "description": "Nitra capacitor components",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,7 +1,7 @@
1
1
  import { Capacitor } from '@capacitor/core'
2
2
  import { FirebaseMessaging } from '@capacitor-firebase/messaging'
3
3
  import { getToken } from '@nitra/vite-boot/token'
4
- import { initializeApp } from 'firebase/app'
4
+ import { initializeApp, getApps } from 'firebase/app'
5
5
 
6
6
  /**
7
7
  * Save the Firebase push token for a given app and platform.
@@ -15,14 +15,17 @@ export async function saveToken() {
15
15
  return
16
16
  }
17
17
 
18
- initializeApp({
19
- apiKey: import.meta.env.VITE_API_KEY,
20
- authDomain: import.meta.env.VITE_AUTH_DOMAIN,
21
- projectId: import.meta.env.VITE_PROJECT_ID,
22
- storageBucket: import.meta.env.VITE_STORAGE_BUCKET,
23
- messagingSenderId: import.meta.env.VITE_MESSAGING_SENDER_ID,
24
- appId: import.meta.env.VITE_APP_ID
25
- })
18
+ // Check if Firebase is already initialized
19
+ if (getApps().length === 0) {
20
+ initializeApp({
21
+ apiKey: import.meta.env.VITE_API_KEY,
22
+ authDomain: import.meta.env.VITE_AUTH_DOMAIN,
23
+ projectId: import.meta.env.VITE_PROJECT_ID,
24
+ storageBucket: import.meta.env.VITE_STORAGE_BUCKET,
25
+ messagingSenderId: import.meta.env.VITE_MESSAGING_SENDER_ID,
26
+ appId: import.meta.env.VITE_APP_ID
27
+ })
28
+ }
26
29
 
27
30
  if (Capacitor.isNativePlatform() && !Capacitor.isPluginAvailable('FirebaseMessaging')) {
28
31
  console.error('FirebaseMessaging plugin is not available')
@@ -65,14 +68,19 @@ export async function saveToken() {
65
68
  * @returns {Promise<object>} Web Push token
66
69
  */
67
70
  async function getWebPushToken() {
68
- const serviceWorkerRegistration = await navigator.serviceWorker.register('sw.js', {
69
- type: 'module'
70
- })
71
+ try {
72
+ const serviceWorkerRegistration = await navigator.serviceWorker.register('sw.js', {
73
+ type: 'module'
74
+ })
71
75
 
72
- return FirebaseMessaging.getToken({
73
- vapidKey: import.meta.env.VITE_VAPID_KEY,
74
- serviceWorkerRegistration
75
- })
76
+ return await FirebaseMessaging.getToken({
77
+ vapidKey: import.meta.env.VITE_VAPID_KEY,
78
+ serviceWorkerRegistration
79
+ })
80
+ } catch (error) {
81
+ console.error('Error getting web push token:', error)
82
+ throw error
83
+ }
76
84
  }
77
85
 
78
86
  /**
@@ -81,7 +89,7 @@ async function getWebPushToken() {
81
89
  * @returns {Promise<void>}
82
90
  */
83
91
  async function sendTokenToServer(pushToken) {
84
- const response = await fetch('/n-push/save-user', {
92
+ const response = await fetch(`${import.meta.env.VITE_BASE_URL}/n-push/save-user`, {
85
93
  method: 'POST',
86
94
  headers: {
87
95
  'Content-Type': 'application/json',