From 3fd340d14d5cc883e761c34218d2b32342d09135 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 22 Jun 2021 20:50:52 -0500 Subject: [PATCH] Conflict protection --- frontend/package-lock.json | 3 ++- frontend/src/App.jsx | 25 +++++++++++++++++------- frontend/src/axios/putUrlHandler.js | 11 ++++++++++- frontend/src/components/UrlShortener.jsx | 21 ++++++++++++++------ 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 0b39cdb..2c71215 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -13933,7 +13933,8 @@ }, "ssri": { "version": "6.0.1", - "resolved": "", + "resolved": "https://registry.npmjs.org/ssri/-/ssri-6.0.1.tgz", + "integrity": "sha512-3Wge10hNcT1Kur4PDFwEieXSCMCJs/7WvSACcrMYrNp+b8kDL1/0wJch5Ni2WrtwEa2IO8OsVfeKIciKCDx/QA==", "requires": { "figgy-pudding": "^3.5.1" } diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index cb3c4eb..4134d0a 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -9,21 +9,32 @@ import UrlShortener from './components/UrlShortener'; import CheckUrlRedirect from './axios/getUrlHandler'; const slug = window.location.pathname.match(/[^/][^/]*/g); +export const routes = [ + { path: '/', component: UrlShortener }, + { path: '/code', component: TextUpload }, + { path: '/files', component: FileUpload }, +]; export default () => { - // Check to see if slug is worth trying :) - !!slug && CheckUrlRedirect(slug).then((url) => { - window.location.href = url; - }).catch(() => {}); + /* Ensure that there's a slug worth trying, and that it isn't + already a route that should be "protected" + */ + if (!!slug && !routes.map( + ({ path }) => path, + ).includes(window.location.pathname)) { + CheckUrlRedirect(slug).then((url) => { + window.location.href = url; + }).catch(() => {}); + } ReactDOM.render(
- - - + {routes.map(({ path, component }) => ( + + ))} , document.getElementById('root'), diff --git a/frontend/src/axios/putUrlHandler.js b/frontend/src/axios/putUrlHandler.js index 9c9e6b7..99cbf12 100644 --- a/frontend/src/axios/putUrlHandler.js +++ b/frontend/src/axios/putUrlHandler.js @@ -1,3 +1,12 @@ import axios from 'axios'; +import { routes } from '../App'; -export default (url, slug) => axios.put('/api/url', { url, slug }).then((resp) => resp.data); +export default (url, slug) => { + const paths = routes.map(({ path }) => path.substr(1)); + + if (slug && paths.includes(slug)) { + return Promise.resolve(null); + } + + return axios.put('/api/url', { url, slug }).then((resp) => resp.data); +}; diff --git a/frontend/src/components/UrlShortener.jsx b/frontend/src/components/UrlShortener.jsx index cf2aa27..ecfa2b8 100644 --- a/frontend/src/components/UrlShortener.jsx +++ b/frontend/src/components/UrlShortener.jsx @@ -21,12 +21,21 @@ export default () => { putUrlHandler( state.url, state.slug, ).then((data) => { - setState({ - ...state, - result: `${window.location.origin}/${data}`, - loading: false, - error: false, - }); + if (data) { + setState({ + ...state, + result: `${window.location.origin}/${data}`, + loading: false, + error: false, + }); + } else { + setState({ + ...state, + result: null, + loading: false, + error: 'There is a conflict with this slug. Please try a different one!', + }); + } }).catch((ex) => { if (ex.response?.data?.error) { setState({