@quintype/framework 7.33.6-fcm-fix.0 → 7.33.6-fcm-fix.1

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/client/impl/fcm.js +65 -39
  2. package/package.json +1 -1
@@ -1,53 +1,79 @@
1
- export function initializeFCM(firebaseConfig, config) {
2
- console.log("opts--------", firebaseConfig);
1
+ export function initializeFCM (firebaseConfig, config) {
2
+ console.log('opts--------', firebaseConfig)
3
3
  Promise.all([
4
- import(/* webpackChunkName: "firebase-app" */ "firebase/app"),
5
- import(/* webpackChunkName: "firebase-messaging" */ "firebase/messaging"),
4
+ import(/* webpackChunkName: "firebase-app" */ 'firebase/app'),
5
+ import(/* webpackChunkName: "firebase-messaging" */ 'firebase/messaging'),
6
+ import(/* webpackChunkName: "firebase-auth" */ 'firebase/auth')
6
7
  ])
7
- .then(([firebase, m]) => {
8
+ .then(([firebase, m, a]) => {
8
9
  const app = firebase.initializeApp({
9
10
  messagingSenderId: firebaseConfig.messagingSenderId.toString(),
10
11
  projectId: firebaseConfig.projectId,
11
12
  apiKey: firebaseConfig.apiKey,
12
13
  storageBucket: firebaseConfig.storageBucket,
13
14
  authDomain: firebaseConfig.authDomain,
14
- appId: firebaseConfig.appId,
15
- });
16
- const messaging = m.getMessaging(app);
17
- return firebaseConfig?.vapidKey ? m.getToken(messaging, {
18
- vapidKey: `${firebaseConfig?.vapidKey}`
19
- }) : m.getToken(messaging);
15
+ appId: firebaseConfig.appId
16
+ })
17
+ const token = getToken(app, m, firebaseConfig?.vapidKey)
18
+ console.log("final token----", token);
19
+ const oauthToken = getOAuthToken(a)
20
+ console.log("final oauthToken----", oauthToken);
21
+ return { token: token, oauthToken: oauthToken }
20
22
  // No need to refresh token https://github.com/firebase/firebase-js-sdk/issues/4132
21
23
  })
22
- .then((token) => {
23
- return registerFCMTopic(token, firebaseConfig.serverKey, config);
24
+ .then(({token, oauthToken}) => {
25
+ return registerFCMTopic(config, firebaseConfig?.serverKey, token, oauthToken)
24
26
  })
25
- .catch((err) => {
26
- console.error(err);
27
- });
27
+ .catch(err => {
28
+ console.error(err)
29
+ })
30
+ }
31
+
32
+ async function getToken (app, m, vapidKey) {
33
+ const messaging = m.getMessaging(app)
34
+ return vapidKey
35
+ ? m.getToken(messaging, {
36
+ vapidKey: vapidKey
37
+ })
38
+ : m.getToken(messaging)
39
+ }
40
+
41
+ async function getOAuthToken (a) {
42
+ const auth = a.getAuth() // Ensure Firebase is initialized
43
+ const user = auth.currentUser
44
+ console.log("user-------", user);
45
+ if (!user) {
46
+ console.error('User not signed in')
47
+ return
48
+ }
49
+
50
+ // Get the ID token from Firebase Auth
51
+ const idToken = await user.getIdToken()
52
+ console.log("idtoken--------", idToken);
53
+ return idToken
28
54
  }
29
55
 
30
- async function registerFCMTopic(token, fcmServerKey, config) {
31
- if (!token) {
32
- console.error("No Token Found");
33
- return;
34
- }
56
+ async function registerFCMTopic (config, fcmServerKey, token, oauthToken) {
57
+ if (!token) {
58
+ console.error('No Token Found')
59
+ return
60
+ }
35
61
 
36
- const serverKey = typeof fcmServerKey === "function" ? await fcmServerKey(config) : fcmServerKey;
37
- console.log("serverKey-------", serverKey);
38
- const url = `https://iid.googleapis.com/iid/v1/${token}/rel/topics/all`;
39
- try {
40
- await fetch(url, {
41
- method: "POST",
42
- headers: {
43
- Authorization: `key=${serverKey}`,
44
- "content-type": "application/json",
45
- },
46
- });
47
- console.log("Registration Done Successfully");
48
- return;
49
- } catch (error) {
50
- console.error("FCM Subscription Failed", error);
51
- return;
52
- }
53
- };
62
+ const serverKey = typeof fcmServerKey === 'function' ? await fcmServerKey(config) : fcmServerKey
63
+ console.log('serverKey-------', serverKey)
64
+ const url = `https://iid.googleapis.com/iid/v1/${token}/rel/topics/all`
65
+ try {
66
+ await fetch(url, {
67
+ method: 'POST',
68
+ headers: {
69
+ Authorization: `Bearer ${oauthToken}`,
70
+ 'content-type': 'application/json'
71
+ }
72
+ })
73
+ console.log('Registration Done Successfully')
74
+ return
75
+ } catch (error) {
76
+ console.error('FCM Subscription Failed', error)
77
+ return
78
+ }
79
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quintype/framework",
3
- "version": "7.33.6-fcm-fix.0",
3
+ "version": "7.33.6-fcm-fix.1",
4
4
  "description": "Libraries to help build Quintype Node.js apps",
5
5
  "main": "index.js",
6
6
  "engines": {