create-rex-app 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/index.js +148 -0
  2. package/package.json +31 -0
  3. package/template/README.md +71 -0
  4. package/template/_gitignore +25 -0
  5. package/template/api/routes.js +9 -0
  6. package/template/eslint.config.js +29 -0
  7. package/template/index.html +13 -0
  8. package/template/package-lock.json +4667 -0
  9. package/template/package.json +33 -0
  10. package/template/public/rex logo.png +0 -0
  11. package/template/public/rex.png +0 -0
  12. package/template/server.js +77 -0
  13. package/template/src/App.css +42 -0
  14. package/template/src/App.jsx +326 -0
  15. package/template/src/assets/react.svg +1 -0
  16. package/template/src/hooks/useTest.js +18 -0
  17. package/template/src/index.css +1 -0
  18. package/template/src/main.jsx +14 -0
  19. package/template/vercel.json +8 -0
  20. package/template/vite.config.js +7 -0
  21. package/template-mongo/README.md +96 -0
  22. package/template-mongo/api/db.js +17 -0
  23. package/template-mongo/api/middleware/authMiddleware.js +31 -0
  24. package/template-mongo/api/models/User.js +28 -0
  25. package/template-mongo/api/routes.js +99 -0
  26. package/template-mongo/env-sample.txt +3 -0
  27. package/template-mongo/eslint.config.js +29 -0
  28. package/template-mongo/index.html +13 -0
  29. package/template-mongo/package-lock.json +5076 -0
  30. package/template-mongo/package.json +38 -0
  31. package/template-mongo/public/rex logo.png +0 -0
  32. package/template-mongo/public/rex.png +0 -0
  33. package/template-mongo/public/vite.svg +1 -0
  34. package/template-mongo/server.js +80 -0
  35. package/template-mongo/src/App.css +42 -0
  36. package/template-mongo/src/App.jsx +42 -0
  37. package/template-mongo/src/assets/react.svg +1 -0
  38. package/template-mongo/src/components/Layout.jsx +85 -0
  39. package/template-mongo/src/components/PublicOnly.jsx +17 -0
  40. package/template-mongo/src/components/RequireAuth.jsx +20 -0
  41. package/template-mongo/src/context/AuthContext.jsx +35 -0
  42. package/template-mongo/src/hooks/useAuthContext.js +11 -0
  43. package/template-mongo/src/hooks/useLogin.js +42 -0
  44. package/template-mongo/src/hooks/useSignup.js +23 -0
  45. package/template-mongo/src/hooks/useTest.js +18 -0
  46. package/template-mongo/src/index.css +1 -0
  47. package/template-mongo/src/main.jsx +18 -0
  48. package/template-mongo/src/pages/Docs.jsx +131 -0
  49. package/template-mongo/src/pages/Home.jsx +93 -0
  50. package/template-mongo/src/pages/Login.jsx +97 -0
  51. package/template-mongo/src/pages/Signup.jsx +112 -0
  52. package/template-mongo/vercel.json +13 -0
  53. package/template-mongo/vite.config.js +7 -0
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "the-react-experiment",
3
+ "private": true,
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "nodemon --watch server.js --watch api server.js",
8
+ "build": "vite build",
9
+ "lint": "eslint .",
10
+ "preview": "vite preview"
11
+ },
12
+ "dependencies": {
13
+ "@tailwindcss/vite": "^4.2.0",
14
+ "@tanstack/react-query": "^5.90.21",
15
+ "axios": "^1.13.5",
16
+ "bcryptjs": "^3.0.3",
17
+ "dotenv": "^17.3.1",
18
+ "express": "^5.2.1",
19
+ "jsonwebtoken": "^9.0.3",
20
+ "mongoose": "^9.2.1",
21
+ "react": "^19.2.4",
22
+ "react-dom": "^19.2.4",
23
+ "react-router-dom": "^7.13.0",
24
+ "tailwindcss": "^4.2.0"
25
+ },
26
+ "devDependencies": {
27
+ "@eslint/js": "^10.0.1",
28
+ "@types/react": "^19.2.14",
29
+ "@types/react-dom": "^19.2.3",
30
+ "@vitejs/plugin-react": "^5.1.4",
31
+ "eslint": "^10.0.1",
32
+ "eslint-plugin-react-hooks": "^7.0.1",
33
+ "eslint-plugin-react-refresh": "^0.5.0",
34
+ "globals": "^17.3.0",
35
+ "nodemon": "^3.1.13",
36
+ "vite": "^7.3.1"
37
+ }
38
+ }
Binary file
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
@@ -0,0 +1,80 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import { fileURLToPath } from 'url'
4
+ import express from 'express'
5
+ import apiRoutes from './api/routes.js'
6
+ import dotenv from 'dotenv';
7
+ import connectDB from './api/db.js';
8
+
9
+ // Load env vars
10
+ dotenv.config();
11
+
12
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
13
+ const isProduction = process.env.NODE_ENV === 'production'
14
+ const PORT = process.env.PORT || 5173
15
+ const app = express()
16
+ async function createServer(app) {
17
+ // const app = express() -commented
18
+
19
+ // 1. Middleware
20
+ app.use(express.json());
21
+
22
+ // 2. Connect Database
23
+ connectDB();
24
+
25
+ // 3. API Routes (Always load these first)
26
+ app.use('/api', apiRoutes)
27
+
28
+ // ---------------------------------------------------------
29
+ // 🚀 PRODUCTION MODE (When deployed to Render/Vercel)
30
+ // ---------------------------------------------------------
31
+ if (isProduction) {
32
+ console.log('🚀 Running in PRODUCTION mode');
33
+
34
+ // Serve static files from the 'dist' folder (created by npm run build)
35
+ app.use(express.static(path.resolve(__dirname, 'dist')));
36
+
37
+ // SPA Fallback: Send index.html for any unknown route (so React Router works)
38
+ app.get(/.*/,(req, res) => {
39
+ res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
40
+ });
41
+ }
42
+
43
+ // ---------------------------------------------------------
44
+ // 🛠️ DEVELOPMENT MODE (Localhost)
45
+ // ---------------------------------------------------------
46
+ else {
47
+ console.log('🛠️ Running in DEVELOPMENT mode');
48
+
49
+ // We only import Vite in dev mode to save memory in prod
50
+ const { createServer: createViteServer } = await import('vite')
51
+
52
+ const vite = await createViteServer({
53
+ server: { middlewareMode: true },
54
+ appType: 'custom'
55
+ })
56
+
57
+ app.use(vite.middlewares)
58
+
59
+ app.use(async (req, res, next) => {
60
+ const url = req.originalUrl
61
+ try {
62
+ let template = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8')
63
+ template = await vite.transformIndexHtml(url, template)
64
+ res.status(200).set({ 'Content-Type': 'text/html' }).end(template)
65
+ } catch (e) {
66
+ vite.ssrFixStacktrace(e)
67
+ next(e)
68
+ }
69
+ })
70
+ }
71
+
72
+ // 4. Start Server
73
+ app.listen(PORT, () => {
74
+ console.log(`Server running at http://localhost:${PORT}`)
75
+ })
76
+ }
77
+
78
+ createServer(app);
79
+
80
+ export default app;
@@ -0,0 +1,42 @@
1
+ #root {
2
+ max-width: 1280px;
3
+ margin: 0 auto;
4
+ padding: 2rem;
5
+ text-align: center;
6
+ }
7
+
8
+ .logo {
9
+ height: 6em;
10
+ padding: 1.5em;
11
+ will-change: filter;
12
+ transition: filter 300ms;
13
+ }
14
+ .logo:hover {
15
+ filter: drop-shadow(0 0 2em #646cffaa);
16
+ }
17
+ .logo.react:hover {
18
+ filter: drop-shadow(0 0 2em #61dafbaa);
19
+ }
20
+
21
+ @keyframes logo-spin {
22
+ from {
23
+ transform: rotate(0deg);
24
+ }
25
+ to {
26
+ transform: rotate(360deg);
27
+ }
28
+ }
29
+
30
+ @media (prefers-reduced-motion: no-preference) {
31
+ a:nth-of-type(2) .logo {
32
+ animation: logo-spin infinite 20s linear;
33
+ }
34
+ }
35
+
36
+ .card {
37
+ padding: 2em;
38
+ }
39
+
40
+ .read-the-docs {
41
+ color: #888;
42
+ }
@@ -0,0 +1,42 @@
1
+ import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'
2
+ import Layout from './components/Layout'
3
+ import RequireAuth from './components/RequireAuth' // <--- Import the Guard
4
+ import PublicOnly from './components/PublicOnly'
5
+ // Pages
6
+ import Home from './pages/Home'
7
+ import Signup from './pages/Signup'
8
+ import Login from './pages/Login'
9
+ import Docs from './pages/Docs'
10
+
11
+ function App() {
12
+ return (
13
+ <Router>
14
+ <Routes>
15
+ <Route path="/" element={<Layout />}>
16
+
17
+ {/* Public Routes (Accessible by everyone) */}
18
+ <Route index element={<Home />} />
19
+ {/* 🔓 PUBLIC ONLY ROUTES (Guests) */}
20
+ {/* If you are logged in, you can't go here! */}
21
+ <Route element={<PublicOnly />}>
22
+ <Route path="signup" element={<Signup />} />
23
+ <Route path="login" element={<Login />} />
24
+ </Route>
25
+
26
+ {/* 🔒 PROTECTED ROUTES (Wrapped in Middleware) */}
27
+ <Route element={<RequireAuth />}>
28
+
29
+ {/* Any route put inside here is automatically protected! */}
30
+ <Route path="docs" element={<Docs />} />
31
+ {/* <Route path="dashboard" element={<Dashboard />} /> */}
32
+ {/* <Route path="settings" element={<Settings />} /> */}
33
+
34
+ </Route>
35
+
36
+ </Route>
37
+ </Routes>
38
+ </Router>
39
+ )
40
+ }
41
+
42
+ export default App
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
@@ -0,0 +1,85 @@
1
+ import { Outlet, Link } from 'react-router-dom'
2
+ import { useAuthContext } from '../hooks/useAuthContext'
3
+
4
+ const Layout = () => {
5
+ const { user, dispatch } = useAuthContext()
6
+
7
+ const handleLogout = () => {
8
+ // 1. Remove from Local Storage
9
+ localStorage.removeItem('user')
10
+ // 2. Clear Global State
11
+ dispatch({ type: 'LOGOUT' })
12
+ }
13
+
14
+ return (
15
+ <div className="min-h-screen bg-[#07070A] text-[#E2E8F0] font-sans flex flex-col selection:bg-purple-500/30">
16
+ {/* Sleek Glassmorphism Header */}
17
+ <header className="border-b border-white/5 bg-[#07070A]/80 backdrop-blur-xl sticky top-0 z-50">
18
+ <nav className="max-w-7xl mx-auto px-6 lg:px-8 h-16 flex justify-between items-center">
19
+
20
+ {/* Rex Logo */}
21
+ <Link to="/" className="flex items-center gap-3 group">
22
+ <div className="h-8 w-8 rounded-lg flex items-center justify-center overflow-hidden shadow-lg group-hover:shadow-purple-500/40 transition-all">
23
+ <img src="/rex.png" alt="Rex" className="w-full h-full object-cover" />
24
+ </div>
25
+ <span className="font-bold text-xl text-white tracking-tight">rex</span>
26
+ </Link>
27
+
28
+ <div className="flex gap-6 font-medium items-center text-sm">
29
+ <Link to="/" className="text-slate-400 hover:text-white transition-colors">Home</Link>
30
+
31
+ {/* CONDITIONAL RENDERING */}
32
+ {user ? (
33
+ // If Logged In:
34
+ <div className="flex items-center gap-4">
35
+
36
+ <div className="flex items-center gap-2">
37
+ {/* 1. NAME (with Email Tooltip) */}
38
+ <span
39
+ title={user.email}
40
+ className="text-xs font-semibold text-slate-300 border border-white/10 px-3 py-1.5 rounded-lg bg-white/5 cursor-help transition-colors hover:bg-white/10 hover:text-white"
41
+ >
42
+ {user.name}
43
+ </span>
44
+
45
+ {/* 2. ROLE BADGE */}
46
+ <span className="text-[10px] font-bold uppercase tracking-widest text-purple-400 bg-purple-500/10 border border-purple-500/20 px-2 py-1 rounded-md">
47
+ {user.role}
48
+ </span>
49
+ </div>
50
+
51
+ {/* 3. LOGOUT BUTTON */}
52
+ <button
53
+ onClick={handleLogout}
54
+ className="bg-red-500/10 hover:bg-red-500/20 border border-red-500/20 text-red-400 hover:text-red-300 px-4 py-1.5 rounded-lg text-sm font-semibold transition-all"
55
+ >
56
+ Logout
57
+ </button>
58
+ </div>
59
+ ) : (
60
+ // If Logged Out:
61
+ <div className="flex items-center gap-5">
62
+ <Link to="/login" className="text-slate-400 hover:text-white transition-colors">
63
+ Login
64
+ </Link>
65
+ <Link to="/signup" className="bg-gradient-to-r from-purple-600 to-orange-500 hover:from-purple-500 hover:to-orange-400 text-white px-5 py-1.5 rounded-lg shadow-[0_0_15px_rgba(124,58,237,0.3)] hover:shadow-[0_0_25px_rgba(124,58,237,0.5)] transition-all font-semibold">
66
+ Sign Up
67
+ </Link>
68
+ </div>
69
+ )}
70
+ </div>
71
+ </nav>
72
+ </header>
73
+
74
+ {/* Main Content Area */}
75
+ <main className="flex-grow max-w-7xl w-full mx-auto px-6 lg:px-8 py-10 relative">
76
+ {/* Optional subtle glow behind the content */}
77
+ <div className="absolute top-0 left-1/2 -translate-x-1/2 w-[800px] h-[400px] bg-purple-600/5 blur-[120px] pointer-events-none rounded-full -z-10"></div>
78
+
79
+ <Outlet />
80
+ </main>
81
+ </div>
82
+ )
83
+ }
84
+
85
+ export default Layout
@@ -0,0 +1,17 @@
1
+ import { useAuthContext } from '../hooks/useAuthContext'
2
+ import { Navigate, Outlet } from 'react-router-dom'
3
+
4
+ const PublicOnly = () => {
5
+ const { user } = useAuthContext()
6
+
7
+ // 1. Check: Is the user logged in?
8
+ if (user) {
9
+ // 2. If YES: Kick them to Home (they don't need to login again!)
10
+ return <Navigate to="/" replace />
11
+ }
12
+
13
+ // 3. If NO: Allow access to the page (Login/Signup)
14
+ return <Outlet />
15
+ }
16
+
17
+ export default PublicOnly
@@ -0,0 +1,20 @@
1
+ import { useAuthContext } from '../hooks/useAuthContext'
2
+ import { Navigate, useLocation, Outlet } from 'react-router-dom'
3
+
4
+ const RequireAuth = () => {
5
+ const { user } = useAuthContext()
6
+ const location = useLocation()
7
+
8
+ // 1. Check: Is the user logged in?
9
+ if (!user) {
10
+ // 2. If NO: Redirect to Login
11
+ // (replace: true keeps the history clean so back button works better)
12
+ // (state: saves where they were trying to go, so we can send them back after login if we want)
13
+ return <Navigate to="/login" state={{ from: location }} replace />
14
+ }
15
+
16
+ // 3. If YES: Render the child routes (The protected page)
17
+ return <Outlet />
18
+ }
19
+
20
+ export default RequireAuth
@@ -0,0 +1,35 @@
1
+ import { createContext, useReducer, useEffect } from 'react'
2
+
3
+ export const AuthContext = createContext()
4
+
5
+ export const authReducer = (state, action) => {
6
+ switch (action.type) {
7
+ case 'LOGIN':
8
+ return { user: action.payload, authIsReady: true }
9
+ case 'LOGOUT':
10
+ return { user: null, authIsReady: true }
11
+ case 'AUTH_READY': // <--- New action
12
+ return { user: action.payload, authIsReady: true }
13
+ default:
14
+ return state
15
+ }
16
+ }
17
+
18
+ export const AuthContextProvider = ({ children }) => {
19
+ const [state, dispatch] = useReducer(authReducer, {
20
+ user: null,
21
+ authIsReady: false // <--- Start as FALSE
22
+ })
23
+
24
+ useEffect(() => {
25
+ const user = JSON.parse(localStorage.getItem('user'))
26
+ // Even if user is null, we are "Ready" because the check is done
27
+ dispatch({ type: 'AUTH_READY', payload: user })
28
+ }, [])
29
+
30
+ return (
31
+ <AuthContext.Provider value={{ ...state, dispatch }}>
32
+ { children }
33
+ </AuthContext.Provider>
34
+ )
35
+ }
@@ -0,0 +1,11 @@
1
+ // src/hooks/useAuthContext.js
2
+ import { AuthContext } from '../context/AuthContext'
3
+ import { useContext } from 'react'
4
+
5
+ export const useAuthContext = () => {
6
+ const context = useContext(AuthContext)
7
+ if (!context) {
8
+ throw Error('useAuthContext must be used inside an AuthContextProvider')
9
+ }
10
+ return context
11
+ }
@@ -0,0 +1,42 @@
1
+ import { useMutation } from '@tanstack/react-query'
2
+ import axios from 'axios'
3
+ import { useNavigate } from 'react-router-dom'
4
+ import { useAuthContext } from './useAuthContext'
5
+
6
+ const loginAndFetchUser = async (credentials) => {
7
+ // 1. Login to get the token
8
+ const loginRes = await axios.post('/api/login', credentials)
9
+ const token = loginRes.data.token
10
+
11
+ // 2. Use the token to fetch user details immediately
12
+ const profileRes = await axios.get('/api/profile', {
13
+ headers: { Authorization: `Bearer ${token}` }
14
+ })
15
+
16
+ // 3. Return combined data (Token + User Info)
17
+ return {
18
+ token,
19
+ ...profileRes.data
20
+ }
21
+ }
22
+
23
+ export const useLogin = () => {
24
+ const navigate = useNavigate()
25
+ const { dispatch } = useAuthContext()
26
+
27
+ return useMutation({
28
+ mutationFn: loginAndFetchUser, // <--- New function
29
+ onSuccess: (data) => {
30
+ // 'data' now contains { token, name, email, role, ... }
31
+
32
+ localStorage.setItem('user', JSON.stringify(data))
33
+ dispatch({ type: 'LOGIN', payload: data })
34
+
35
+ navigate('/')
36
+ },
37
+ onError: (error) => {
38
+ const message = error.response?.data?.message || 'Login failed'
39
+ alert(`❌ Error: ${message}`)
40
+ }
41
+ })
42
+ }
@@ -0,0 +1,23 @@
1
+ import { useMutation } from '@tanstack/react-query'
2
+ import axios from 'axios'
3
+
4
+ const signupUser = async (userData) => {
5
+ // userData is { name, email, password }
6
+ const response = await axios.post('/api/users', userData)
7
+ return response.data
8
+ }
9
+
10
+ export const useSignup = () => {
11
+ return useMutation({
12
+ mutationFn: signupUser,
13
+ onSuccess: (data) => {
14
+
15
+ alert(`Welcome, ${data.name}! Account created.`)
16
+ },
17
+ onError: (error) => {
18
+ // Extract the error message from the backend response
19
+ const message = error.response?.data?.message || 'Signup failed'
20
+ alert(`Error: ${message}`)
21
+ }
22
+ })
23
+ }
@@ -0,0 +1,18 @@
1
+ import axios from 'axios'
2
+ import { useQuery } from '@tanstack/react-query'
3
+
4
+ // The Fetcher (Private to this file mostly, or exported if needed elsewhere)
5
+ const fetchServerMessage = async () => {
6
+ const response = await axios.get('/api/test')
7
+ return response.data
8
+ }
9
+
10
+ // The Custom Hook
11
+ export const useTest = () => {
12
+ return useQuery({
13
+ queryKey: ['serverMessage'],
14
+ queryFn: fetchServerMessage,
15
+ // You can add default options here if you want (e.g., staleTime)
16
+ staleTime: 1000 * 60 * 5, // Data is fresh for 5 minutes
17
+ })
18
+ }
@@ -0,0 +1 @@
1
+ @import "tailwindcss";
@@ -0,0 +1,18 @@
1
+ import { StrictMode } from 'react'
2
+ import { createRoot } from 'react-dom/client'
3
+ import './index.css'
4
+ import App from './App.jsx'
5
+ import { AuthContextProvider } from './context/AuthContext'
6
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
7
+
8
+ const queryClient = new QueryClient()
9
+ createRoot(document.getElementById('root')).render(
10
+ <StrictMode>
11
+ <AuthContextProvider>
12
+ <QueryClientProvider client={queryClient}>
13
+ <App />
14
+ </QueryClientProvider>
15
+ </AuthContextProvider>
16
+
17
+ </StrictMode>,
18
+ )
@@ -0,0 +1,131 @@
1
+ const Docs = () => {
2
+ return (
3
+ <div className="max-w-4xl mx-auto py-8 z-10 relative">
4
+
5
+ {/* BACKGROUND GLOW */}
6
+ <div className="absolute top-0 left-1/2 -translate-x-1/2 w-[600px] h-[300px] bg-blue-500/10 blur-[100px] pointer-events-none -z-10"></div>
7
+
8
+ <div className="bg-white/[0.02] rounded-3xl shadow-2xl border border-white/10 backdrop-blur-md overflow-hidden">
9
+
10
+ {/* HEADER */}
11
+ <div className="p-8 sm:p-10 border-b border-white/10 bg-black/20">
12
+ <div className="inline-flex items-center gap-2 bg-blue-500/10 border border-blue-500/20 text-blue-400 px-3 py-1 rounded-full text-xs font-mono font-bold uppercase tracking-wider mb-4">
13
+ <span className="w-1.5 h-1.5 rounded-full bg-blue-400 animate-pulse"></span>
14
+ MongoDB Template
15
+ </div>
16
+ <h1 className="text-4xl sm:text-5xl font-extrabold text-white tracking-tight mb-4">
17
+ Boilerplate <span className="bg-gradient-to-r from-blue-400 to-emerald-400 bg-clip-text text-transparent">Overview</span>
18
+ </h1>
19
+ <p className="text-slate-400 text-lg max-w-2xl">
20
+ You've successfully scaffolded the Rex MongoDB template. Here is a brief guide to the architecture and auth flow of this specific stack.
21
+ </p>
22
+ </div>
23
+
24
+ {/* CONTENT */}
25
+ <div className="p-8 sm:p-10 space-y-12 text-slate-300">
26
+
27
+ {/* Section 1: The Stack */}
28
+ <section>
29
+ <h2 className="text-2xl font-bold text-white mb-4 flex items-center gap-3">
30
+ <span className="text-blue-400">01.</span> The Stack
31
+ </h2>
32
+ <div className="grid sm:grid-cols-2 gap-4">
33
+ <div className="bg-black/30 p-4 rounded-xl border border-white/5">
34
+ <div className="text-xs text-slate-500 font-mono mb-1">Frontend</div>
35
+ <div className="font-semibold text-white">React + Vite + Tailwind</div>
36
+ </div>
37
+ <div className="bg-black/30 p-4 rounded-xl border border-white/5">
38
+ <div className="text-xs text-slate-500 font-mono mb-1">Backend</div>
39
+ <div className="font-semibold text-white">Node.js + Express</div>
40
+ </div>
41
+ <div className="bg-black/30 p-4 rounded-xl border border-white/5">
42
+ <div className="text-xs text-slate-500 font-mono mb-1">Database</div>
43
+ <div className="font-semibold text-white">MongoDB (Mongoose)</div>
44
+ </div>
45
+ <div className="bg-black/30 p-4 rounded-xl border border-white/5">
46
+ <div className="text-xs text-slate-500 font-mono mb-1">Security</div>
47
+ <div className="font-semibold text-white">JWT Authentication</div>
48
+ </div>
49
+ </div>
50
+ </section>
51
+
52
+ {/* Section 2: Auth Flow */}
53
+ <section>
54
+ <h2 className="text-2xl font-bold text-white mb-4 flex items-center gap-3">
55
+ <span className="text-emerald-400">02.</span> Auth Flow & Guard
56
+ </h2>
57
+ <p className="mb-4 text-slate-400">
58
+ Authentication is handled via JWT. Instead of checking for a user on every page, we use a Wrapper Component called <code className="text-emerald-300 bg-emerald-400/10 px-1.5 py-0.5 rounded font-mono text-sm">RequireAuth</code>.
59
+ </p>
60
+
61
+ <div className="bg-black/50 p-6 rounded-2xl border border-white/5 font-mono text-sm text-slate-400 overflow-x-auto leading-loose">
62
+ <span className="text-blue-400">// How the Bouncer works conceptually:</span><br/><br/>
63
+ [ App Router ]<br/>
64
+ &nbsp;&nbsp;│<br/>
65
+ &nbsp;&nbsp;├── Public Routes (Home, Login)<br/>
66
+ &nbsp;&nbsp;│<br/>
67
+ &nbsp;&nbsp;└── <span className="text-emerald-400">&lt;RequireAuth /&gt;</span> (The Guard 🛡️)<br/>
68
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│<br/>
69
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── Checks Context: <span className="text-white">"Do we have a user?"</span><br/>
70
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│<br/>
71
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── <span className="text-red-400">NO?</span> → Redirects to /login<br/>
72
+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── <span className="text-green-400">YES?</span> → Renders the Child Page (Outlet)
73
+ </div>
74
+ </section>
75
+
76
+ {/* Section 3: File Structure */}
77
+ <section>
78
+ <h2 className="text-2xl font-bold text-white mb-4 flex items-center gap-3">
79
+ <span className="text-yellow-400">03.</span> Project Structure
80
+ </h2>
81
+ <p className="mb-4 text-slate-400">
82
+ This boilerplate is modular. To add a new feature: create a model in <code className="text-yellow-300">api/models</code>, a route in <code className="text-yellow-300">api/routes.js</code>, and a hook in <code className="text-yellow-300">src/hooks</code>.
83
+ </p>
84
+ <pre className="bg-black/50 p-6 rounded-2xl border border-white/5 text-xs sm:text-sm text-slate-300 overflow-x-auto leading-relaxed font-mono">
85
+ {`root/
86
+ ├── .env `} <span className="text-slate-500">// Secrets (DB URI, JWT Secret)</span>{`
87
+ ├── server.js `} <span className="text-slate-500">// Entry point (Express App)</span>{`
88
+ ├── api/ `} <span className="text-slate-500">// Backend Logic</span>{`
89
+ │ ├── db.js
90
+ │ ├── routes.js
91
+ │ ├── models/ `} <span className="text-slate-500">// Mongoose Schemas (User.js)</span>{`
92
+ │ └── middleware/ `} <span className="text-slate-500">// Auth Guard (auth.js)</span>{`
93
+
94
+ └── src/ `} <span className="text-slate-500">// Frontend (React)</span>{`
95
+ ├── main.jsx
96
+ ├── App.jsx `} <span className="text-slate-500">// Router Definitions</span>{`
97
+ ├── components/
98
+ │ ├── Layout.jsx
99
+ │ └── RequireAuth.jsx
100
+ ├── context/ `} <span className="text-slate-500">// Global User State</span>{`
101
+ ├── hooks/ `} <span className="text-slate-500">// React Query Hooks</span>{`
102
+ └── pages/ `}
103
+ </pre>
104
+ </section>
105
+ </div>
106
+
107
+ {/* FOOTER CTA */}
108
+ <div className="p-8 sm:p-10 border-t border-white/10 bg-black/40 text-center">
109
+ <h3 className="text-xl font-bold text-white mb-2">Need to know how Rex handles the server?</h3>
110
+ <p className="text-slate-400 mb-6 text-sm">
111
+ For deep dives into the unified architecture, Vite middleware, and deployment strategies, read the official framework documentation.
112
+ </p>
113
+ <a
114
+ href="https://rex-docs.vercel.app/"
115
+ target="_blank"
116
+ rel="noopener noreferrer"
117
+ className="inline-flex items-center justify-center gap-2 bg-white/5 hover:bg-white/10 border border-white/10 hover:border-purple-500/50 text-white px-6 py-2.5 rounded-lg font-semibold transition-all"
118
+ >
119
+ Read Official Rex Docs
120
+ <svg width="16" height="16" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth="2">
121
+ <path strokeLinecap="round" strokeLinejoin="round" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
122
+ </svg>
123
+ </a>
124
+ </div>
125
+
126
+ </div>
127
+ </div>
128
+ )
129
+ }
130
+
131
+ export default Docs