-
Notifications
You must be signed in to change notification settings - Fork 255
Wusa's data sup #574
Copy link
Copy link
Open
Description
Full Stack Data Application Example
This issue proposes adding a comprehensive full stack data app example (Node.js + Express + MongoDB + JWT Auth + Wallet + External API) that demonstrates the following features:
Features
- User Registration & Login (JWT Auth)
- Secure Password Hashing
- Wallet System (Fund + Balance)
- API Integration (Data Purchase Simulation)
- Protected Routes
Backend (Express/MongoDB/Node)
-
User model (email, password, wallet)
-
JWT-based authentication with auth middleware
-
REST APIs
- Register (
POST /api/register) - Login (
POST /api/login) - Wallet Balance (
GET /api/wallet) - Fund Wallet (
POST /api/wallet/fund) - Data Purchase (
POST /api/buy-datawith external API integration)
- Register (
-
Example Environment Variables
MONGO_URIJWT_SECRET
Example server.js snippet:
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const axios = require('axios');
const cors = require('cors');
const app = express();
app.use(express.json());
app.use(cors());
// ... see full code in this issueUser Schema Example
const UserSchema = new mongoose.Schema({
email: String,
password: String,
wallet: { type: Number, default: 0 }
});
const User = mongoose.model('User', UserSchema);Middleware (JWT auth example)
const auth = (req, res, next) => {
const token = req.header('Authorization');
if (!token) return res.status(401).send('Access denied');
try {
const verified = jwt.verify(token, process.env.JWT_SECRET);
req.user = verified;
next();
} catch {
res.status(400).send('Invalid token');
}
};Key Routes
// Registration
app.post('/api/register', async (req, res) => {
const { email, password } = req.body;
const hashed = await bcrypt.hash(password, 10);
const user = new User({ email, password: hashed });
await user.save();
res.send('User registered');
});
// Login
app.post('/api/login', async (req, res) => {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user) return res.status(400).send('User not found');
const valid = await bcrypt.compare(password, user.password);
if (!valid) return res.status(400).send('Invalid password');
const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET);
res.json({ token });
});// ... (more route and code snippets in the proposal if needed)
Frontend (React Example)
- Simple login and wallet balance display
- Uses
axiosto interact with backend endpoints
Example App.js
import React, { useState } from 'react';
import axios from 'axios';
function App() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [token, setToken] = useState('');
const [balance, setBalance] = useState(0);
### ****
const login = async () => {
const res = await axios.post('http://localhost:5000/api/login', { email, password });
setToken(res.data.token);
};
const getWallet = async () => {
const res = await axios.get('http://localhost:5000/api/wallet', {
headers: { Authorization: token }
});
setBalance(res.data.balance);
};
return (
<div style={{ padding: 20 }}>
<h2>Login</h2>
<input placeholder="Email" onChange={e => setEmail(e.target.value)} />
<input type="password" placeholder="Password" onChange={e => setPassword(e.target.value)} />
<button onClick={login}>Login</button>
<h2>Wallet</h2>
<button onClick={getWallet}>Check Balance</button>
<p>Balance: {balance}</p>
</div>
);
}
export default App;Next Improvements (Future Enhancements)
- Add payment gateway (Paystack/Flutterwave)
- Transaction history
- Admin dashboard
- Email verification
This proposal can serve as a ready-to-use example, boilerplate, or template for users looking to scaffold a full stack, JWT-authenticated application with VS Code generators.
If accepted, I'd be willing to help contribute the full application structure and code example!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels