Repository navigation
Expand file tree
/
Copy pathauth.js
More file actions
68 lines (59 loc) · 2.04 KB
/
Copy pathauth.js
File metadata and controls
68 lines (59 loc) · 2.04 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
const express = require('express');
const router = express.Router();
const User = require('../models/User');
const whatsappService = require('../services/whatsapp');
// Register user
router.post('/register', async (req, res) => {
try {
const { playerName, email, whatsapp, game, ffUid, ffIgn, bgmiUid, bgmiIgn, whatsappConsent } = req.body;
// Check if user already exists
let user = await User.findOne({ email });
if (user) {
return res.status(400).json({ message: 'User already exists' });
}
// Create new user
user = new User({
playerName,
email,
whatsapp,
game,
whatsappConsent
});
// Add game IDs based on preference
if (game === 'freefire' || game === 'both') {
user.freefire = { uid: ffUid, ign: ffIgn };
}
if (game === 'bgmi' || game === 'both') {
user.bgmi = { uid: bgmiUid, ign: bgmiIgn };
}
await user.save();
// Send WhatsApp confirmation
if (whatsappConsent) {
await whatsappService.sendWelcomeMessage(whatsapp, playerName);
}
res.status(201).json({
message: 'Registration successful',
user: {
id: user._id,
playerName: user.playerName,
email: user.email
}
});
} catch (error) {
console.error('Registration error:', error);
res.status(500).json({ message: 'Server error', error: error.message });
}
});
// Get user profile
router.get('/profile/:userId', async (req, res) => {
try {
const user = await User.findById(req.params.userId).select('-__v');
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
res.json(user);
} catch (error) {
res.status(500).json({ message: 'Server error', error: error.message });
}
});
module.exports = router;