-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
141 lines (128 loc) · 5.73 KB
/
server.js
File metadata and controls
141 lines (128 loc) · 5.73 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const express = require('express');
const bodyParser = require('body-parser');
const { MongoClient } = require('mongodb');
const cors = require('cors');
const { router: dashboardRoutes, setCollections } = require('./routes/dashboardRoutes');
const { router: addMoneyRoutes } = require('./routes/addMoneyRoutes');
const { router: withdrawalRoutes, setCollections: setWithdrawalCollections } = require('./routes/withdrawalRoutes');
const app = express();
const port = 3000;
const corsOptions = {
origin: 'https://sethcolorprediction.netlify.app', // Replace with your frontend domain
methods: ['GET', 'POST', 'PUT', 'DELETE'], // Allow the necessary HTTP methods
credentials: true // If you need cookies or credentials with the request
};
// Middleware
app.use(bodyParser.json());
app.use(cors(corsOptions));
// MongoDB Connection
const uri = "mongodb+srv://rk1007:Rk%4010070711@cluster0.clpac.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"; // Replace with your MongoDB connection string
const client = new MongoClient(uri);
let usersCollection, resultsCollection, globalDataCollection, addMoneyRequestsCollection, withdrawalRequestsCollection,activeBetsCollection, upiIdCollection;
client.connect()
.then(() => {
console.log("Connected to MongoDB!");
const db = client.db('userDB');
usersCollection = db.collection('users');
resultsCollection = db.collection('results');
globalDataCollection = db.collection('globalData');
addMoneyRequestsCollection = db.collection('addMoneyRequests');
withdrawalRequestsCollection = db.collection('withdrawalRequests');
activeBetsCollection = db.collection('activeBets');
upiIdCollection = db.collection('upiIdColl');
setCollections(usersCollection, resultsCollection, globalDataCollection,activeBetsCollection);
const { setCollections: setAddMoneyCollections } = require('./routes/addMoneyRoutes');
setAddMoneyCollections(usersCollection, addMoneyRequestsCollection,upiIdCollection); // For addMoneyRoutes
setWithdrawalCollections(usersCollection, withdrawalRequestsCollection);
console.log("Collections set successfully!");
app.use('/api/dashboard', dashboardRoutes);
app.use('/api/add-money', addMoneyRoutes);
app.use('/api/withdrawal', withdrawalRoutes);
// Start the server only after MongoDB is connected
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
})
.catch(err => {
console.error("Failed to connect to MongoDB:", err);
});
// Routes
app.use('/api/dashboard', dashboardRoutes);
app.use('/api/add-money', addMoneyRoutes);
app.use('/api/withdrawal', withdrawalRoutes);
// Signup Route
app.post('/signup', async (req, res) => {
const { username, phone, password, favCar, favFood, bestFriend } = req.body;
try {
const existingUser = await usersCollection.findOne({ phone });
if (existingUser) {
return res.status(400).json({ message: 'User already exists.' });
}
const newUser = { username, phone, password, favCar, favFood, bestFriend,balance:0, playerHistory:[] };
await usersCollection.insertOne(newUser);
res.status(201).json({ message: 'User signed up successfully!' });
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Internal server error.' });
}
});
app.post('/check-phone', async (req, res) => {
const { phone } = req.body;
try {
const existingUser = await usersCollection.findOne({ phone });
if (existingUser) {
return res.status(200).json({ exists: true });
} else {
return res.status(200).json({ exists: false });
}
} catch (err) {
console.error('Error checking phone number:', err);
res.status(500).json({ message: 'Error checking phone number.' });
}
});
// Login Route
app.post('/login', async (req, res) => {
const { phone, password } = req.body;
try {
const user = await usersCollection.findOne({ phone, password });
if (user) {
res.status(200).json({ message: 'Login successful!', username: user.username, user });
} else {
res.status(401).json({ message: 'Invalid phone or password.' });
}
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Internal server error.' });
}
});
// Forgot Password Route
app.post('/forgot-password', async (req, res) => {
const { phoneOrUsername, favCar, favFood, bestFriend, newPassword } = req.body;
try {
// Verify security questions
const user = await usersCollection.findOne({ $or: [{ phone: phoneOrUsername }, { username: phoneOrUsername }], });
if (!user) {
return res.status(404).json({ message: 'User not found.' });
}
if (
user.favCar !== favCar ||
user.favFood !== favFood ||
user.bestFriend !== bestFriend
) {
return res.status(400).json({ message: 'Security answers do not match.' });
}
// Update password
const result = await usersCollection.updateOne(
{ _id: user._id },
{ $set: { password: newPassword } }
);
if (result.modifiedCount > 0) {
res.status(200).json({ message: 'Password updated successfully!' });
} else {
res.status(500).json({ message: 'Failed to update password.' });
}
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Internal server error.' });
}
});