forked from RickDeb2004/Business-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
91 lines (76 loc) · 2.32 KB
/
server.js
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
const express = require('express');
const admin = require('firebase-admin');
const bodyParser = require('body-parser');
const cors = require('cors');
const socketIo = require('socket.io');
// Initialize Firebase Admin SDK
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://buisness-bf4ca-default-rtdb.firebaseio.com"
});
const app = express();
const server = require('http').createServer(app);
const io = socketIo(server, {
cors: {
origin: '*',
}
});
app.use(cors());
app.use(bodyParser.json());
const PORT = process.env.PORT || 3000;
// Route to create a new user
app.post('/create-user', async (req, res) => {
const { email, password, displayName, role, uid } = req.body;
try {
const userRecord = await admin.auth().createUser({
uid: uid,
email: email,
password: password,
displayName: displayName,
});
// Extract adminId from uid (assuming format is adminId_uuid)
const adminId = uid.split('_')[0];
// Add user data to the database under the specified reference
const userRef = admin.database().ref(`userList/${adminId}/${userRecord.uid}`);
await userRef.set({
name: displayName,
email,
role,
password,
signInTime: "yet to login",
blocked: false,
});
res.status(201).send({ uid: userRecord.uid });
} catch (error) {
res.status(500).send(error.message);
}
});
app.get('/', (req, res) => {
res.send('Hello World');
});
// Handle socket connections
io.on('connection', (socket) => {
console.log('New client connected');
socket.on('joinRoom', ({ adminId }) => {
socket.join(adminId);
});
socket.on('sendMessage', ({ adminId, message }) => {
const messageRef = admin.database().ref(`chats/${adminId}`).push();
messageRef.set({
message,
timestamp: Date.now(),
sentByMe: true,
});
io.to(adminId).emit('receiveMessage', { message, timestamp: Date.now(), sentByMe: true });
});
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
// app.listen(PORT, () => {
// console.log(`Server is running on port ${PORT}`);
// });
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});