forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferralRoutes.js
More file actions
33 lines (29 loc) 路 1.04 KB
/
Copy pathreferralRoutes.js
File metadata and controls
33 lines (29 loc) 路 1.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
const express = require('express');
const router = express.Router();
const referralController = require('../controllers/referralController');
const jwt = require('jsonwebtoken');
const auth = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) {
return res.status(401).json({ error: 'No token provided' });
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET || 'secret');
req.user = decoded;
next();
} catch (error) {
return res.status(401).json({ error: 'Invalid token' });
}
};
const admin = (req, res, next) => {
if (req.user.role !== 'admin') {
return res.status(403).json({ error: 'Admin access required' });
}
next();
};
router.post('/generate', auth, referralController.generateReferral);
router.get('/:code', referralController.getReferral);
router.post('/track', referralController.trackReferral);
router.post('/credit', auth, referralController.creditReferral);
router.get('/stats', auth, admin, referralController.getStats);
module.exports = router;