- 👋 Hi, I’m @OkfreelancerAi -// Import necessary modules const express = require('express'); const bodyParser = require('body-parser'); const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken');
const app = express(); app.use(bodyParser.json());
// MongoDB connection mongoose.connect('mongodb://localhost:27017/adultContentApp', { useNewUrlParser: true, useUnifiedTopology: true, });
// User schema for authentication and affiliate tracking const userSchema = new mongoose.Schema({ username: { type: String, required: true }, password: { type: String, required: true }, ageVerified: { type: Boolean, default: false }, affiliateCode: { type: String, unique: true }, // Unique affiliate code referredBy: { type: String }, // Tracks who referred the user earnings: { type: Number, default: 0 }, // Earnings from referrals });
userSchema.pre('save', async function (next) { if (this.isModified('password')) { this.password = await bcrypt.hash(this.password, 10); } next(); });
const User = mongoose.model('User', userSchema);
// Content schema for managing videos const contentSchema = new mongoose.Schema({ title: { type: String, required: true }, category: { type: String, required: true }, url: { type: String, required: true }, // Video URL price: { type: Number, required: true }, // Pay-to-watch price });
const Content = mongoose.model('Content', contentSchema);
// Middleware for authentication const authenticate = (req, res, next) => { const token = req.headers.authorization; if (!token) return res.status(401).send('Access denied');
try { const decoded = jwt.verify(token, 'secretKey'); req.user = decoded; next(); } catch (err) { res.status(400).send('Invalid token'); } };
// Routes
// Register a new user app.post('/register', async (req, res) => { const { username, password, referredBy } = req.body;
try {
const affiliateCode = ${username}-${Date.now()}
; // Generate unique affiliate code
const user = new User({ username, password, affiliateCode, referredBy });
await user.save();
// Reward the referrer if applicable
if (referredBy) {
const referrer = await User.findOne({ affiliateCode: referredBy });
if (referrer) {
referrer.earnings += 10; // Example reward for referral
await referrer.save();
}
}
res.status(201).send({ message: 'User registered successfully', affiliateCode });
} catch (err) { res.status(400).send(err.message); } });
// Login a user app.post('/login', async (req, res) => { const { username, password } = req.body;
const user = await User.findOne({ username }); if (!user) return res.status(404).send('User not found');
const validPassword = await bcrypt.compare(password, user.password); if (!validPassword) return res.status(400).send('Invalid credentials');
const token = jwt.sign({ id: user._id }, 'secretKey', { expiresIn: '1h' }); res.send({ token }); });
// Add new content (Admin only) app.post('/content', authenticate, async (req, res) => { const { title, category, url, price } = req.body;
try { const content = new Content({ title, category, url, price }); await content.save(); res.status(201).send('Content added successfully'); } catch (err) { res.status(400).send(err.message); } });
// Get all content (Pay-to-watch model) app.get('/content', authenticate, async (req, res) => { try { const contentList = await Content.find(); res.send(contentList); } catch (err) { res.status(500).send(err.message); } });
// Affiliate dashboard to check earnings app.get('/affiliate-dashboard', authenticate, async (req, res) => { try { const user = await User.findById(req.user.id); if (!user) return res.status(404).send('User not found');
res.send({
username: user.username,
affiliateCode: user.affiliateCode,
earnings: user.earnings,
referredBy: user.referredBy || 'None',
});
} catch (err) { res.status(500).send(err.message); } });
// Start server app.listen(3000, () => console.log('Server running on port 3000')); // Import necessary modules const express = require('express'); const bodyParser = require('body-parser'); const mongoose = require('mongoose'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken');
const app = express(); app.use(bodyParser.json());
// MongoDB connection mongoose.connect('mongodb://localhost:27017/adultContentApp', { useNewUrlParser: true, useUnifiedTopology: true, });
// User schema for authentication and affiliate tracking const userSchema = new mongoose.Schema({ username: { type: String, required: true }, password: { type: String, required: true }, ageVerified: { type: Boolean, default: false }, affiliateCode: { type: String, unique: true }, // Unique affiliate code referredBy: { type: String }, // Tracks who referred the user earnings: { type: Number, default: 0 }, // Earnings from referrals });
userSchema.pre('save', async function (next) { if (this.isModified('password')) { this.password = await bcrypt.hash(this.password, 10); } next(); });
const User = mongoose.model('User', userSchema);
// Content schema for managing videos const contentSchema = new mongoose.Schema({ title: { type: String, required: true }, category: { type: String, required: true }, url: { type: String, required: true }, // Video URL price: { type: Number, required: true }, // Pay-to-watch price });
const Content = mongoose.model('Content', contentSchema);
// Middleware for authentication const authenticate = (req, res, next) => { const token = req.headers.authorization; if (!token) return res.status(401).send('Access denied');
try { const decoded = jwt.verify(token, 'secretKey'); req.user = decoded; next(); } catch (err) { res.status(400).send('Invalid token'); } };
// Routes
// Register a new user app.post('/register', async (req, res) => { const { username, password, referredBy } = req.body;
try {
const affiliateCode = ${username}-${Date.now()}
; // Generate unique affiliate code
const user = new User({ username, password, affiliateCode, referredBy });
await user.save();
// Reward the referrer if applicable
if (referredBy) {
const referrer = await User.findOne({ affiliateCode: referredBy });
if (referrer) {
referrer.earnings += 10; // Example reward for referral
await referrer.save();
}
}
res.status(201).send({ message: 'User registered successfully', affiliateCode });
} catch (err) { res.status(400).send(err.message); } });
// Login a user app.post('/login', async (req, res) => { const { username, password } = req.body;
const user = await User.findOne({ username }); if (!user) return res.status(404).send('User not found');
const validPassword = await bcrypt.compare(password, user.password); if (!validPassword) return res.status(400).send('Invalid credentials');
const token = jwt.sign({ id: user._id }, 'secretKey', { expiresIn: '1h' }); res.send({ token }); });
// Add new content (Admin only) app.post('/content', authenticate, async (req, res) => { const { title, category, url, price } = req.body;
try { const content = new Content({ title, category, url, price }); await content.save(); res.status(201).send('Content added successfully'); } catch (err) { res.status(400).send(err.message); } });
// Get all content (Pay-to-watch model) app.get('/content', authenticate, async (req, res) => { try { const contentList = await Content.find(); res.send(contentList); } catch (err) { res.status(500).send(err.message); } });
// Affiliate dashboard to check earnings app.get('/affiliate-dashboard', authenticate, async (req, res) => { try { const user = await User.findById(req.user.id); if (!user) return res.status(404).send('User not found');
res.send({
username: user.username,
affiliateCode: user.affiliateCode,
earnings: user.earnings,
referredBy: user.referredBy || 'None',
});
} catch (err) { res.status(500).send(err.message); } });
// Start server
app.listen(3000, () => console.log('Server running on port 3000'));
I’m interested in ...
- 🌱 I’m currently learning ...
- 💞️ I’m looking to collaborate on ...
- 📫 How to reach me ...
- 😄 Pronouns: ...
- ⚡ Fun fact: ...