File size: 3,586 Bytes
f62cc5e
 
 
 
 
 
 
b922d49
 
 
 
c12647e
 
 
 
 
 
 
b922d49
c12647e
 
b922d49
c12647e
 
 
 
f62cc5e
 
 
 
 
b922d49
f62cc5e
 
 
 
 
c12647e
f62cc5e
 
c12647e
f62cc5e
 
 
 
 
 
 
 
 
 
 
 
c12647e
f62cc5e
 
 
 
 
b922d49
 
 
 
 
 
 
 
 
 
 
 
 
f62cc5e
 
 
 
 
b922d49
f62cc5e
 
 
b922d49
 
f62cc5e
 
 
 
 
 
 
 
 
 
 
 
 
 
b922d49
f62cc5e
 
 
b922d49
 
f62cc5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c12647e
b922d49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const cors = require('cors');
const admin = require('firebase-admin');

const app = express();
const PORT = process.env.PORT || 7860;

const allowedOrigins = [
  'https://raybil.me', 
  'https://story.raybil.me'
];

const corsOptions = {
  origin: function (origin, callback) {
    if (!origin) return callback(null, true);
    if (allowedOrigins.indexOf(origin) !== -1) {
      callback(null, true);
    } else {
      callback(null, false);
    }
  },
  methods: ['POST', 'GET', 'OPTIONS'],
  credentials: true
};

app.use(cors(corsOptions));
app.use(express.json());

try {
  const secretKeyString = process.env.FIREBASE_SERVICE_ACCOUNT;
  if (!secretKeyString) {
    throw new Error("FIREBASE_SERVICE_ACCOUNT secret is missing!");
  }
  const serviceAccount = JSON.parse(secretKeyString);
  admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
  });
  console.log("Firebase Admin SDK initialized successfully!");
} catch (error) {
  console.error("Firebase Initialization Failed:", error.message);
  process.exit(1);
}

const healthCheckHandler = (req, res) => {
  res.status(200).send('ok');
};
app.all('/health', healthCheckHandler);
app.all('/check', healthCheckHandler);

const enforcePostOnly = (req, res, next) => {
  if (req.method !== 'POST') {
    return res.status(405).json({ 
      success: false, 
      message: `Method ${req.method} Not Allowed. Only POST requests are accepted.` 
    });
  }
  next();
};

function getSafeTopicName(category) {
  const mapping = {
    "নবীদের কাহিনী": "nobider_kahini",
    "সাহাবিদের জীবনী": "sahabider_jiboni",
    "আল-কুরআন ও হাদিসের গল্প": "quran_hadis",
    "পূর্বসূরি ও বুজুর্গদের কাহিনী": "scholars_stories",
    "শিশুদের ইসলামিক গল্প": "kids_stories"
  };
  if (mapping[category]) {
    return mapping[category];
  }
  return "general_stories";
}

app.post('/subscribe', enforcePostOnly, async (req, res) => {
  const { fcm_token, selected_category, action } = req.body;

  if (!fcm_token || !selected_category || action !== 'subscribe') {
    return res.status(400).json({ success: false, message: "Invalid payload or wrong action." });
  }

  try {
    const safeTopic = getSafeTopicName(selected_category);
    await admin.messaging().subscribeToTopic(fcm_token, safeTopic);
    
    res.status(200).json({ 
      success: true, 
      message: `Successfully registered to ${selected_category} category.` 
    });
  } catch (error) {
    res.status(500).json({ success: false, error: error.message });
  }
});

app.post('/unsubscribe', enforcePostOnly, async (req, res) => {
  const { fcm_token, selected_category, action } = req.body;

  if (!fcm_token || !selected_category || action !== 'unsubscribe') {
    return res.status(400).json({ success: false, message: "Invalid payload or wrong action." });
  }

  try {
    const safeTopic = getSafeTopicName(selected_category);
    await admin.messaging().unsubscribeFromTopic(fcm_token, safeTopic);

    res.status(200).json({ 
      success: true, 
      message: `Successfully removed from ${selected_category} category.` 
    });
  } catch (error) {
    res.status(500).json({ success: false, error: error.message });
  }
});

app.use((req, res) => {
  res.status(404).json({ success: false, message: "Endpoint not found." });
});

app.listen(PORT, '0.0.0.0', () => {
  console.log(`Server is running securely on port ${PORT}`);
});