Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c3a322fe2a | |||
| bac1063de5 | |||
| 2dcafd200d | |||
| a18ea7fe67 | |||
| b6e8d7ad7a | |||
| 1c4d87622c | |||
| 2d9f29e694 | |||
| 4b6eb2271c | |||
| 56fc50d3b9 | |||
| d29e3397a6 | |||
| 335c7008aa | |||
| e89b0645ca | |||
| 36e79a513f | |||
| 6ef0affcf6 | |||
| cb0c1284bf | |||
| 5cf4b66894 | |||
| 45cf281c91 | |||
| 2794b4c724 | |||
| b1f49a4166 | |||
| 4cbf67dcbb | |||
| fd29385cf8 |
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2021 Kayne Ruse, KR Game Studios
|
||||
Copyright (c) 2021-2023 Kayne Ruse, KR Game Studios
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ To set up this template in development mode:
|
||||
- Account deletion
|
||||
- Password management
|
||||
- JSON web token authentication
|
||||
- HttpOnly cookies for security
|
||||
- Optional post validation hook
|
||||
- Fully Featured News Blog (as a microservice)
|
||||
- Publish, edit or delete articles as needed
|
||||
|
||||
+9
-7
@@ -2,14 +2,16 @@
|
||||
import 'regenerator-runtime/runtime';
|
||||
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
|
||||
import App from './pages/app';
|
||||
import TokenProvider from './pages/utilities/token-provider';
|
||||
|
||||
ReactDOM.render(
|
||||
<TokenProvider>
|
||||
<App />
|
||||
</TokenProvider>,
|
||||
document.querySelector('#root')
|
||||
);
|
||||
ReactDOM
|
||||
.createRoot(document.getElementById('root'))
|
||||
.render(
|
||||
<TokenProvider>
|
||||
<App />
|
||||
</TokenProvider>
|
||||
)
|
||||
;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useEffect, useContext, useRef } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, Navigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from '../utilities/apply-to-body';
|
||||
|
||||
@@ -13,7 +13,7 @@ const Account = props => {
|
||||
|
||||
//misplaced?
|
||||
if (!authTokens.accessToken) {
|
||||
return <Redirect to='/' />;
|
||||
return <Navigate to='/' />;
|
||||
}
|
||||
|
||||
//refs
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useContext, useRef } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, Navigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from '../utilities/apply-to-body';
|
||||
|
||||
@@ -13,7 +13,7 @@ const Login = props => {
|
||||
|
||||
//misplaced?
|
||||
if (authTokens.accessToken) {
|
||||
return <Redirect to='/' />;
|
||||
return <Navigate to='/' />;
|
||||
}
|
||||
|
||||
//refs
|
||||
@@ -40,7 +40,7 @@ const Login = props => {
|
||||
if (accessToken) {
|
||||
authTokens.setAccessToken(accessToken);
|
||||
|
||||
props.history.push('/');
|
||||
return <Navigate to='/' />;
|
||||
}
|
||||
}
|
||||
}>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useContext, useRef } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from '../utilities/apply-to-body';
|
||||
|
||||
@@ -9,12 +9,15 @@ import { TokenContext } from '../utilities/token-provider';
|
||||
const validateEmail = require('../../../common/utilities/validate-email');
|
||||
|
||||
const Recover = props => {
|
||||
//history
|
||||
const navigate = useNavigate();
|
||||
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//misplaced?
|
||||
if (authTokens.accessToken) {
|
||||
return <Redirect to='/' />;
|
||||
navigate("/");
|
||||
}
|
||||
|
||||
//refs
|
||||
@@ -39,7 +42,7 @@ const Recover = props => {
|
||||
|
||||
//redirect
|
||||
if (redirect) {
|
||||
props.history.push('/');
|
||||
navigate("/");
|
||||
}
|
||||
}
|
||||
}>
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
import React, { useContext, useRef } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import queryString from 'query-string';
|
||||
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from '../utilities/apply-to-body';
|
||||
|
||||
import { TokenContext } from '../utilities/token-provider';
|
||||
|
||||
const Reset = props => {
|
||||
//params
|
||||
const [params, setParams] = useSearchParams(); //the URLSearchParams API
|
||||
|
||||
//history
|
||||
const navigate = useNavigate();
|
||||
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//query
|
||||
const query = queryString.parse(props.location.search);
|
||||
|
||||
//misplaced?
|
||||
if (authTokens.accessToken || !query.email || !query.token) {
|
||||
return <Redirect to='/' />;
|
||||
if (authTokens.accessToken || !params.has('email') || !params.has('token')) {
|
||||
navigate("/");
|
||||
}
|
||||
|
||||
//refs
|
||||
@@ -31,7 +33,7 @@ const Reset = props => {
|
||||
<h1 className='text centered'>Reset Password</h1>
|
||||
<form className='constrained' onSubmit={async evt => {
|
||||
evt.preventDefault();
|
||||
const [err, redirect] = await update(passwordRef.current.value, retypeRef.current.value, query);
|
||||
const [err, redirect] = await update(passwordRef.current.value, retypeRef.current.value, params);
|
||||
|
||||
if (err) {
|
||||
alert(err);
|
||||
@@ -42,7 +44,7 @@ const Reset = props => {
|
||||
|
||||
//redirect
|
||||
if (redirect) {
|
||||
props.history.push('/');
|
||||
navigate("/");
|
||||
}
|
||||
}}>
|
||||
<input type='password' name='password' placeholder='New Password' ref={passwordRef} />
|
||||
@@ -56,7 +58,7 @@ const Reset = props => {
|
||||
);
|
||||
};
|
||||
|
||||
const update = async (password, retype, query) => {
|
||||
const update = async (password, retype, params) => {
|
||||
if (password != retype) {
|
||||
return ['Passwords do not match'];
|
||||
}
|
||||
@@ -65,7 +67,7 @@ const update = async (password, retype, query) => {
|
||||
return ['Password is too short'];
|
||||
}
|
||||
|
||||
const result = await fetch(`${process.env.AUTH_URI}/auth/reset?email=${query.email}&token=${query.token}`, {
|
||||
const result = await fetch(`${process.env.AUTH_URI}/auth/reset?email=${params.get('email')}&token=${params.get('token')}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useContext, useRef } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from '../utilities/apply-to-body';
|
||||
|
||||
@@ -10,12 +10,15 @@ const validateEmail = require('../../../common/utilities/validate-email');
|
||||
const validateUsername = require('../../../common/utilities/validate-username');
|
||||
|
||||
const Signup = props => {
|
||||
//history
|
||||
const navigate = useNavigate();
|
||||
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//misplaced?
|
||||
if (authTokens.accessToken) {
|
||||
return <Redirect to='/' />;
|
||||
navigate("/");
|
||||
}
|
||||
|
||||
//refs
|
||||
@@ -44,7 +47,7 @@ const Signup = props => {
|
||||
|
||||
//redirect
|
||||
if (redirect) {
|
||||
props.history.push('/');
|
||||
navigate("/");
|
||||
}
|
||||
}
|
||||
}>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useContext } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, Navigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from '../utilities/apply-to-body';
|
||||
|
||||
@@ -14,7 +14,7 @@ const Admin = props => {
|
||||
|
||||
//misplaced? (admin only)
|
||||
if (!authTokens.accessToken || !authTokens.getPayload().admin) {
|
||||
return <Redirect to='/' />;
|
||||
return <Navigate to='/' />;
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useContext } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, Navigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from '../utilities/apply-to-body';
|
||||
|
||||
@@ -17,7 +17,7 @@ const Mod = props => {
|
||||
|
||||
//misplaced? (admin only)
|
||||
if (!authTokens.accessToken || !(authTokens.getPayload().admin || authTokens.getPayload().mod)) {
|
||||
return <Redirect to='/' />;
|
||||
return <Navigate to='/' />;
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -7,16 +7,13 @@ const ChatReports = props => {
|
||||
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
useEffect(async () => {
|
||||
const result = await authTokens.tokenFetch(`${process.env.CHAT_URI}/admin/reports`);
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
alert(err);
|
||||
} else {
|
||||
setReports(await result.json());
|
||||
}
|
||||
useEffect(() => {
|
||||
authTokens.tokenFetch(`${process.env.CHAT_URI}/admin/reports`)
|
||||
.then(res => res.json())
|
||||
.then(json => {
|
||||
setReports(json);
|
||||
})
|
||||
;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
@@ -17,16 +17,13 @@ const NewsEditor = props => {
|
||||
const [index, setIndex] = useState(null);
|
||||
|
||||
//run once
|
||||
useEffect(async () => {
|
||||
const result = await fetch(`${process.env.NEWS_URI}/news/metadata?limit=999`);
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
alert(err);
|
||||
} else {
|
||||
setArticles(await result.json());
|
||||
}
|
||||
useEffect(() => {
|
||||
fetch(`${process.env.NEWS_URI}/news/metadata?limit=999`)
|
||||
.then(res => res.json())
|
||||
.then(json => {
|
||||
setArticles(json)
|
||||
})
|
||||
;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
+34
-21
@@ -1,11 +1,8 @@
|
||||
//react
|
||||
import React, { useContext } from 'react';
|
||||
import { BrowserRouter, Switch } from 'react-router-dom';
|
||||
import React, { useContext, Suspense, lazy } from 'react';
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom';
|
||||
import { TokenContext } from './utilities/token-provider';
|
||||
|
||||
//library components
|
||||
import LazyRoute from './utilities/lazy-route';
|
||||
|
||||
//styling
|
||||
import '../styles/styles.css';
|
||||
|
||||
@@ -13,33 +10,49 @@ import '../styles/styles.css';
|
||||
import Footer from './panels/footer';
|
||||
import PopupChat from './panels/popup-chat';
|
||||
|
||||
//lazy wrappers
|
||||
const Homepage = lazy(() => import('./homepage'));
|
||||
const Signup = lazy(() => import('./accounts/signup'));
|
||||
const Login = lazy(() => import('./accounts/login'));
|
||||
const Account = lazy(() => import('./accounts/account'));
|
||||
const Dashboard = lazy(() => import('./dashboard'));
|
||||
const Recover = lazy(() => import('./accounts/recover'));
|
||||
const Reset = lazy(() => import('./accounts/reset'));
|
||||
const Admin = lazy(() => import('./administration/admin'));
|
||||
const Mod = lazy(() => import('./administration/mod'));
|
||||
const PrivacyPolicy = lazy(() => import('./static/privacy-policy'));
|
||||
const Credits = lazy(() => import('./static/credits'));
|
||||
const NotFound = lazy(() => import('./not-found'));
|
||||
|
||||
const App = props => {
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//default render
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Switch>
|
||||
<LazyRoute exact path='/' component={() => import('./homepage')} />
|
||||
<Suspense>
|
||||
<Routes>
|
||||
<Route exact path='/' element={<Homepage />} />
|
||||
|
||||
<LazyRoute path='/signup' component={() => import('./accounts/signup')} />
|
||||
<LazyRoute path='/login' component={() => import('./accounts/login')} />
|
||||
<LazyRoute path='/account' component={() => import('./accounts/account')} />
|
||||
<LazyRoute path='/dashboard' component={() => import('./dashboard')} />
|
||||
<Route path='/signup' element={<Signup />} />
|
||||
<Route path='/login' element={<Login />} />
|
||||
<Route path='/account' element={<Account />} />
|
||||
<Route path='/dashboard' element={<Dashboard />} />
|
||||
|
||||
<LazyRoute path='/recover' component={() => import('./accounts/recover')} />
|
||||
<LazyRoute path='/reset' component={() => import('./accounts/reset')} />
|
||||
<Route path='/recover' element={<Recover />} />
|
||||
<Route path='/reset' element={<Reset />} />
|
||||
|
||||
<LazyRoute path='/admin' component={() => import('./administration/admin')} />
|
||||
<LazyRoute path='/mod' component={() => import('./administration/mod')} />
|
||||
<Route path='/admin' element={<Admin />} />
|
||||
<Route path='/mod' element={<Mod />} />
|
||||
|
||||
<LazyRoute path='/privacypolicy' component={() => import('./static/privacy-policy')} />
|
||||
<LazyRoute path='/credits' component={() => import('./static/credits')} />
|
||||
<Route path='/privacypolicy' element={<PrivacyPolicy />} />
|
||||
<Route path='/credits' element={<Credits />} />
|
||||
|
||||
<LazyRoute path='*' component={() => import('./not-found')} />
|
||||
</Switch>
|
||||
{ authTokens.accessToken ? <PopupChat /> : <></> }
|
||||
<Footer />
|
||||
<Route path='*' element={<NotFound />} />
|
||||
</Routes>
|
||||
</Suspense>
|
||||
{ authTokens.accessToken ? <PopupChat /> : <></> }
|
||||
<Footer />
|
||||
</BrowserRouter>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useContext } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, Navigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from './utilities/apply-to-body';
|
||||
|
||||
@@ -13,7 +13,7 @@ const Dashboard = props => {
|
||||
|
||||
//misplaced?
|
||||
if (!authTokens.accessToken) {
|
||||
return <Redirect to='/' />;
|
||||
return <Navigate to='/' />;
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useContext } from 'react';
|
||||
import { Link, Redirect } from 'react-router-dom';
|
||||
import { Link, Navigate } from 'react-router-dom';
|
||||
|
||||
import ApplyToBody from './utilities/apply-to-body';
|
||||
|
||||
@@ -13,7 +13,7 @@ const HomePage = props => {
|
||||
|
||||
//misplaced?
|
||||
if (authTokens.accessToken) {
|
||||
return <Redirect to='/dashboard' />;
|
||||
return <Navigate to='/dashboard' />;
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -22,7 +22,6 @@ const NewsFeed = props => {
|
||||
<div className='panel'>
|
||||
<h1 className='text centered'>News Feed</h1>
|
||||
{articles.map((article, index) => {
|
||||
console.log(article)
|
||||
return (
|
||||
<div key={index} className='panel'>
|
||||
<hr />
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import React from 'react';
|
||||
import { Route } from 'react-router-dom';
|
||||
import loadable from '@loadable/component';
|
||||
|
||||
const LazyRoute = props => {
|
||||
const { component, ...lazyProps } = props;
|
||||
|
||||
const lazyComponent = loadable(component);
|
||||
|
||||
return <Route {...lazyProps} component={lazyComponent} />
|
||||
};
|
||||
|
||||
export default LazyRoute;
|
||||
@@ -9,6 +9,12 @@ const TokenProvider = props => {
|
||||
//state to be used
|
||||
const [accessToken, setAccessToken] = useState('');
|
||||
|
||||
//force a logout under certain conditions
|
||||
const forceLogout = () => {
|
||||
localStorage.removeItem("accessToken");
|
||||
setAccessToken("");
|
||||
};
|
||||
|
||||
//make the access token persist between reloads
|
||||
useEffect(() => {
|
||||
setAccessToken(localStorage.getItem("accessToken") || '');
|
||||
@@ -25,7 +31,7 @@ const TokenProvider = props => {
|
||||
let bearer = accessToken;
|
||||
|
||||
//if expired (10 minutes, normally)
|
||||
const expired = new Date(decode(accessToken).exp * 1000) < Date.now();
|
||||
const expired = new Date(decode(accessToken).exp + 600) < Date.now();
|
||||
|
||||
if (expired) {
|
||||
//BUGFIX: if logging out, just skip over the refresh token
|
||||
@@ -47,6 +53,9 @@ const TokenProvider = props => {
|
||||
|
||||
//any errors, throw them
|
||||
if (!response.ok) {
|
||||
if (response.status == 403) {
|
||||
forceLogout();
|
||||
}
|
||||
throw `${response.status}: ${await response.text()}`;
|
||||
}
|
||||
|
||||
@@ -82,6 +91,9 @@ const TokenProvider = props => {
|
||||
|
||||
//any errors, throw them
|
||||
if (!response.ok) {
|
||||
if (response.status == 403) {
|
||||
forceLogout();
|
||||
}
|
||||
throw `${response.status}: ${await response.text()}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,6 @@
|
||||
<meta property="og:description" content="" />
|
||||
</head>
|
||||
<body>
|
||||
<div id = "root"></div>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+1
-1
@@ -278,7 +278,7 @@ networks:
|
||||
`;
|
||||
|
||||
const dockerfile = `
|
||||
FROM node:16
|
||||
FROM node:18-bullseye-slim
|
||||
WORKDIR "/app"
|
||||
COPY . /app
|
||||
RUN mkdir /app/public
|
||||
|
||||
Generated
+1939
-7728
File diff suppressed because it is too large
Load Diff
+30
-29
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mern-template",
|
||||
"version": "1.3.0",
|
||||
"version": "1.4.3",
|
||||
"description": "A website template using the MERN stack.",
|
||||
"main": "server/server.js",
|
||||
"scripts": {
|
||||
@@ -27,36 +27,37 @@
|
||||
},
|
||||
"homepage": "https://github.com/KRGameStudios/MERN-template#readme",
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.14.8",
|
||||
"@babel/preset-env": "^7.14.8",
|
||||
"@babel/preset-react": "^7.14.5",
|
||||
"@loadable/component": "^5.15.0",
|
||||
"babel-loader": "^8.2.2",
|
||||
"clean-webpack-plugin": "^3.0.0",
|
||||
"compression-webpack-plugin": "^8.0.1",
|
||||
"concurrently": "^6.2.0",
|
||||
"css-loader": "^6.2.0",
|
||||
"dateformat": "^4.5.1",
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "^4.17.1",
|
||||
"html-webpack-plugin": "^5.3.2",
|
||||
"@babel/core": "^7.22.5",
|
||||
"@babel/preset-env": "^7.22.5",
|
||||
"@babel/preset-react": "^7.22.5",
|
||||
"babel-loader": "^9.1.2",
|
||||
"clean-webpack-plugin": "^4.0.0",
|
||||
"compression-webpack-plugin": "^10.0.0",
|
||||
"concurrently": "^7.6.0",
|
||||
"css-loader": "^6.8.1",
|
||||
"dateformat": "^5.0.3",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"html-webpack-plugin": "^5.5.3",
|
||||
"jwt-decode": "^3.1.2",
|
||||
"mariadb": "^2.5.4",
|
||||
"query-string": "^7.0.1",
|
||||
"react": "^17.0.2",
|
||||
"react-dom": "^17.0.2",
|
||||
"react-router": "^5.2.0",
|
||||
"react-router-dom": "^5.2.0",
|
||||
"react-select": "^5.2.1",
|
||||
"sequelize": "^6.6.5",
|
||||
"socket.io-client": "^4.1.3",
|
||||
"style-loader": "^3.2.1",
|
||||
"webpack": "^5.46.0",
|
||||
"webpack-bundle-analyzer": "^4.4.2",
|
||||
"webpack-cli": "^4.7.2"
|
||||
"mariadb": "^3.2.0",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-router": "^6.3.0",
|
||||
"react-router-dom": "^6.14.0",
|
||||
"react-select": "^5.7.3",
|
||||
"sequelize": "^6.32.1",
|
||||
"socket.io-client": "^4.7.0",
|
||||
"style-loader": "^3.3.3",
|
||||
"webpack": "^5.88.0",
|
||||
"webpack-bundle-analyzer": "^4.9.0",
|
||||
"webpack-cli": "^5.1.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.12",
|
||||
"webpack-dev-server": "^4.6.0"
|
||||
"nodemon": "^2.0.22",
|
||||
"webpack-dev-server": "^4.15.1"
|
||||
},
|
||||
"overrides": {
|
||||
"semver": "^7.5.2"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user